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.

Recommended Answers

All 11 Replies

What do you think of: cin >> [I]your_command[/I] >> [I]your_integer_parameter[/I]; ?

What do you think of: cin >> [I]your_command[/I] >> [I]your_integer_parameter[/I]; ?

The problem with cin >> a >> b;

is that it'll make the program prompt for 2 input values every time.

There are other inputs aside from playlist n such as info and quit that only have one value. Therefore cin >> a >> b; won't work for other commands in this program.

The problem with cin >> a >> b;
is that it'll make the program prompt for 2 input values every time.

Another approach:
Get the whole line (you were already doing this)
Make use of string streams to parse the whole command !

It seems you don't understand my post in the recent thread where Narue kindly gave you the answer:
http://www.daniweb.com/forums/thread194034.html
There was your current problem solution in the code snippet.
Probably you have a good chance to post your problem in loop forever manner ;)

It seems you don't understand my post in the recent thread where Narue kindly gave you the answer:
http://www.daniweb.com/forums/thread194034.html
There was your current problem solution in the code snippet.
Probably you have a good chance to post your problem in loop forever manner ;)

You don't understand what I'm saying.

Condition:
If Input = string with more than one integer or no integer then it is invalid.

e.g
playlist
playlist 200 200

If Input = string with only one integer then it is valid

e.g
playlist 5

Those are my conditions.

Narue gave me a code that took integers(plural) from a string and stores them into a variable.

What I want is to return invalid if there is more than one integer present.

Another approach:
Get the whole line (you were already doing this)
Make use of string streams to parse the whole command !

I'm not quite sure what you mean, but what I think your getting at is that when I get two or more integers I break the line up and only extract the pieces of information I need from the line (1 integer 1 string).

However what I want is a command of 1 string 1 integer. Anymore integers and I just ignore the command or cout invalid.

>You don't understand what I'm saying.
;););)

>You don't understand what I'm saying.
;););)

I'm glad you noticed :yawn:

If I understood C++ as well as half of you peeps do I wouldn't be asking questions and I would also notice what I'm missing. It doesn't help if you just tell me I have the information.

Information is useless if you don't understand how to use it.

If you have the ability to look at a book and immediately understand every single detail of the subject being described. Then congratulations to you. Your a genius, but I'm not.

You don't understand what I'm saying

I don't believe that ArkM doesn't understand what you're saying, in fact he does understand what you mean (perfectly), take a look at his code in your previous thread, it's very understandable and well documented.
And about the string streams: I linked that post to a web page, it might be helpful if you read this info as well, don't understand it directly? Well, then you'll have to Google a bit, remember: Google is your friend!

Information is useless if you don't understand how to use it.

Or if you don't do any effort to understand and use it!

If you have the ability to look at a book and immediately understand every single detail of the subject being described. Then congratulations to you. Your a genius, but I'm not.

Sure he's a genius, probably he wrote the code himself so he won't have to take a book to understand his own code, by the way: nearly every line is commented, did you notice this?

I don't believe that ArkM doesn't understand what you're saying, in fact he does understand what you mean (perfectly), take a look at his code in your previous thread, it's very understandable and well documented.
And about the string streams: I linked that post to a web page, it might be helpful if you read this info as well, don't understand it directly? Well, then you'll have to Google a bit, remember: Google is your friend!

You know how sometimes you google one thing then it just pops up with something else you don't know? Then the cycle just keeps repeating itself? Unfortunately I'm in this sort of situation because I haven't learned enough fundamentals.

Anyway it doesn't matter, I have all the time I need to read up on it since its already past the deadline.

>You know how sometimes you google one thing then it just pops up with something else you don't know?

Yes, but essentially you already know string streams, if you can do I/O with cin/cout, you do already know the basics of string streams, so I would advice to Google up some information about string streams :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.