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.
#include <iostream>
#include <string>
usingnamespace 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;
}
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>
usingnamespace bar;
usingnamespace foo;
int main()
{
doSomeThing(); // Error compiler is confused where there it's doSomeThing() from
// bar or foo
return 0;
}
@danwiffle using namespace std is considered a bad practice because of the scope it's within when used. If you are using usingnamespace 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.