Frustrated Dad needs help with Sons project

My son is trying to write a simple multiple choice program for a school project, it should output "You are correct" if correct answer is given or output "You are incorrect" if incorrect answer is given. As it stands now any input is shown as a correct answer, can anyone please point us in the right direction?

// Dies Drear2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "string"
#include"windows.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char q1;
cout<<"Welcome to the House of Dies Drear computer quiz!\n";
cout<<"Question 1: Who did Thomas learn was in his dream?\n";
cout<<"A. Pluto\n";
cout<<"B. Mr.Small\n";
cout<<"C. Mrs.Small\n";
cout<<"D. Mayhew\n";
cin>>q1;
if (q1="a" || "A")
{
cout<<"You are correct!\n";
Sleep(2000);
}
else
{
cout<<"You are incorrect!\n";
Sleep(2000);
}
Please use code tags next time you post your code.

1
2
3
4
5
if (q1="a" || "A")
{ 
cout<<"You are correct!\n";
Sleep(2000);
}

The condition for the if statement is incorrect. Your son should be using the equal-to operator (==) to compare the string to the string literal. You should also use the greater than and less than signs around the headers you wish to include. Here's the correct code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"
#include <iostream> 
#include <string>
#include <windows.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {
      char q1;
      cout<<"Welcome to the House of Dies Drear computer quiz!\n";
      cout<<"Question 1: Who did Thomas learn was in his dream?\n";
      cout<<"A. Pluto\n";
      cout<<"B. Mr.Small\n";
      cout<<"C. Mrs.Small\n";
      cout<<"D. Mayhew\n";
      cin>>q1;
      if (q1 == "a" || q1 == "A") { 
            cout<<"You are correct!\n";
            Sleep(2000);
      }
      else {
            cout<<"You are incorrect!\n";
            Sleep(2000);
      }
      return 0;
}
Last edited on
if (q1="a" || "A")

is equivalent to:

1
2
q1 = ("a" || "A") ;
if ( q1 )


Note that = is assignment. == is comparison. q1 is of type char. String literals are of type pointer-to-char. It would make more sense to compare to a char than to an array of them.

If you were to write it:
if (q1 == 'a' || 'A')

that would be equivalent to:
if ( (q1 == 'a') || 'A' )
which would always evaluate to true since 'A' is non-zero.

if ( q1 == 'a' || q1 == 'A' )
is what you want.
First off thanks for the help, that got us moving in the right direction, first question works perfect now but he is trying to do a total of 10 questions and right now the program exits after the first question (correct or not) and never gets to the second one, help is appreciated thanks.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Dies Drear2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream> 
#include <string>
#include <windows.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {
      char q1;
	  char q2;
      cout<<"Welcome to the House of Dies Drear computer quiz!\n";
      cout<<"Question 1: Who did Thomas learn was in his dream?\n";
      cout<<"A. Pluto\n";
      cout<<"B. Mr.Small\n";
      cout<<"C. Mrs.Small\n";
      cout<<"D. Mayhew\n";
      cin>>q1;
      if ( q1 == 'a' || q1 == 'A' )
	  { 
           cout<<"You are correct!\n";
		   Sleep(2000);
	  }
	  else
	  {
		  cout<<"You are incorrect!\n";
	  Sleep(2000);
	  exit(1);
	  }
		   cout<<"Question 2: Where is the House of Dies Drear located?\n";
cout<<"A. Pennsylvania\n";
cout<<"B. Oregon\n";
cout<<"C. Ohio\n";
cout<<"D. North Carolina\n";
cin>>q2;
if ( q2 == 'a' || == 'A')
{
	cout<<"You are correct!\n";
	Sleep(2000);
}

      }
      else {
            cout<<"You are incorrect!\n";
			Sleep(2000);
			exit(1)
}
      }






      return 0;
}
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
40
41
42
43
44
#include <iostream> 
#include <string>
#include <windows.h>
using namespace std;
int main(void) {
      char q1;
      char q2;
      cout<<"Welcome to the House of Dies Drear computer quiz!\n";
      cout<<"Question 1: Who did Thomas learn was in his dream?\n";
      cout<<"A. Pluto\n";
      cout<<"B. Mr.Small\n";
      cout<<"C. Mrs.Small\n";
      cout<<"D. Mayhew\n";
      cin>>q1;
      if ( q1 == 'a' || q1 == 'A' )
      { 
           cout<<"You are correct!\n";
           Sleep(2000);
      }
      else
      {
          cout<<"You are incorrect!\n";
      Sleep(2000);
      exit(1);
      }
    cout<<"Question 2: Where is the House of Dies Drear located?\n";
    cout<<"A. Pennsylvania\n";
    cout<<"B. Oregon\n";
    cout<<"C. Ohio\n";
    cout<<"D. North Carolina\n";
    cin>>q2;
    if ( q2 == 'a' || q2 == 'A')
    {
        cout<<"You are correct!\n";
        Sleep(2000);
    }

    else {
        cout<<"You are incorrect!\n";
        Sleep(2000);
        exit(1);
    }
      return 0;
}


There, I fixed your code. Your son was missing a few brackets and semi-colons. There shouldn't be a problem now.
Good formatting will help you. Any time you enter a "{}" block, indent a tab. I also like to put the starting "{" on a new line. For example, you have:
int main(void) {
I would do:
1
2
int main(void)
{


It is more clear to me what is happening when I do that. Although, not on every text editor...

Here is your code with the formatting I describe (though I've replaced all tabs with two spaces because tabs don't do well in copy/paste)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
// Dies Drear2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream> 
#include <string>
#include <windows.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
  char q1;
  char q2;
  cout<<"Welcome to the House of Dies Drear computer quiz!\n";
  cout<<"Question 1: Who did Thomas learn was in his dream?\n";
  cout<<"A. Pluto\n";
  cout<<"B. Mr.Small\n";
  cout<<"C. Mrs.Small\n";
  cout<<"D. Mayhew\n";
  cin>>q1;
  if ( q1 == 'a' || q1 == 'A' )
  { 
    cout<<"You are correct!\n";
    Sleep(2000);
  }
  else
  {
    cout<<"You are incorrect!\n";
    Sleep(2000);
    exit(1);
  }
  cout<<"Question 2: Where is the House of Dies Drear located?\n";
  cout<<"A. Pennsylvania\n";
  cout<<"B. Oregon\n";
  cout<<"C. Ohio\n";
  cout<<"D. North Carolina\n";
  cin>>q2;
  if ( q2 == 'a' || == 'A')
  {
    cout<<"You are correct!\n";
    Sleep(2000);
  }

}
else
{
  cout<<"You are incorrect!\n";
  Sleep(2000);
  exit(1)
}
}
return 0;
}


You can see something is going wrong near the end where you have enough braces to end main().
Greatly appreciate the help, this one still just exits after the first question regardless of the answer:

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
40
41
42
43
44
#include <iostream> 
#include <string>
#include <windows.h>
using namespace std;
int main(void) {
      char q1;
      char q2;
      cout<<"Welcome to the House of Dies Drear computer quiz!\n";
      cout<<"Question 1: Who did Thomas learn was in his dream?\n";
      cout<<"A. Pluto\n";
      cout<<"B. Mr.Small\n";
      cout<<"C. Mrs.Small\n";
      cout<<"D. Mayhew\n";
      cin>>q1;
      if ( q1 == 'a' || q1 == 'A' )
      { 
           cout<<"You are correct!\n";
           Sleep(2000);
      }
      else
      {
          cout<<"You are incorrect!\n";
      Sleep(2000);
      exit(1);
      }
    cout<<"Question 2: Where is the House of Dies Drear located?\n";
    cout<<"A. Pennsylvania\n";
    cout<<"B. Oregon\n";
    cout<<"C. Ohio\n";
    cout<<"D. North Carolina\n";
    cin>>q2;
    if ( q2 == 'a' || q2 == 'A')
    {
        cout<<"You are correct!\n";
        Sleep(2000);
    }

    else {
        cout<<"You are incorrect!\n";
        Sleep(2000);
        exit(1);
    }
      return 0;
}


Here is the debug output:

'Dies Drear2.exe': Loaded 'C:\Users\Michael\Documents\Visual Studio 2010\Projects\Dies Drear2\Debug\Dies Drear2.exe', Symbols loaded.
'Dies Drear2.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'Dies Drear2.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'Dies Drear2.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'Dies Drear2.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
'Dies Drear2.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
The program '[9108] Dies Drear2.exe: Native' has exited with code 0 (0x0)
That would be expected :)

Note the entry point for main (line 5 on what you just posted, and line 9 of what I posted). You are running a windows console program, which expects _tmain(), xhtmlx gave you a general console program which simply uses main()

There's nothing really "wrong" with either way. It has to do with the way you have made your project. When you made your console program, you could have started with an empty project and simply added the main.cpp. Then you could use main()
Last edited on
So how do we get it to move on to the second (and rest of) questions?
I know it's not relataed, but it would be faster to say something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int questionnum;
char answer;

//questionnum is increased after every question

//Question 1 Code here:
goto ifstatesforquestions //After answer is typed in it goes to a section of code that handles answers


ifstatesforquestions:


if ((question == 1 && answer = 'a') || //All questions and answers)
//^ I actually don't know if you can put parentheses in if statements or not, but I think there's a way to separate statements



Maybe this will make it easier to program. Hope you interpreted this well.
So basically after question 1 is answered, correct answer should move on to question 2 and incorrect answer should exit the program, right now program is exiting regardless of answer to question 1.
I suspect you replaced _tmain with main and now the code isn't actually being compiled successfully and you're using the previously compiled/linked executable. Be careful what you're saying yes to when you're clicking dialogs.
^^ Also why you shouldn't copy/paste other people's code.

Just replace main with _tmain.
Last edited on
btw sir, you can learn from here:

http://cplusplus.com/doc/tutorial/

or you can download the tutorial:

http://cplusplus.com/files/tutorial.pdf
Topic archived. No new replies allowed.