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.

2 Answers2

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);
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • 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::getline()' – Ikechi Anyanwu Aug 21 '12 at 05:41
  • There are two getline functions. [std::getline](http://www.cplusplus.com/reference/string/getline/) is in ; it produces a string object (which is probably what you want). [istream::getline](http://www.cplusplus.com/reference/iostream/istream/getline/) is in ; it reads data into a C string. I've clarified my answer. – nneonneo Aug 21 '12 at 05:46
  • How do i get it to look in instead of – Ikechi Anyanwu Aug 21 '12 at 05:51
  • My earlier answer recommended cin.getline, which reads into a C string. I have changed it to recommend the getline function in , which is what you probably want. Also added an example to make the usage clear. – nneonneo Aug 21 '12 at 05:55
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

Coding Mash
  • 3,338
  • 5
  • 24
  • 45