Need to get a int value after a string

Hi,

I will not separate from the others in this great forum - I mean that I'm new to C++.

I have to define some strings that I'll use for commands in a simulation:
Like get, put, etc...
Then after typing the correct string you get some output result.
I've managed to do that.
Now my problem is that I need to put some parameters after the string and get the value of this parameter into variable.
For example
get 5,5,5 - I need to get all the values in separate variables

My question is - do I need to compare the string and after that using streamstring to get the value or it will work without comparison?

Thanks in advance


This is a possible solution:
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
   int args[3], a;
   string input;
   char ch;
   while(true)
   {
	cin.get(ch);
	if (cin.eof() || ch=='\n') break;
	if (isalpha(ch))
	{
	     cin.putback(ch);
	     cin >> input;
	     a=0;
	}
	if (isdigit(ch))
	{
	     cin.putback(ch);
	     cin >> args[a++];
	}
   }
   /*now if input was "get 5,6,7": 
      input == "get"
      args[0] == 5
      args[1] == 6
      args[2] == 7
    */
   return 0;
}


if you want something easyer this is better:
1
2
3
int args[3];
string input;
cin >> input >> args[0] >> args[1] >> args[3];

but this will accept an input like this:
get 5 6 7
Last edited on
Topic archived. No new replies allowed.