I need help...bad...

So for my assignment I have to calculate the score of a students two question long, multiple choice quiz. HOWEVER. If the student does not answer a question, I'm supposed to enter an F for them and if they answer and it is not A-F, I am supposed to give them an error message and close the program. The thing is, how are you supposed to NOT answer the question, you HAVE to cin an answer.

int main(){
char ans;
char ans2;
int score;
char grade;
string fName;
string lName;
cout << "Enter full name: ";
cin >> fName >> lName;
cout << "Enter question 1 answer: ";
cin >> ans;
ans = toupper(ans);

if(ans == 'A'){
score = 5;
}
else if(ans == 'B'){
score = 7;
}
else if(ans == 'C'){
score = 10;
}
else if(ans == 'D'){
score = 5;
}
else if(ans == 'E'){
score = 8;
}
else if(ans == 'F'){
score = 0;
}
else{
ans = 'F';
}
cout << "Enter answer to question 2: ";
cin >> ans2;
ans2 = toupper(ans2);

switch(ans2){
case 'A': score += 10;
break;
case 'B': score += 5;
break;
case 'C': score += 8;
break;
case 'D': score += 7;
break;
case 'E': score += 5;
break;
}
if(score >= 18){
grade = 'A';
}
else if(score >= 16){
grade = 'B';
}
else if(score >= 14){
grade = 'C';
}
else if(score >=10){
grade = 'D';
}
else if(score >=0){
grade = 'F';
}
cout << firstName << " " << lastName << " " << "Answers: " << answer1 << "," << answer2 << " " << "Score: " << score << " "<< "Grade: " << grade << endl;
system("pause");
return 0;
}
Here is my first thing that popped into my head.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main(){

	std::cout << "Enter A - F: ";
	
	char ch = std::cin.get();
	std::cin.sync();
	
	switch (ch){
	case '\n':
		std::cout << "Fail";
		break;
	case 'A':
		std::cout << "Pass";
		break;
	default:
		std::cout << "Default";
	}
	std::cout << std::endl;
	return 0;
}
Last edited on
OMG thank you. \n....I never considered that a thing.
Nevermind, that doesn't work at all.
Last edited on
Ok second thought....

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
#include <iostream>
#include <string>

int main(){

	std::string ans;
	std::cout << "Enter A: ";
	char ch = std::cin.get();
	if (ch == '\n')
		std::cout << "Fail\n";
	else{
		getline(std::cin, ans);

		if (ans.empty()){
			switch (ch){
			case 'A':
				std::cout << "Pass\n";
				break;
			default:
				std::cout << "Default\n";
			}
		}
		else{
			std::cerr << "Invalid Answer.\n";
			return 1;
		}
	}
	return 0;
}
1
2
3
4
5
6
7
8
else if(cin.get() == '\n')
{
     ans = 'F'
}
else
{
     //display error message
}
Last edited on
Topic archived. No new replies allowed.