How can I take in a name as string?

I am a real beginner and wanted to ask a simple question, how can I get this to only accept names under names and not numbers? and also I have a bug I cant fix when they enter a letter under age my code breaks.
#include <iostream>
#include <string>
using namespace std;

void main()
{
string name;
float hoursa, hoursb, weekly;
int daysa, daysb, age;

cout << "Enter your name: " << endl;
cin >> name;
cout << endl << "How old are you? " << endl;
cin >> age;
if( age < 130 && age > 0)
;
else
{
while (!(age < 130 && age > 0))
{
cout << "age is not valid, enter in a valid age: " << endl;
cin >> age;
}
}
cout << endl << "How many hours a day do you play on weekdays? " << endl;
cin >> hoursa;
if( hoursa <= 24 && hoursa >= 0)
;
else
{
while (!(hoursa <= 24 && hoursa >= 0))
{
cout << "hours is not valid, enter in a hours: " << endl;
cin >> hoursa;
}
}
cout << endl << "How many days a week do you play on the weekdays? " << endl;
cin >> daysa;
if( daysa < 6 && daysa >= 0)
;
else
{
while (daysa > 6 && daysa <= 0)
{
cout << "days is not valid, enter in a valid days: " << endl;
cin >> daysa;
}
}
cout << endl << "How many hours a day do you play during the weekend? " << endl;
cin >> hoursb;
if( hoursb <= 24 && hoursb >= 0)
;
else
{
while (!(hoursb <= 24 && hoursb >= 0))
{
cout << "hours is not valid, enter in a hours: " << endl;
cin >> hoursb;
}
}
cout << endl << "How many days a week do you play on the weekend? " << endl;
cin >> daysb;
if( daysb < 3 && daysb >= 0)
;
else
{
while (daysb > 3 && daysb <= 0)
{
cout << "days is not valid, enter in a valid days: " << endl;
cin >> daysb;
}
}

weekly=(hoursa*daysa)+(hoursb*daysb);

cout << endl << "Your name is " << name << " & you play for " << weekly << " hours a week " <<endl<<endl;

Please help...
Last edited on
Firstly, please use code tags when posting code. You can find them to the write of the message box - a little box that displays <>.

Secondly, void main() is not valid c++. Main() must always be of type int, and must always return an int.

Right, when getting input from users to put in string objects you need to use the function getline(). In your case: getline(cin, name);
http://www.cplusplus.com/reference/string/getline/

Next, whenever you use cin>> you should use cin.ignore() straight after it. When you use cin to get input the user has to press the enter key which leaves a newline character, or '\n', in the stream (which can make you program go a bit tits up if you have further input to get). What cin.ignore() does is ignore that character.

Your next section of code (if, else and while) is a bit wrong. There is no need for you to have the if or the else there, all that is needed is the while loop.

Try applying this advice to the rest of your program and post back if you need more help.

As far as your bug is concerned, it is because the input stream is expecting an integer and you are giving it a char. You need to write your code so that char's do not get accpeted.
Last edited on
Thank you.
Topic archived. No new replies allowed.