1. ## Switch Statement

Hey for my program I am using a switch statement to convert string numbers to int numbers. Its in a function called Set, i'm going to post the whole thing. Its not working it prints out random things and I can't find a pattern to tell whats wrong. I also have another function called Print that prints out the numbers stored in the array and that might be the problem area but I am unsure.
Code:
```Set(string num)
{
int i;
if (num[0] == '-')
{
positive = false;
num.erase(0,1);
numDigits = num.size();
digits = new int[numDigits];
for(i=0;i<numDigits;i++)
{
switch (num[i])
{
case '0':
digits[numDigits-i] = 0;
break;
case '1':
digits[numDigits-i] = 1;
break;
case '2':
digits[numDigits-i] = 2;
break;
case '3':
digits[numDigits-i] = 3;
break;
case '4':
digits[numDigits-i] = 4;
break;
case '5':
digits[numDigits-i] = 5;
break;
case '6':
digits[numDigits-i] = 6;
break;
case '7':
digits[numDigits-i] = 7;
break;
case '8':
digits[numDigits-i] = 8;
break;
case '9':
digits[numDigits-i] = 9;
break;
}
}
}
else
{
positive = true;
numDigits = num.size();
digits = new int[numDigits];
for(i=numDigits;i>=0;i--)
{
switch (num[i])
{
case '0':
digits[i] = 0;
break;
case '1':
digits[i] = 1;
break;
case '2':
digits[i] = 2;
break;
case '3':
digits[i] = 3;
break;
case '4':
digits[i] = 4;
break;
case '5':
digits[i] = 5;
break;
case '6':
digits[i] = 6;
break;
case '7':
digits[i] = 7;
break;
case '8':
digits[i] = 8;
break;
case '9':
digits[i] = 9;
break;
}
}
}
}```

2. Code:
```template<class T>
void StrToNumber(std::string str, T& num)
{
std::stringstream ss; // include <sstream>
ss << str;
ss >> num;
}

// usage

int a = 0;
std::string str = "1234";
StrToNumber(str, a);

// a == 1234```

3. stringstream will do this for you. However, on your note: If the problem is in the printing, then most likely it is the printing function. How did you implement that?
PS: Your code could be consolidated into one switch for sure.

4. Suppose we're determined not to use stringstream or atoi.

Code:
```//assuming ASCII char representation (if that isn't guaranteed)
//uses <string> and <cmath>
int get_int(std::string num_str)
{
int n = 0;  //the actual int
unsigned short exp = 0;
std::string::reverse_iterator iter = num_str.rbegin();

while(iter != num_str.rend())
{
if(*iter >= 48 && *iter <= 57)   //if it's coded for ASCII num representation
{
n += (*iter - 48) * std::pow(10, exp);  //update n
++iter;
++exp;
}
else if(*iter == '-')   //if it's a minus sign
{
if(iter + 1 != num_str.rend()) error();  //negative sign not the first char? Error.
n *= -1;
return n;
}
else   //if it's anything else (no hex, sci notation -- just plain old int)
{
error();
}
} //end while loop
return n;
}```

5. >> if(*iter >= 48 && *iter <= 57)
Why use 48 and 57? Just use '0' and '9'. It will work just the same. It will also work for any character set, not just ASCII.

6. Code:
```switch (num[i])
{
case '0':
digits[i] = 0;
break;
case '1':
digits[i] = 1;
break;
case '2':
digits[i] = 2;
break;
case '3':
digits[i] = 3;
break;
case '4':
digits[i] = 4;
break;
case '5':
digits[i] = 5;
break;
case '6':
digits[i] = 6;
break;
case '7':
digits[i] = 7;
break;
case '8':
digits[i] = 8;
break;
case '9':
digits[i] = 9;
break;
}```
OMG, so much code instead of 1 line:
digits[i] = num[i] - '0';

Code:
`std::pow(10, exp);`
Do you know how expensive it is? You are iterating from 0 - use multiplication on every iteration to get the multiplier...

7. I'm trying to do this without iterators or vectors, just straight arrays and basic stuff.

8. The code is not relevantly different.

9. Originally Posted by DarkDot
I'm trying to do this without iterators or vectors, just straight arrays and basic stuff.
Like this:
Code:
```    char myString[] = "-1200";
signed int number = 0;
int i;
int neg = 0;

for (i=0; i < strlen(myString); i++)
{
number *=10;
if (myString[i] == '-' && number == 0)
neg = !neg;
else
if (!neg)
number += (myString[i] - '0');
else
number -= (myString[i] - '0');
}```

10. I don't understand the above code. I'm going to post my changed code as well as my print function.
Code:
```Set(string num)
{
int i;
if (num[0] == '-')
{
positive = false;
num.erase(0,1);
numDigits = num.size();
digits = new int[numDigits];
for(i=0;i<numDigits;i++)
{
digits[i] = num[i]-'0';
}
}
else
{
positive = true;
numDigits = num.size();
digits = new int[numDigits];
for(i=numDigits;i>=0;i--)
{
digits[i] = num[i]-'0';
}
}
}

Print()
{
int i;
if(positive == false)
{
cout << "-";

for(i=1;i<=numDigits;i++)
{
cout << digits[numDigits-1];
}
}
else if(positive == true)
{
for(i=1;i<=numDigits;i++)
{
cout << digits[numDigits-1];
}
}
}```

11. >> for(i=numDigits;i>=0;i--)
Why does this attempt to go backwards when the other code all goes forward? You are starting one place too early here.

Also, in your Print function you aren't using i at all in the loops, you are just printing the last digit over and over.

12. i think thats my problem it should print - i not -1