I like making programs that are quizzes, so I've been learning the code for that type of thing (using booleans and loops so the user can repeat it until they get it correct)
I made a test program with just one question, just to test it out:
#include <iostream>
#include <Windows.h>
#include <string>
usingnamespace std;
string answer;
bool correct;
void question(){
cout<<"What year was Israel founded in?"<<endl;
cout<<"(A)1948, (B)1949, (C)1950, (D)1921"<<endl;
}
int main(){
do{
question();
getline(cin, answer);
if(answer == "a" || "A"){
correct = true;
}
elseif(answer != "A" || "a"){
correct = false;
cout<<"Sorry incorrect!"<<endl;
system("CLS");
Sleep(3000);
}
}
while(correct = false);
if(correct = true){
cout<<"Correct!!! Nice job!"<<endl;
Sleep(3000);
system("PAUSE");
}
}
My problem is, no matter what the user enters,(the user being me testing it), the program acts like they put in the correct answer (a) even if they didn't. Please help!
Please use code tags --> the <> button to the right of the cplusplus.com post edit box. As well as looking nice, it provides line numbers which make it easier to refer to posted code.
This just doesnt make sense, as to why it wont work. I tryed several versions with a char, a null terminated string and such. I think it may have to do with how strings are compared, I think.
"not (A and B)" is the same as "(not A) or (not B)"
"not (A or B)" is the same as "(not A) and (not B)"
Andy
PS Yours? You mean the one with ints?
Because this is daft logic:
As
bool correct;
is global, and global (file scope) variables are initialized to zero, which means false.
1 2 3 4 5 6 7 8 9
if (answer == 1)
{
correct = true;
}
if (answer != 2)
{
correct = false;
}
And this code only sets correct to true in the first if-test if answer is 1, but then it immediately set it back to false as 1 != 2. For 2 it just leaves the value alone, but it's already false. For all other cases it sets it (from false) to false.