How can I make a program not to view next contents of the program if the user as not enter his/her name? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How can I make a program not to view next contents of the program if the user as not enter his/her name?

Have made a simple program in C++.. That request a user to enter his name. I want the program not to go to the step or to view next contents of the the program regardless the user not entering his/her name..

23rd Apr 2019, 10:48 AM
Coder learner
Coder learner - avatar
7 Answers
+ 12
Hey there, Anna and kiRA are correct. First, you need to get the user's name getline(cin, name); Then, you need to compare the name to an empty string. if (name != "") { cout << "Welcome, " << name << "!"; } That's it! You don't need an else statement because it will just be empty.
6th May 2019, 10:29 AM
Edwin Pratt
Edwin Pratt - avatar
+ 8
Coder C++ I would recommend that you use the following if statement: if (name != "" && name != " " && name != "1" && name != "2" && name != "3" && name != "4" && name != "5" && name != "6" && name != "7" && name != "8" && name != "9" && name != "0") { cout << "Welcome," << name << "!"; } -- I know, it is not the best, but once you are completely familiar to functions, you can put that whole block of code in its own function.
6th May 2019, 9:36 PM
Edwin Pratt
Edwin Pratt - avatar
+ 2
string name{}; cout<<"Register you name: "; do { getline(cin,name); } while(name == "");
23rd Apr 2019, 11:46 AM
Anna
Anna - avatar
+ 1
use loop and *if-else statement for checking whether string is empty or not*
23rd Apr 2019, 11:53 AM
kiRA
kiRA - avatar
+ 1
Edwin Pratt the program can still run whenever a user enters anything though an integer
6th May 2019, 9:29 PM
Coder learner
Coder learner - avatar
23rd Apr 2019, 10:57 AM
Coder learner
Coder learner - avatar
0
Anna have tried that.. Still not getting it right... Help out
6th May 2019, 7:04 AM
Coder learner
Coder learner - avatar