Name, Age, Classification Coding Problems

I have to design a program that checks the age, classification, and name of a person to decide whether or not they are old enough to vote. When I run my code, I am asked for my name as is desired, but if I actually enter letters, the code will skip the classification section and go straight to the age question. If I enter a number for the name, it goes through the program as desired. What am I missing?

Edit: I am going to modify my structure a little bit (just to make it more appealing) once I can get the code to run properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
using namespace std;

int main( )


{

	string name;
	string classification;
	int age;

//ask their name

	cout << "Please enter your name (First and Last):" <<endl;
	cin >> name;

//ask for their student classification

	cout << "Please enter your classification\n";
	cout << "Choose from the following: Freshman, Sophomore, Junior, Senior:" << endl;	
	cin >> classification;

//ask for their age

	cout << "Please enter your age:" <<endl;
	cin >> age;

		if (age > 17)
	
			{
				cout << "You can Vote!";
			} else
			{
				cout << "You can not Vote." << endl;
			}




return 0;

}
Last edited on
You should std::getline() to extract string with white spaces.Your code should be like this
1
2
std::cout << "Please enter your name (First and Last):" <<std::endl;
std::getline(std::cin,name);
.One more thing,using namespace is considered bad practice.
Last edited on
why is using namespace considered bad practice?
let say you use two libraries called foo and bar and both of them has same function name called doSomeThing().
1
2
3
4
5
6
7
8
9
10
11
12
#include <foo>
#include <bar>

using namespace bar;
using namespace foo;

int main()
{
    doSomeThing(); // Error compiler is confused where there it's doSomeThing() from
                   // bar or foo
    return 0;
}

Last edited on
@danwiffle using namespace std is considered a bad practice because of the scope it's within when used. If you are using using namespace std the program will automatically assume that when something isn't defined, that it belongs to std. This works for small programs. But in reality, when coding bigger projects you will not be allowed to be using namespace like that. Because it can really mix up other scopes. Thus creating problems for yourself and maybe even your coworkers.
Topic archived. No new replies allowed.