#include<iostream>
#include<string>
usingnamespace std;
int main()
{
string char1, char2, char3, char4, char5, char6;
cout<<"Hello this is your standard True and False quiz"<<endl;
cout<<"Please enter 'True' or 'False'"<<endl;
cout<<"No#1 George Washington invented the toilet?"<<endl;
cin>>char1;
while (char1 != "True" || "False")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char1;
}
system("PAUSE");
return 0;
}
But when I input the True or False it keeps displaying an incorrect answer message
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
string char1, char2, char3, char4, char5, char6;
cout<<"Hello this is your standard True and False quiz"<<endl;
cout<<"Please enter 'T' or 'F'"<<endl;
cout<<"No#1 George Washington invented the toilet?"<<endl;
cin>>char1;
if ( char1 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char1;
};
if ( char1 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char1;
};
if ( char1 == "T" )
{
cout<<"You entered the incorrect answer the answer is False"<<endl;
};
cout<<"No#2 The Squareroot of 3136 is 56?"<<endl;
system("PAUSE");
return 0;
}
My problem is that when i give it the correct answer the first time it wont cut straight to the other part of the code it will display you entered an incorrect character stuff until the two times is over.
#include <iostream>
usingnamespace std;
int main()
{
char q1;
char q2;
cout << "Hello, this is a standard TRUE or FALSE quiz.\n\n";
cout << "#1. George Washington invented the toilet.\n";
cout << "Please enter ""T"" or ""F""\n";
cin >> q1;
if ((q1 == 'T') || (q1 == 't'))
{
cout << "You entered the INCORRECT answer.\n\n";
}
elseif ((q1 == 'F') || (q1 == 'f'))
{
cout << "You entered the CORRECT answer!\n\n";
}
else
{
cout << "ERROR: Invalid input.\n\n";
}
cout << "#2. The square root of 3,136 is 56.\n";
cout << "Please enter ""T"" or ""F""\n";
cin >> q2;
// ... finish code
system("PAUSE");
return 0;
}
Since it is a true or false quiz, you don't have to worry about catching separate input for:
TRUE, T, t, FALSE, F, and f.
We are only worried about whether it is True, False, or Invalid.
Use a char instead of a string, this way when the user enters "TRUE" or "FALSE", it is held as "T" or "F"
The char data type can only hold a single letter, meaning it will only read the first letter of
the word "TRUE" or "FALSE" - input as: T or F (or given the invalid input error)
First off: Here is just an example starting on line 14 of your code ...
1 2 3 4 5
if ( char1 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char1;
};
If I read your problem correctly, you are getting a message saying that "You entered the incorrect character please reenter True or False" no matter what you enter for each question.
If this is the case, the reason is because the condition (char1 != "T" || "F") in your if statements actually return true every time. The reason for this is that while in english the statement reads, "If char1 is not equal to T or F do the following ... ", in c++ it actually reads like this: "If char1 is not equal to "T" OR IF "F""
This means that in an ||(or) statement, c++ will evaluate both sides of a || and return true if either side is true. Here is the equivalent of your code and maybe it will give you a better understanding:
(char1!="T") || ("F")
Here is what your code should look like to implement what your trying to do:
1 2 3 4 5
if ( char1 != "T" && char1 != "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char1;
};
This evaluates to, "if char1 does not equal T and char1 does not equal F ..."
Also, another note that I think you would really like to incorporate into your program to make it possible to add questions in the future without needing to do another hundred lines of code:
Instead of using an if statement for every possible choice, try implementing this little bit of pseudocode:
int correct = 0;
int incorrect = 0;
string char1, char2, char3, char4, char5, char6;
//Question 1
//Get char1
if(answer is correct)
correct++; //correct = correct + 1;
else //answer incorrect
incorrect++; //incorrect = incorrect + 1;
//Question 2
//Get char2
if(answer is correct)
correct++; //correct = correct + 1;
else //answer incorrect
incorrect++; //incorrect = incorrect + 1;
//Question 3
//etc ...
//Let the user know how he did on the quiz
cout<<"You answered "<<correct<<" correct and "<<incorrect<<"incorrect."<<endl;
Also, one last note. If you are interested in input validation (Making sure the person typed in T or F), read up on how do:whiles work. It makes doing input validation very easy.
Let us know how that works out, any other questions by what I've said just ask.