0
//sLine is the string
for(int l = 0; l < sLine.length(); l++)
{
    string sNumber;
    if(sLine[l] == '-')
    {   
        sNumber.push_back(sLine[l]);
        sNumber.push_back(sLine[l + 1]);
        l++;
    }
    else if(sLine[l] != '\t')
    {
        sNumber.push_back(sLine[l]);
    }
    const char* testing = sNumber.c_str();
    int num = atoi(testing);
    cout << num;
}

I have this for-loop which checks each character of the string and converts every number in this string to be a int. But for some reason, the atoi function is doing it twice so when I cout it, it displays it twice for some reason... Why is that?

example: INPUT 3 3 -3 9 5
-8 -2 9 7 1
-7 8 4 4 -8
-9 -9 -1 -4 -8

OUTPUT 3030-309050 -80-20907010
-70804040-80
-90-90-10-40-80

Danny
  • 9,199
  • 16
  • 53
  • 75
  • 3
    `sNumber.push_back(sLine[l + 1]);` may access beyond the string end. – Vlad Nov 04 '11 at 18:57
  • @Vlad But i've put that there because of negative numbers... – Danny Nov 04 '11 at 18:58
  • It's executing the entire loop twice. – Mooing Duck Nov 04 '11 at 18:58
  • Can you show an example of the output, for some input? And on an unrelated note, use `isdigit` instead of the check against `'\t'`, just in case. :) – Some programmer dude Nov 04 '11 at 18:59
  • 3
    `std::string s = "45"; int i = boost::lexical_cast(s);` btw – Tom Kerr Nov 04 '11 at 18:59
  • @MooingDuck what? How? theres only 1 loop. when I do a cout on sNumber, its fine. Just after the convertion, it displays it twice or 0... – Danny Nov 04 '11 at 19:00
  • @Danny: I understand your intention, but nevertheless: imagine what is going to happen when `sLine` is just `-`. – Vlad Nov 04 '11 at 19:00
  • can you put an example INPUT/OUTPUT to see more clearly what you want to do and the error? – noripcord Nov 04 '11 at 19:01
  • editted my post to show input/output – Danny Nov 04 '11 at 19:05
  • Well, you output 0 when the current character is neither a digit not a `-`. – Vlad Nov 04 '11 at 19:06
  • @Danny This snippet makes no sense. Can you show an expected input and desired output? – Jon Nov 04 '11 at 19:07
  • @jon I have editted my post to show what I've inputted and the output of it. I want the output to be the same as the input (for now). Basically, I just want to convert it from string to int, but when it does the convertion, it has the extra 0 in all characters – Danny Nov 04 '11 at 19:10
  • 1
    Does this answer your question? [How can I convert a std::string to int?](https://stackoverflow.com/questions/7663709/how-can-i-convert-a-stdstring-to-int) – user16217248 Mar 28 '23 at 18:56

4 Answers4

10

It's displaying a zero for all nonrecognized characters, because atoi returns 0 when given a non-numeric string (like a space!)

However, what you want to do, is shockingly simple:

std::stringstream ss(sLine);
int num;
while(ss >> num) {
    cout << num;
}
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
0

Move this:

const char* testing = sNumber.c_str();
int num = atoi(testing);
cout << num;

Below the last } in the code you pasted, i.e. out of the for-loop. Currently you get a separate printout for every character in sLine because it's executed on every iteration of the loop. (The last character in sLine may be a linefeed so this can occur even if you think you wrote only one digit.)

Edit: Also move the declaration of sNumber above the for-loop.

You may also want to change if (sLine[l] == '-') to if (sLine[l] == '-' && (l + 1) < sLine.length()) so you don't access beyond the end of the string if the dash is the final character on the line.

You may also want to rename the variable l to something that looks less like a 1. =)

You may also want to rethink if this is the right way to do this at all (usually if a simple thing gets this complicated, chances are you're doing it wrong).

Arkku
  • 41,011
  • 10
  • 62
  • 84
0

You output extra 0 for the characters which are not digits. The problem is that atoi returns 0 when it cannot convert the input, so your whitespaces are printed as zeroes.

Vlad
  • 35,022
  • 6
  • 77
  • 199
0

That seems like a painful way to recreate the wheel. You'd be better off using a stringstream to parse this.

std::stringstream strm(sLine);
int num;
while(strm >> num)
{
    std::cout << num << std::endl;
}
Jon
  • 3,065
  • 1
  • 19
  • 29
  • 3
    Please don't encourage the use `eof()` or `fail()` as loop conditions. Doing so almost always results in buggy code. Rather do `while ( strm >> num ) { ... } `. For a demonstration of the evil power of `eof()`: http://ideone.com/ZKa67 – Robᵩ Nov 04 '11 at 19:25