2

I have this code:

#include <iostream>
#include <vector>
#include <ctime>
#include <math.h>
#include <string>

using namespace std;

int main()
{
    srand(time(0));
    string command_one;
    int slot;
    cout<<"One chip or Quit?\n";
    getline(cin, command_one);
    if(command_one=="One chip"){
        cout<<"Pick a slot between 0 and 8 (inclusive)\n";
        cin>>slot;
        if(slot>=0 and slot<=8){
            double position=slot;
        }
        else{
            cout<<"This Option is invalid!\n";
            main();
        }
    }
    else if(command_one=="Quit"){
        cout<<"Have a nice day! :D";
    }
    else{
        cout<<"This Option is invalid!\n";
        main();
    }
}

When it hits the else loop nested in the if(command_one=="One chip") it returns

"This Option is invalid!
One chip, Multi chip, or Quit?
This Option is invalid!
One chip, Multi chip, or Quit?"

But it should be:

"This Option is invalid!
One chip, Multi chip, or Quit?"

How can this be fixed?

3
  • Don't recurse into main? Generally you would make a new function for this and call it from main. Commented Sep 10, 2014 at 15:29
  • extract the recursive functionality to other method, don't use main to recurse. By the way this line: double position=slot; don't do anything. Commented Sep 10, 2014 at 15:29
  • clean up the trailing newline from your input stream Commented Sep 10, 2014 at 15:31

3 Answers 3

1
cin>>slot;

This leaves the new-line character following the number in the stream's buffer. The next call to getline will find that, giving an empty line.

You can use ignore to ignore the new-line character and any other garbage at the end of the line:

cin.ignore(-1, '\n'); // ignore any number of characters up to and including new-line

Note that calling main is not allowed (although some compilers might allow it); and unbounded recursion like this could eventually cause a stack overflow. Consider using a loop instead.

Sign up to request clarification or add additional context in comments.

Comments

0

cin doesn't take the \n character.getline() takes it as an empty line input. Put a getchar() to take that \n character.

#include <iostream>
#include <vector>
#include <ctime>
#include <math.h>
#include <string>
#include <stdio.h>

using namespace std;

int main()
{
    string command_one;
    int slot;
    cout<<"One chip or Quit?\n";
    getline(cin, command_one);
    if(command_one=="One chip")
    {
        cout<<"Pick a slot between 0 and 8 (inclusive)\n";
        cin>>slot;

        //putting a getchar() after cin

        getchar();
        if(slot>=0 and slot<=8)
        {
            double position=slot;
        }
        else
        {
            cout<<"This Option is invalid!\n";
            main();
        }
    }
    else if(command_one=="Quit")
    {
        cout<<"Have a nice day! :D";
    }
    else
    {
        cout<<"This Option is invalid!\n";
        main();
    }
}

1 Comment

when you input number from keyboard and press enter the new line character '\n' is also taken that's why your program was generating output twice but placing a getchar() function after cin the getchar() function takes the new line character '\n' and the program generates output only once...
0

std::getline:

Extracts characters from input and appends them to str until one of the following occurs (checked in the order listed)
a) end-of-file condition on input, in which case, getline sets eofbit.
b) the next available input character is delim, as tested by Traits::eq(c, delim), in which case the delimiter character is extracted from input, but is not appended to str.

Therefore, you need to consume the leftover \n character.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.