Parser
-
I wrote this string parser. It compiles without errors. But when I launch the program, it doesn't seem to quit the loop.... What's wrong?
#include int Parse(char *String)
{
int Temp = 0;
int Result = 0;
int Length = int(strlen(String)); // The length of the string.
bool Negative = false;
// Main cycle. Loop through all the symbols of the string.
for (int i = 0; i < Length; i++)
{
if (i = 0)
{
// If the first symbol of the string is "-". Make it negative.
if (String[i] == 45)
{
Negative = true;
}
}
else
{
// Make sure the input symbol is a number. Return 0 if it's not.
if (String[i] < 48)
{
return 0;
}
else if (String[i] > 57)
{
return 0;
}
else
{
// For aech number, perform specific operations.
switch (String[i])
{
// If it's 0.
case 48:
Temp = 1;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 1.
case 49:
Temp = 1;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 2.
case 50:
Temp = 2;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 3.
case 51:
Temp = 3;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 4.
case 52:
Temp = 4;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 5.
case 53:
Temp = 5;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 6.
case 54:
Temp = 6;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 7.
case 55:
Temp = 7;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 8.
case 56:
Temp = 8;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 9.
case 57:
Temp = 9;
for (int j = 0; -
I wrote this string parser. It compiles without errors. But when I launch the program, it doesn't seem to quit the loop.... What's wrong?
#include int Parse(char *String)
{
int Temp = 0;
int Result = 0;
int Length = int(strlen(String)); // The length of the string.
bool Negative = false;
// Main cycle. Loop through all the symbols of the string.
for (int i = 0; i < Length; i++)
{
if (i = 0)
{
// If the first symbol of the string is "-". Make it negative.
if (String[i] == 45)
{
Negative = true;
}
}
else
{
// Make sure the input symbol is a number. Return 0 if it's not.
if (String[i] < 48)
{
return 0;
}
else if (String[i] > 57)
{
return 0;
}
else
{
// For aech number, perform specific operations.
switch (String[i])
{
// If it's 0.
case 48:
Temp = 1;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 1.
case 49:
Temp = 1;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 2.
case 50:
Temp = 2;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 3.
case 51:
Temp = 3;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 4.
case 52:
Temp = 4;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 5.
case 53:
Temp = 5;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 6.
case 54:
Temp = 6;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 7.
case 55:
Temp = 7;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 8.
case 56:
Temp = 8;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 9.
case 57:
Temp = 9;
for (int j = 0; -
I wrote this string parser. It compiles without errors. But when I launch the program, it doesn't seem to quit the loop.... What's wrong?
#include int Parse(char *String)
{
int Temp = 0;
int Result = 0;
int Length = int(strlen(String)); // The length of the string.
bool Negative = false;
// Main cycle. Loop through all the symbols of the string.
for (int i = 0; i < Length; i++)
{
if (i = 0)
{
// If the first symbol of the string is "-". Make it negative.
if (String[i] == 45)
{
Negative = true;
}
}
else
{
// Make sure the input symbol is a number. Return 0 if it's not.
if (String[i] < 48)
{
return 0;
}
else if (String[i] > 57)
{
return 0;
}
else
{
// For aech number, perform specific operations.
switch (String[i])
{
// If it's 0.
case 48:
Temp = 1;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 1.
case 49:
Temp = 1;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 2.
case 50:
Temp = 2;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 3.
case 51:
Temp = 3;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 4.
case 52:
Temp = 4;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 5.
case 53:
Temp = 5;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 6.
case 54:
Temp = 6;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 7.
case 55:
Temp = 7;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 8.
case 56:
Temp = 8;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 9.
case 57:
Temp = 9;
for (int j = 0; -
I wrote this string parser. It compiles without errors. But when I launch the program, it doesn't seem to quit the loop.... What's wrong?
#include int Parse(char *String)
{
int Temp = 0;
int Result = 0;
int Length = int(strlen(String)); // The length of the string.
bool Negative = false;
// Main cycle. Loop through all the symbols of the string.
for (int i = 0; i < Length; i++)
{
if (i = 0)
{
// If the first symbol of the string is "-". Make it negative.
if (String[i] == 45)
{
Negative = true;
}
}
else
{
// Make sure the input symbol is a number. Return 0 if it's not.
if (String[i] < 48)
{
return 0;
}
else if (String[i] > 57)
{
return 0;
}
else
{
// For aech number, perform specific operations.
switch (String[i])
{
// If it's 0.
case 48:
Temp = 1;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 1.
case 49:
Temp = 1;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 2.
case 50:
Temp = 2;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 3.
case 51:
Temp = 3;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 4.
case 52:
Temp = 4;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 5.
case 53:
Temp = 5;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 6.
case 54:
Temp = 6;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 7.
case 55:
Temp = 7;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 8.
case 56:
Temp = 8;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 9.
case 57:
Temp = 9;
for (int j = 0;You may want to start and write defensively. Instead of
if (i == 0)
Writeif (0 == i)
In this case you can not change the value ofi
by accident. It is so easy to leave out an equal sign. :) Ant. I'm hard, yet soft.
I'm coloured, yet clear.
I'm fruity and sweet.
I'm jelly, what am I? Muse on it further, I shall return! - David Walliams (Little Britain) -
I wrote this string parser. It compiles without errors. But when I launch the program, it doesn't seem to quit the loop.... What's wrong?
#include int Parse(char *String)
{
int Temp = 0;
int Result = 0;
int Length = int(strlen(String)); // The length of the string.
bool Negative = false;
// Main cycle. Loop through all the symbols of the string.
for (int i = 0; i < Length; i++)
{
if (i = 0)
{
// If the first symbol of the string is "-". Make it negative.
if (String[i] == 45)
{
Negative = true;
}
}
else
{
// Make sure the input symbol is a number. Return 0 if it's not.
if (String[i] < 48)
{
return 0;
}
else if (String[i] > 57)
{
return 0;
}
else
{
// For aech number, perform specific operations.
switch (String[i])
{
// If it's 0.
case 48:
Temp = 1;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 1.
case 49:
Temp = 1;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 2.
case 50:
Temp = 2;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 3.
case 51:
Temp = 3;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 4.
case 52:
Temp = 4;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 5.
case 53:
Temp = 5;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 6.
case 54:
Temp = 6;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 7.
case 55:
Temp = 7;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 8.
case 56:
Temp = 8;
for (int j = 0; j < (Length - i); j++)
{
Temp *= 10;
}
Result += Temp;
Temp = 0;
break;
// If it's 9.
case 57:
Temp = 9;
for (int j = 0; -
why not edit you message and put your code into a <pre></pre> block for helping us to see the indentation ?!:suss:
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
oh, that's better now ;) but the #include still don't appear. be carful to type & l t ; to see a
<
and a & g t ; for a>
. ok, now, you parser seem to be parser a little than one i wrote before. I cannot say to you what is wrong in your code, but I can give you an advice. see the source of my VisualCalc to perform your code... If you need any question about your source, ask it to me here ; if it is about my code, post your request at the bottom of the VisualCalc article. See you,
TOXCCT >>> GEII power
[toxcct][VisualCalc]