Splitting a string

I want be able to input a string such as "box 3 4 5" and then assign each of the 4 items to a new variable to do calculations (volume, surface area).

I'm new to c++ and am having difficulty assigning the first variable (box) as a word and the next 3 as integers.

My code below will split the string but how do I then separate out the variables?




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
  istringstream iss;
  string coms;
  getline(cin, coms);
                                                                  
  iss.str (coms);
  for(int i = 0; i <=coms.length(); i++)
    {
      string val;
      iss >> val;
      cout << "i = " << i;
      cout << "val = " << val<< endl;
     }
}
Last edited on
Please use code tags!

"How to use code tags"
http://www.cplusplus.com/articles/jEywvCM9/

An an incentive, if your code has a main() (and includes the required headers), you can run it directly off the web page by clicking on the little cog button at the top right of the code region. (If the button's not there, try refreshing your browser.)

Anyway...

My code below will split the string ...

Your code is trying to extract too many tokens (you're call iss >> val once per letter in your string.) Adding error reporting code you see:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
  istringstream iss;
  string coms;
  //getline(cin, coms);
  coms = "box 3 4 5"; // for testing online
  // C++ Shell does allow input but I'm too lazy...

  iss.str (coms);
  for(int i = 0; i <=coms.length(); i++)
  {
    string val;
    iss >> val;
    if(iss.good())
    {
      cout << "i = " << i;
      cout << "val = " << val<< endl;
    }
    else
    {
      cout << "iss not good!" << endl;
    }
  }
  return 0;
}


you get

i = 0val = box
i = 1val = 3
i = 2val = 4
iss not good!
iss not good!
iss not good!
iss not good!
iss not good!
iss not good!
iss not good!


Try

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
  istringstream iss;
  string coms;
  //getline(cin, coms);
  coms = "box 3 4 5"; // for testing online
  // C++ Shell does allow input but I'm too lazy...

  iss.str (coms);
  string val;
  int i = 0;
  while(iss >> val)
  {
    cout << "i = " << i++;
    cout << "val = " << val<< endl;
  }
 
  return 0;
}


how do I then separate out the variables?


Do you have a set sequence in which the value will appear? If so, then extract into the necessary variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
  istringstream iss;
  string coms;
  //getline(cin, coms);
  coms = "box 3 4 5"; // for testing online

  iss.str (coms);
  string val;
  int w = 0;
  int h = 0;
  int d = 0;

  if(iss >> val)
  {
    cout << "val = " << val<< endl;      
    if(    (val == "box")
        && (iss >> w >> h >> d) )
    {
        cout << "w   = " << w << endl;
        cout << "h   = " << h << endl;
        cout << "d   = " << d << endl;
        cout << "vol = " << (w * h * d) << endl;       
    }
    else
    {
        cout << "unknown type?" << endl;
    }
  }
 
  return 0;
} 


Andy
Last edited on
Ok thanks of the heads up on code formatting.

what I need to do is take those "vals" after "box" and turn them into integer values that I can multiply together to get the volume and surface area

also if i input "triangle 6 10" I want it to recognize it as a triangle and multiply base times height to get a calculation for volume again so again i need to separate triangle from the integer values.
fantastic that worked thank you!


is there anything you can tell me about the way you used

if (iss >> val)



what exactly is the computer being told to do?
Last edited on
If you look at the declaration of operator>> you'll see it returns a reference to the istream you're extracting from, e.g.

istream& operator>> (int& val);
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

and (a bit tortuous...) if you check out std::basic_ios, the parent class of std::istream, you'll find it implements:
http://www.cplusplus.com/reference/ios/basic_ios/operator_bool/

operator void*() const; (C++98)

or

explicit operator bool() const; (C++11)

which

Returns whether an error flag is set (either failbit or badbit).

which allows you to test if an istream (or ostream or iostream, for that matter) is in a valid state.

So in the case of if (iss >> val) the if() is checking to see if iss (as returned by operator>>) is still in an ok state.

For a more lucid explanation see:

How does that funky while (std::cin >> foo) syntax work?
https://isocpp.org/wiki/faq/input-output#istream-and-while

Andy

PS If you add the headers to you code above it'll be compilable online!
Last edited on
Topic archived. No new replies allowed.