954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ std::cin, how do i stop blank input

Well I am coding a small intermediate program to help me with learning c++ , and i am wondering how to check for blank input to cin >> string

heres what i have

string name;

cout << "Enter You're Name: ";
getline(cin, name);


and if i just hit enter without typing any name the program just stays blank

it is a console app btw

Razzo1
Newbie Poster
1 post since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

add some check to name.

if (name.empty()){
 cout <<"You entered nothing..."<<endl;
}
richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

loop through it and make sure at least one character isn't a space would also tell you.

Oh, and it's "Your" instead of "You're", you're = you are

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

Use a loop:

string name;

cout << "Enter You're Name: ";
while(name.empty())
  getline(cin, name);
mike_2000_17
Posting Virtuoso
Moderator
2,134 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: