English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

cout << "Name: " << d.name << " ID: " << d.id_num << " Salary: $" << d.salary << " Errors: ";
if(d.name.size()== 0)
cout << "Name ";

When the user enters a name and lets say they enter numbers, how do i make the above if statement work? I want the word "name" printed when they enter numbers into d.name...thanks

2007-01-29 15:37:46 · 3 answers · asked by COD 3 in Computers & Internet Programming & Design

3 answers

Yikes...you are first trying to print out d.name...then you check to see if it is empty ??

I think you want something like (my C++ is weak...):

if (d.name.size() <= 0 ) {
cout << "Name: ";
cin.getline(d.name, 50); // assuming d.name can hold 49 chars
}

cout << "Name: " << d.name << " ID: " << d.id_num << " Salary: $" << d.salary << " Errors: ";


Of course, I don't know what you plan to do about id_num and salary if name is empty...

2007-01-29 15:48:38 · answer #1 · answered by RGB_Mars 3 · 0 0

If you want "Name" to be printed out, you have to check for size NOT = 0
if(d.name.size() != 0)
cout << "Name ";

The way you have it now, you will never print "Name" if you enter something into d.name because the size will NEVER be zero.

2007-01-29 23:49:11 · answer #2 · answered by fiberangel 2 · 0 0

In C++, I'm not entirely sure, I usually use C# in Visual Studio. I think C# (It may be Python) has a built in method, isdigit(), which returns a boolean (true/false) value depending on if the variable is a digit. For example, in your case:

if(d.name.isdigit() == true)
cout<<"Name";

2007-01-29 23:49:58 · answer #3 · answered by ChickenMaster27 2 · 0 0

fedest.com, questions and answers