0

Heres a snippet of my code:

int main();
{
     string cmd1;
     int cmd2;

     cout << "Command: " << flush;
     cin >> cmd1;

     if (cmd1 == "playlist")
     {
	     cin >> cmd2;
	     if (cmd2==XXXXXX)
	     {
		     cout << "Illegal command: " << cmd1 << endl;
	     }
	     else
	     cmdPlaylist(cmd2);
     }

return 0;
}

If input is playlist 6

then action would be run cmdPlaylist(6);

If input is playlist

then action would cout << "Illegal Command: " << cmd1 << endl;

My question is:

How do I get my program to print

cout << "Illegal Command: " << cmd1 << endl;

if there is no cmd2.


Is there some function I can use that states

if cmd2!=an integer
or
if cmd2!=no input/invalid

then cout << "Illegal Command: " << cmd1 << endl;

3
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by shuda009
0

Oddly enough, most of the problems with stream input are solved the same way: read an entire line into a string and then parse that line for validity:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string line;

    std::cout<<"> ";

    if ( getline ( std::cin, line ) ) {
        std::istringstream in ( line );
        std::string command;
        int argument;

        if ( in>> command >> argument )
            std::cout<<"Valid\n";
        else
            std::cout<<"Invalid\n";
    }
}
0

1. here's this forum announcement: http://www.daniweb.com/forums/announcement8-3.html
[code=cplusplus] source

[/code]
2. See an example of these cmds parsing:

// don't forget to include <sstream>
typedef istringstream::pos_type Pos;
// cmd arg parsing sample only:
void cmdGet()
{
    string line, cmd, arg;
    bool intarg = false;
    int  iarg;
    cout << "Type instruction: "; // no need in flush
    if (getline(cin,line)) {
        istringstream is(line); // parse line
        if (is >> cmd) {    // get 1st token
            Pos pos = is.tellg();//save position
            if (is >> arg) {    // has arg
                is.seekg(pos);  // read again
                if (is >> iarg) // try to get int
                    intarg = true;
                else
                    intarg = false; // not-a-number
            } else { // no arg
                arg.clear(); // arg.empty() == true
            }
            // Process cmd & arg, for example:
            cout << cmd;
            if (!arg.empty()) { // has arg
                cout << ' ' << arg;
                if (intarg)
                    cout << " (integer)";
            }
            cout << endl;
        } else { // no cmd at all
            cout << "Try again...\n";
        }
    } else { // cin closed
        return; // ctrl-z on Windows, for example
    }
}

Try this. Adopt it (it's freeware in the public domain ;)...

0

Lets say I have an input

playlist 6

Then I need to extract 6 from the string and store it as an integer how would I do it?

I know

springstream in(6)

then in become 6

would convert the 6 itself

but if I apply

springstream in(playlist 6)

then in becomes 0.

0

Did you even bother to read my example? You butchered it beyond comprehension despite the fact that it already does what you're currently asking for. :icon_rolleyes:

[edit]
By the way, I'm merging this into the original thread so you don't confuse the bejeezus out of everyone.
[/edit]

0

Did you even bother to read my example? You butchered it beyond comprehension despite the fact that it already does what you're currently asking for. :icon_rolleyes:

[edit]
By the way, I'm merging this into the original thread so you don't confuse the bejeezus out of everyone.
[/edit]

Sorry, its not that I tried to butcher. It's just I don't really understand what you've given me.

//Edit
Now I do, sorry once again... Don't kill me! I'm sorry!

Most importantly thanks alot!

0

>Don't kill me! I'm sorry!
Rawr! ;)

hehe XD

I've got another question :P

I have 3 input commands:

info
playlist n
quit

and I'm using an if statement to identify command = input.

e.g
cout << "Command: " << flush;
getline(cin, line);

if (cmd == quit)
return 0;
else if (cmd == info)
cmdInfo(cmd);
else if (cmd == playlist n)
cout << "This is tripping me up" << endl;
else
cout << "Illegal Command: " << cmd << endl;

The problem with Playlist n is n could be any number.

So what I want to know is if there is any command/function that allows me to only read the first part of the string?

I tried using getline(cin, line, ' ');

However, it put the program into an infinite loop.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.