2

My input format :

192,21,4 33,2,1 23,7,61

and I want to obtain only numbers.

#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
using namespace std;

int strtoint(const std::string str){      //converting the string to integer
    float a = str.length();
    int result = 0;
    for(int i=1; i<=str.length(); i++){
            result+=(str[i-1]-'0')*pow(10,(a-1));
            a--;
    }
    return result;
}

int main ()
{
  string name;
  string segment;
  while(getline (cin,name, ' ')){
        stringstream test(name);
        while(getline(test, segment, ','))
            {
              cout << segment <<" "<<strtoint(segment)<<endl;
            }
  }
  return 0;
}

I didn't obtain last one (61). So output is 192 21 4 33 2 1 23 7.

How do I get all the numbers?

sarikaya
  • 301
  • 4
  • 12
  • 1
    I just ran this in ideone and got all 9 integers, printed twice, each on a separate line: http://ideone.com/lvzTpq – Derek Jan 29 '14 at 21:40
  • @notch, are you also getting 572 on the end of the output? I believe that would be because getline is adding newline character ("\n") to segment string at end of 61. If nothing shows up at all, perhaps getline doesn't know to return because it doesn't receive space (" ") nor newline char ("\n") – Joy Rê Jan 29 '14 at 22:11
  • Derek's input adds the newline after the 61. `getline` doesn't care about \n, and also removes the delimiter. – Peter Jan 29 '14 at 22:29
  • yes, I am also getting 572 on the end of the output, what can we do for solution? – sarikaya Jan 29 '14 at 23:25

3 Answers3

1

I don't know why your implementation doesn't work with the inputs you provided - Derek showed in the comments that is should work. Any extra characters that it understands wrong would be interpreted as numbers by your program, so if there's some hidden input (tab, newline, dot instead of comma, etc.) it should output garbage instead of 61, rather than nothing.

Rather than using getline, I would use other ways to split a string. Primarily because getline implies that it reads an entire line and therefore it can be hard for others to understand what you actually do. See this thread for how to split a string: Split a string in C++?

When parsing numbers you will run into issues as soon as you run into unexpected characters, e.g."a". There are better and simpler ways to parse numbers: How to parse a string to an int in C++?

Community
  • 1
  • 1
Peter
  • 5,608
  • 1
  • 24
  • 43
1

How about:

int main()
{
    std::string input = "192,21,4 33,2,1 23,7,61";  
    std::stringstream lineStream(input);    

    std::vector<int> numbers;
    std::string number;

    while (std::getline(lineStream, number, ','))
    {
        numbers.push_back(atoi(number.c_str()));
    }

    for (int n : numbers)
    {
        std::cout << n << " ";
    }
}
jaho
  • 4,852
  • 6
  • 40
  • 66
0

If you do not input a space (" ") after 61, getline will not return, and thus never print 61. It does not return upon receiving newline character ("\n") if you specify a delimeter.

Joy Rê
  • 159
  • 4