Why isn't this program running?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main()
{
	int quiz_1=10,quiz_2=15;
	char choice='\0',q1='\0',q2='\0';
	
	cout<<"Show marks of Quiz-1 or Quiz-2?"<<endl;
	cin>>choice;
	
	if(choice == q1)
	{
		cout<<quiz_1;
	}
	else
	{
		if(choice == q2)
		{
			cout<<quiz_2;
		}
	}
	return 0;
}
Last edited on
Because both q1 and q2 are set to the same value, neither of which is a character which can be entered at the keyboard.
if(choice == q1) // if choice == '\0' if(choice == q2) // if choice == '\0'
you are comparing user choice with null character, this is never true. and quiz never runs.

anyway, why do you initialize q1 and q2 with '\0' ?
Aren't we supposed to initialize char with '\0'?
Okay now i wont initialize q1 and q2 with '\0'. Chervil and codekiddy what do you suggest that I should do with this? How can I fix this?
q1='a',q2='b';

now the user either enters letter a or letter b.
I would start here:
 
cout<<"Show marks of Quiz-1 or Quiz-2?"<<endl;

What is the user expected to type in response to the prompt? If the user types "Quiz-1", the next line cin>>choice; will get the letter 'Q' as the value of choice. So, make it clear that only a single character should be typed, and specify what the possible choices are.

 
cout<<"Show marks of Quiz-1 or Quiz-2? Please enter 1 or 2:"<<endl;


Now that is clear, it should also be clear what values should be assigned to q1 and q2.

Topic archived. No new replies allowed.