Quiz Type Application Not Working

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:

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
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <Windows.h>
#include <string>
using namespace 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;
			}
		else
			if(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!
Last edited on
Dont use system("pause")

Looking at your code, give me a minute.
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.

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main() {
    cout << "Hello nicely formatted world!\n";

    return 0;
}


And firedraco's article: "How to use tags"
http://www.cplusplus.com/articles/z13hAqkS/

You can even go back and add tags to your earlier posts!

Andy
Last edited on
Ok I am just confused at this point as to why it isn't working lol
Last edited on
i added the code tags can u help me now?

1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main(){

//please help me now
}
Last edited on
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.
Even this wont work:

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
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
using namespace std;

int answer;
bool correct;

int main()
{
	cout<<"What year was Israel founded in?"<<endl;
	cout<<"(A)1948, (B)1949, (C)1950, (D)1921"<<endl;

	cin>>answer;

	if (answer == 1)
	{
		correct = true;
	}

	if (answer != 2)
	{
		correct = false;
	}


	if (correct == true)
	{
		cout<<"Congratulations"<<endl;
	}

	if (correct == false)
	{
		cout<<"You fail"<<endl;
	}

return 0;
}
Last edited on
:-) much better!

And I can now say that line 20 should be

if(answer == "a" || answer == "A"){

You see, C++ isn't that smart, so thinks this

if(answer == "a" || "A"){

is really this

if( (answer == "a") || "A" ){

Where "A" is as a char* pointer which looks like an integer to if()

And any non-zero integer is treated as true, so "A" -> <a non-zero memory address> -> true. So this is the same as

if( (answer == "a") || true){

And anything <or> true is true!

Same issue on line 24.

And line 31 should just be (== not =)

while(correct == false);

And same for line 33.

Andy

PS You don't need to test explicitly against true, so some (lazy) people (like me) would just use

if( correct ) // to test if true

and

if( !correct ) // to test if false

Or maybe you already knew and just like having the true or false?
Last edited on
Oh yea true thanks andywestken xD
But also why wont mine work? o.O
Oops - one last thing, line 24 should use && not ||

if(answer != "A" && answer != "a"){

De Morgan's laws
http://en.wikipedia.org/wiki/De_Morgan%27s_laws

"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.

You should experiment a bit!
Woops, sorry thanks lol
Topic archived. No new replies allowed.