Help with using true or false "if and else" statements.

Jan 13, 2015 at 9:19pm
Hi there, essentially what I'm trying to do is set it up where I ask a true or false question and if the answer isn't true then it will continue to ask another question. My problem is that when I run the code the answer that should come up with yes comes up regardless of what the user inputs. My code is incomplete but if I can figure out how to do this section then I can replicate it for the rest. Thanks in advance. My code thus far is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main(){
bool a
cout <<"True or false, was it detected in the tracking chamber and deposited in the E/M calorimeter?";
cin >> a;

if (a=True)
{
cout <<"electron";
}
else
{
cout <<"True or false, was it undetected in the tracking chamber and deposited in the E/M calorimeter?";
}
return 0;
}
Jan 13, 2015 at 9:49pm
My problem is that when I run the code the answer that should come up with yes comes up regardless of what the user inputs.

Because of if (a=True). Use equality. Not assignment.
Jan 13, 2015 at 9:51pm
First of all line 5 needs a ; after the a.

Second your declaration of a needs to be string because that is what you are asking from the user not of type bool

Third if line 5 reads string a; then you need to #include <string> at line 2

Fourth line 9 needs to read if (a=="True") because it is now of type string

Fifth you will need to add system("Pause") to lines 12 and 16 to see what your console is outputting

Last edited on Jan 13, 2015 at 9:55pm
Topic archived. No new replies allowed.