Sign in to follow this  
NUCLEAR RABBIT

cout and cin question

Recommended Posts

Hello, I created a very simple program that was in my book (c++), and something odd went wrong. I've used cin.get() a plethora of times and this has never happened. I tried using system("pause"); and it works, but for some reason when i try and use cin.get(), the program just ends when it gets to that part, it doesnt wait for the user to press enter.
#include <iostream>
using namespace std;

int ask(int high, int low =1);

int main()
{
    int num = ask(5);
    cout << "Thank you for entering " << num << "\n\n";
    
    int num2 = ask(10,5);
    cout << "Thank you for entering " << num2 << "\n\n";
    
    cout << "Press Enter to exit.";
    cin.get();
    
    return 0;
}

int ask(int high, int low)
{
    int number;
    
    do {
        cout << "Please enter a number between " << low << " - " << high << ": ";
        cin >> number;
    }while(number > high || number < low);
    
    return number;
}

Share this post


Link to post
Share on other sites
cin.get() simply reads a byte from the input stream. The pausing behaviour is a side effect of the fact that the I/O is "blocking": i.e. if data is expected and not available, everything waits until data is provided. However, the console is also line-buffered, meaning that data doesn't appear to your program until the next newline character.

When you read a value off the stream with the operator>> it leaves any trailing whitespace - including newlines - in place. Therefore cin.get() picks up the newline that was left after the user hits return to submit the second number.

You should not try to pause your programs artificially at the end. This point has been raised many times around here. Run the program properly: from an already-open console window or by using a batch file.

Share this post


Link to post
Share on other sites
The problem is that a '\n' character is still in the input buffer from your call to cin. When you call cin.get() the '\n' character is returned immediately and the program exits.

Try putting

cin.ignore(cin.rdbuf()->in_avail() + 1);
cin.get();


instead. This will ignore everything in the cin buffer and get a new char.

Hope that helps

Edit: Zahlman is right. You should not put a pause in your program (if you plan on disrtibuting it especially!)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Simian Man
The problem is that a '\n' character is still in the input buffer from your call to cin. When you call cin.get() the '\n' character is returned immediately and the program exits.

Try putting

cin.ignore(cin.rdbuf()->in_avail() + 1);
cin.get();

instead. This will ignore everything in the cin buffer and get a new char.

Hope that helps

Edit: Zahlman is right. You should not put a pause in your program (if you plan on disrtibuting it especially!)


Thanks guys [smile]

Share this post


Link to post
Share on other sites
Why doesn't anyone every just add a line asking to user to press a key to exit?


std::cout<<"Press a key to exit./n";
std::cin>>x;

then you don't have a concern about artifically exiting the program. The program will be design to keep the program open till the user has a chance to review the information.

You could even make it a function

void over()
{
int x;
std::cout<<"Press a key to exit./n";
std::cin>>x;
}

forgot to declare the int.

Share this post


Link to post
Share on other sites
It's \n, not /n.

As for "why", it doesn't make sense for all programs. Many programs are designed primarily to be run non-interactively.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this