I'm writing a program where I have a variety of input commands.

Lets say here are my three commands:

info
playlist #
quit

info - gives me information
quit - terminates the program

The problem:

playlist # - The command playlist # means I need to cout a number of songs according to the size of #.

For example:

Playlist 1

I love you

Playlist 3

I love you
Your a Jerk
Never lie

Playlist 5

I love you
Your a Jerk
Never lie
Jealousy
Ultimate Dilema

and so on.

The problem is the input method.

For example if I use cin >> a >> b;

Then it'll prompt for two inputs even if there should only be one.

e.g info and quit only have one input value.

If I use getline(cin, string)

Then I take all the inputs into consideration, however that means I can't apply playlist # to a function.

Lets say cmdPlaylist(int b)

So my question is how do I split something in the getline?

Recommended Answers

All 3 Replies

http://www.daniweb.com/forums/thread27905.html

Thanks, but it doesn't fix my problem.

You see the thing is n of 'playlist n' isn't fixed.

and I have a set of different inputs.

I have to program the input commands to react differently.

e.g

cout << "Command: " << flush;       //Prompt for input
    getline(cin, cmd1);

    if (cmd1 == "quit")
        return 0;
    else if (cmd1 == "info")
    {
        cmdInfo(cmd1);                         //Calls info function
    }
    else if (cmd1 == "playlist")
        cmdPlaylist(cmd2);                   //Calls playlist function
    else
        cout << "Illegal command: " << cmd1 << endl;

What I'm getting at here is how can I recognize playlist n, before splitting it.

Lets say for example

I enter playlist 6

it'll return as illegal command according to the code above, because I'm using getline(cin, cmd1); as input identifier.

If I use cin >> a >> b; then playlist 6 will be fine but info and quit won't work unless you put two input values for both.

e.g info info or quit quit

I tried using

getline(cin, cmd1, ' ');

but it just ended up looping endlessly saying it was an illegal command.

Member Avatar for jencas

http://www.cplusplus.com/reference/string/string/substr/

1. Use sunbstr to recognize if left part of cmd1 equals "playlist"
2. If yes, separate number from cmd1 (i.e. by tokenizing)
3. pass number to the function which handles your playllist

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.