0

Okay so I was just doing a little practice when I came to a problem that I've never had before.

#include <iostream>

using namespace std;

int main()
{
string empname = "";
int empage = 0;
char yes = 'y';

cout << "Please enter employee name:" << endl;
cin >> empname;
cin.get();
cout << "Your name is " + empname + " is this correct? (yes/no):" << endl;

if (yes)
{
    cout << "good" << endl;
}
else
{
    cout << "Please try again" << endl;
}

cout << "Please enter employee age:" << endl;
cin >> empage;
cin.get();
cout << "Your age is " + empname + " is this correct? (yes/no):" << endl;
if (yes)
{
    cout << "good" << endl;
}
else
{
    cout << "Please try again" << endl;
}
}

This executes as a console program, but after line 11 [include whitespace] (cout << "Please enter employee name:\t" << endl;), it just skips past everything, and says press ENTER to continue. What am I doing wrong.

6
  • If char yes = 'y';, then yes is always true. I assume this part is yet to be implemented. Commented Aug 21, 2012 at 5:21
  • It looks fine and works fine on gcc 4.6.3. Are you sure you are showing the code you are trying to run? Commented Aug 21, 2012 at 5:22
  • Can you try fflush(stdin) after cin >> empname; line. Commented Aug 21, 2012 at 5:24
  • @BharatSharma: fflush(stdin) is undefined behaviour. See: stackoverflow.com/questions/2979209/using-fflushstdin Commented Aug 21, 2012 at 5:27
  • @GregHewgill Thanks I think you have given the correct answer. Actually I have used it when I was in my college and It was working fine on that compiler. But i was knowing that we need to clear that input buffer. Commented Aug 21, 2012 at 5:32

2 Answers 2

7

I assume this 'press ENTER to continue' part is from your environment (batch script, editor, etc.) since it's not in your code.

The problem is that cin (istream in general) delimit input by all whitespace, not just newlines. So cin >> empname actually stores only the part of the employee name up to the first space, i.e. the first name. cin.get() gets only a single character, so it doesn't wait for a newline to appear.

You should use std::getline (in <string>) instead to get an entire line of input.

Example:

string empname = "";
cout << "Please enter employee name:" << endl;
getline(cin, empname);
Sign up to request clarification or add additional context in comments.

4 Comments

But is getline() apart of an external header file? Because when I compile i get an error:no matching function for call to 'std::basic_istream<char>::getline()'
There are two getline functions. std::getline is in <string>; it produces a string object (which is probably what you want). istream::getline is in <iostream>; it reads data into a C string. I've clarified my answer.
How do i get it to look in <string> instead of <char>
My earlier answer recommended cin.getline, which reads into a C string. I have changed it to recommend the getline function in <string>, which is what you probably want. Also added an example to make the usage clear.
0

You are working with cin and cin.get() at the same time. As rightly pointed out by nneoneo, better use getline here. Apart from that, let me tell you whenever you use formatted and un formatted input back to back, you get this sort of error (Remember cin is formatted and cin.get and getline are unformatted). It is because formatted input removes the space or endline character from the stream. That character remains in the stream and next time when you try an unformatted input, that character gets into the stream and into your variable.

To avoid getting this character getting into it, you can use cin.clear () or cin.ingore() between formatted and unformatted input. hope that helps

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.