In my previous posts, I asked about how to transform a part of a string into an integer.
Narue kindly gave me the answer and I've implemented it into my code. Here is my code:
int main()
{
string line;
string cmd;
while (cmd!="quit")
{
cout << "Command: " << flush;
getline(cin, line);
istringstream in(line);
int integer;
if (line == "quit")
return 0;
else if (line == "info")
cout << "Run cmdInfo(cmd)" << endl;
else if (in >> cmd >> integer)
cout << "Run cmdPlaylist(integer)" << endl;
else
cout << "Invalid Command: " << line << endl;
}
return 0;
}
Basically what this program does is produce certain responses to specified inputs.
There are 4 types inputs:
1. info (run cmdInfo)
2. quit (terminate program)
3. playlist n (run cmdInfo)
4. everything else (cout Invalid Command: cmd)
The problem I'm having is the playlist input.
Test 1: Successful
Input = playlist 6
Desired Output = Run cmdPlaylist(integer)
Actual Output = Run cmdPlaylist(integer)
Test 2: Unsuccessful
Input = playlist 200 200
Desired Output = Invalid Command: playlist 200 200
Actual Output = Run cmdPlaylist(integer)
What I want the program to do is run cmdPlaylist(integer) when there is playlist and 1 integer as input. When there is more than one integer or no integer then cout Invalid Command.
I'm not sure how to do this and I'd like some help.
I thought about using delimiters
e.g
getline(cin, line, ' ')
However this would mean I would disregard even the first integer when I only want to discard the second.