Need help debugging a program of mine

I made a program that was supposed to ask the user to answer 5 questions and then suggest a class for them to play in a game. When I try compiling it in Terminal, it spits out several errors that don't even have to do with this document. What am I doing wrong?

Also, I know that the "goto" command isn't supposed to be used, but it seemed to me the best way to get my program to function the way it's supposed to.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

int main() 
{

string letsgo,Q1 = "A",Q2 = "A",Q3 = "A",Q4 = "A",Q5 = "A";
int mediccount,magecount,warriorcount,gunnercount,int_array;

cout << "Input the letter that corresponds to your selected answer then hit enter. The best way to get an accurate answer is to answer these questions truthfully, even though an obvious pattern may emerge. Continue? (y/n)";
cin >> letsgo;
if (letsgo=("y"||"Y"))
{

question1:
cout << "1. At the beach, you see a large island in the middle of the water. What do you do?\n" << "A) I wonder if there are any survivors that need helping.\n" << "B) Let's blow it up. WITH FIRE.\n" << "C) I claim this land in the name of the king. Me.\n" << "D) Use the coconuts on the trees as target practice.\n";
cin >> Q1;
if (Q1=!("A"||"a"||"B"||"b"||"C"||"c"||"D"||"d"))
{
cout << "Please enter a valid character.\n";
goto question1;
}

question2:
cout << "2. You find yourself in a forest.\n" << "A) I know the closest exit, water source, and predator, down to the closest centimeter.\n" << "B) Let's burn it down. WITH FIRE.\n" << "C) I smell the nearest predator and my pheromones tell him he's going to be my next meal." << "D) Spot the mouse in the next three fields over.\n";
cin >> Q2;
if (Q1=!("A"||"a"||"B"||"b"||"C"||"c"||"D"||"d"))
{
cout << "Please enter a valid character.\n";
goto question2;
}

question3:
cout << "3. When someone yells "MEDIC!", you are:\n" << "A) That medic.\n" << "B) Who needs medics?\n" << "C) The guy who caused someone to call for the medic.\n" << "D) Trying to snipe the medic at this moment.\n";
cin >> Q3;
if (Q1=!("A"||"a"||"B"||"b"||"C"||"c"||"D"||"d"))
{
cout << "Please enter a valid character.\n";
goto question3;
}

question4:
cout << "4. You're making sausages.\n" << "A) What the hell am I doing here?\n" << "B) Burn, baby, burn.\n" << "C) Twist, pull, twist, pull, twist, pull…\n" << "D) Let's make them as large as possible so they'll fit properly in my potato cannon.\n";
cin >> Q4;
if (Q1=!("A"||"a"||"B"||"b"||"C"||"c"||"D"||"d"))
{
cout << "Please enter a valid character.\n";
goto question4;
}

question5:
cout << "5. A wizard has taken over your town.\n" << "A) Assist the revolutionaries.\n" << "B) I am that wizard." << "C) Lead the revolutionaries.\n" << "D) Oh, you mean that wizard I shot yesterday?";
cin >> Q5;
if (Q1=!("A"||"a"||"B"||"b"||"C"||"c"||"D"||"d"))
{
cout << "Please enter a valid character.\n";
goto question5;
}

int_array[] = {Q1,Q2,Q3,Q4,Q5,}
mediccount = (int) count (int_array, int_array+5, ("a"||"A"));
magecount = (int) count (int_array, int_array+5, ("b"||"B"));
warriorcount = (int) count (int_array, int_array+5, ("c"||"C"));
gunnercount = (int) count (int_array, int_array+5, ("d"||"D"));

if (mediccount > (magecount||warriorcount||gunnercount))
{
cout << "You are most like the medic.";
}
else if (magecount > (mediccount||warriorcount||gunnercount))
{
cout << "You are most like the mage.";
}
else if (warriorcount > (mediccount||magecount||gunnercount))
{
cout << "You are most like the warrior.";
}
else if (gunnercount > (mediccount||magecount||warriorcount))
{
cout << "You are most like the gunner.";
}
else if (mediccount == magecount)
{
cout << "You have traits of both the medic and the mage.";
}
else if (mediccount == warriorcount)
{
cout << "You have traits of both the medic and the warrior.";
}
else if (mediccount == gunnercount)
{
cout << "You have traits of both the medic and the gunner.";
}
else if (magecount == warriorcount)
{
cout << "You have traits of both the mage and the warrior.";
}
else if (magecount == gunnercount)
{
cout << "You have traits of both the mage and the gunner.";
}
else if (warriorcount == gunnercount)
{
cout << "You have traits of both the warrior and the gunner.";
}

return 0;
}
1. The inequality operator is !=, not =!.
2. If you try to do (x # (a || b || c || (...))), where # is any relational operator (==, !=, >, <, >=, <=), the result you'll get (if the expression even compiles) will be very different from what you expect. You have to write each comparison separately and then combine them using boolean operators. For example, on line 16 you have letsgo=("y"||"Y"). The correct form is letsgo=="y" || letsgo=="Y".
3. Some conditions are incorrect even by your syntax. For example, lines 22, 31, 40, 49, and 58.
4. int_array was declared on line 12 as a single int. There's three problems with your usage on line 64. One, you're trying to assign an array to a scalar. Two, you can't use the array syntax outside of the array declaration. For example,
1
2
3
int array[]={1,2,3}; //OK
int array2[3]; //OK
array2={1,2,3}; //Wrong. After the array's been declared, you can only assign individual elements. 
Three, you're trying to assign an array of strings to an array of integers.
5. I'm not sure what you're trying to say here: count (int_array, int_array+5, ("a"||"A"))
Last edited on
Thanks for the reply.

So line 22 should be if (Q1!="a" || Q1!="A") and so on?

In lines 65-68, I was trying to ask the computer to count the number of times a certain letter came up in an array comprised by all the user's answers to the questions.
So line 22 should be if (Q1!="a" || Q1!="A") and so on?
Consider carefully what would happen if you use that condition. If it makes things easier, think about it like this: which integers match the condition (x!=10 || x!=20)? Remember that (a || b) means "a, b, or both a and b are true".

In lines 65-68, I was trying to ask the computer to count the number of times a certain letter came up in an array comprised by all the user's answers to the questions.
In general, || evaluates to a boolean value. Knowing this, can you guess why that line will not work?
Additionally, how would std::count() know that you want the elements to be equal to one of the things and not, say, lower than one of them?
Oh, haha. Standard rookie mistake. Would if (Q1!="a")|| if (Q1!="A") be correct?

As for the count function, I've never used it before; I pretty much copied the format outlined in the description of it as best I could. I have no inkling of why or how it works.
Would if (Q1!="a")|| if (Q1!="A") be correct?
No. The syntax in your previous suggestion was correct. The problem is the boolean logic.

It's not advisable to use something if you don't understand what it does. Count the elements yourself.
I was hoping I'd avert the problem by changing the syntax. So if I have a number, say, 30. From my understanding, the line if (Q1!=10 || Q1!=20) would return a 1 on both functions. So the conditional statement would be true and it would proceed with the code inside the brackets, right?

How would I count the elements myself?
So if I have a number, say, 30. From my understanding, the line if (Q1!=10 || Q1!=20) would return a 1 on both functions. So the conditional statement would be true and it would proceed with the code inside the brackets, right?
And if it's 10? And if it's 20? Pi? The square root of e? Do you see where I'm going?

How would I count the elements myself?
Come on, now. There's a limit to how low level my help will go. You need to at least be able to figure that out.
Oh, okay. So it would actually include 10 because it satisfies the Q1!=20. Would it be easier (or even possible) to set up an array so I end up with something like if (Q1!=[10,20])

Alright, fine, I guess I'll have to brood on how to count the elements for a while.
That syntax is not possible. There's a way to do it with arrays, but it's overkill in this case. Look up boolean operators. I can't believe your material doesn't cover this.
Well, apparently my book is a beginner's guide to C++. But in either case, I'm just doing my best to suggest any possible answers to my problem. My most recent one is setting it to if (Q1=="a" || Q1=="A") and restructuring my code based on that.

As to counting, I'm thinking I'll have to use something like:
1
2
3
4
if (Q1 == "a" || Q1 == "A")
{
mediccount = mediccount + 1
}
Last edited on
See section "Logical Operators": http://www.cplusplus.com/doc/tutorial/operators/
So that would be if !(Q1=="a" || Q1=="A")?
The condition always has to be wrapped in parentheses, but other than that, yes, that would work.

See also: http://en.wikipedia.org/wiki/De_Morgan_law
In summary: (!a || !b) == !(a && b) ; (!a && !b) == !(a || b)
Okay, I fixed that problem. No errors from that section, it looks like.

In line 38, I want to know how I would go about including quotation marks in the string. I understand why it doesn't work (because the first quotation mark before the MEDIC ends the one that begins the string).

Secondly, is there a less labor-intensive way to perform the counting operation than putting a group of four "if" statements after every question?
"\""

Secondly, is there a less labor-intensive way to perform the counting operation than putting a group of four "if" statements after every question?
Several. The problem is that the way you're approaching this will naturally lead to more verbose code.
Read about functions to see how to minimize repetitive code.
Last edited on
I finally got around to applying what you helped me with here. It outputs 2 errors:

Classchoosert2.cpp:28: error: a function-definition is not allowed here before ‘{’ token
Classchoosert2.cpp:131: error: expected `}' at end of input

The second error makes sense to me, but I tried adding another "}" at the end, but it outputs the same error. Also, could you give me a little bit more on where/how to use the "\""?

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

int mediccount = 0,magecount = 0,warriorcount = 0,gunnercount = 0;

char counter (char a)
{
	switch (a)
	{
		case 'a':
		case 'A': mediccount = mediccount + 1;
			break;
		case 'b':
		case 'B': magecount = magecount + 1;
			break;
		case 'c':
		case 'C': warriorcount = warriorcount + 1;
			break;
		case 'd':
		case 'D': gunnercount = gunnercount + 1;
	}

int main() 
{

char letsgo,Q1 = 'A',Q2 = 'A',Q3 = 'A',Q4 = 'A',Q5 = 'A';
int int_array;

cout << "Input the letter that corresponds to your selected answer then hit enter. The best way to get an accurate answer is to answer these questions truthfully, even though an obvious pattern may emerge. Continue? (y/n)";
cin >> letsgo;
if (letsgo=='y' || letsgo=='Y')
{

question1:
cout << "1. At the beach, you see a large island in the middle of the water. What do you do?\n" << "A) I wonder if there are any survivors that need helping.\n" << "B) Let's blow it up. WITH FIRE.\n" << "C) I claim this land in the name of the king. Me.\n" << "D) Use the coconuts on the trees as target practice.\n";
cin >> Q1;
if (!(Q1=='a'|| Q1=='A'|| Q1=='b'|| Q1=='B'|| Q1=='c'|| Q1=='C'|| Q1=='d'|| Q1=='D'))
{
cout << "Please enter a valid character.\n";
goto question1;
}
	
question2:
cout << "2. You find yourself in a forest.\n" << "A) I know the closest exit, water source, and predator, down to the closest centimeter.\n" << "B) Let's burn it down. WITH FIRE.\n" << "C) I smell the nearest predator and my pheromones tell him he's going to be my next meal." << "D) Spot the mouse in the next three fields over.\n";
cin >> Q2;
if (!(Q1=='a'|| Q1=='A'|| Q1=='b'|| Q1=='B'|| Q1=='c'|| Q1=='C'|| Q1=='d'|| Q1=='D'))
{
cout << "Please enter a valid character.\n";
goto question2;
}

question3:
cout << "3. When someone yells "MEDIC!", you are:\n" << "A) That medic.\n" << "B) Who needs medics?\n" << "C) The guy who caused someone to call for the medic.\n" << "D) Trying to snipe the medic at this moment.\n";
cin >> Q3;
if (!(Q1=='a'|| Q1=='A'|| Q1=='b'|| Q1=='B'|| Q1=='c'|| Q1=='C'|| Q1=='d'|| Q1=='D'))
{
cout << "Please enter a valid character.\n";
goto question3;
}

question4:
cout << "4. You're making sausages.\n" << "A) What the hell am I doing here?\n" << "B) Burn, baby, burn.\n" << "C) Twist, pull, twist, pull, twist, pull…\n" << "D) Let's make them as large as possible so they'll fit properly in my potato cannon.\n";
cin >> Q4;
if (!(Q1=='a'|| Q1=='A'|| Q1=='b'|| Q1=='B'|| Q1=='c'|| Q1=='C'|| Q1=='d'|| Q1=='D'))
{
cout << "Please enter a valid character.\n";
goto question4;
}

question5:
cout << "5. A wizard has taken over your town.\n" << "A) Assist the revolutionaries.\n" << "B) I am that wizard." << "C) Lead the revolutionaries.\n" << "D) Oh, you mean that wizard I shot yesterday?";
cin >> Q5;
if (!(Q1=='a'|| Q1=='A'|| Q1=='b'|| Q1=='B'|| Q1=='c'|| Q1=='C'|| Q1=='d'|| Q1=='D'))
{
cout << "Please enter a valid character.\n";
goto question5;
}
	
	counter (Q1);
	counter (Q2);
	counter (Q3);
	counter (Q4);
	counter (Q5);

if (mediccount > (magecount||warriorcount||gunnercount))
{
cout << "You are most like the medic.";
}
else if (magecount > (mediccount||warriorcount||gunnercount))
{
cout << "You are most like the mage.";
}
else if (warriorcount > (mediccount||magecount||gunnercount))
{
cout << "You are most like the warrior.";
}
else if (gunnercount > (mediccount||magecount||warriorcount))
{
cout << "You are most like the gunner.";
}
else if (mediccount == magecount)
{
cout << "You have traits of both the medic and the mage.";
}
else if (mediccount == warriorcount)
{
cout << "You have traits of both the medic and the warrior.";
}
else if (mediccount == gunnercount)
{
cout << "You have traits of both the medic and the gunner.";
}
else if (magecount == warriorcount)
{
cout << "You have traits of both the mage and the warrior.";
}
else if (magecount == gunnercount)
{
cout << "You have traits of both the mage and the gunner.";
}
else if (warriorcount == gunnercount)
{
cout << "You have traits of both the warrior and the gunner.";
}

return 0;
}
Last edited on
You forgot a closing brace between lines 25 and 27.

Note two things: 1) there's still quite a bit of repetitive code lying around; 2) some of your conditions are still wrong, some because you're still doing the a > (b || c || d), some because the repetitive code requires you to change many small things in many places.
It looks like the program does exactly what I want it to do, now. Before I go, though, could you give me a brief summary of what the difference is between if{}while and do{}while statements?

EDIT: Woops, looks like if{}while isn't even a valid statement. Thanks for all your help, by the way!
Last edited on
Topic archived. No new replies allowed.