I am making a trivia game and I am having some issues with a loop in it. When it runs it show all answers are right. The game is based on a 3 strike system, you get three strikes and the games over and it awards 1 point for each right answer. What am I missing? I am new to programming and haven't run into a problem like this to troubleshoot. I am using a parallel array to read in all the questions and chioces. An everything is working except getting it to distinguish between the right and wrong answers. Thanks in advance for any advise. 8 2470
One reason is your code in l67 and the like where the if (statement) reads i=5 || 11
It should read: if(i=5 || i=11 || i= .....etc); Do you mean if i 'is assigned' [=] 5 or if i 'equals' [==] 5?
Thank you for the quick reply. If I change the ' if ' statement expressions to
(i = 5 || i = 7 || i = 9 etc...) it gives numerous error codes with this. The error code I get is "error C2106: '=' : left operand must be l-value" I am using Visual studio 2008 if that matters. What I want it to do is to assign the right value to the variable 'correct' based on the random value of ' i '. Thats why I wrote this the way I have. That way if (i = 5 or 7 or 9 etc...). The program is taking any user input as the correct answer. Hope this helps to clarify.
Take a closer look at whodgson's post.
Try (i == 5 || i == 7 || i == 9 etc. Notice the equals signs come in pairs.
A single equals sign is used to assign a value to a variable; a pair of equals signs is used to test for equality.
I tried this and the extra '=' doesn't give the errors as before, but it still treats all input as the correct answer. So is there an issue with assigning the value to 'correct' or in the loops in line 79 and 85 or both? Thanks for all the help.
Hi,
What do you mean by:
if( ....)
correct = 'c' || 'C'
I think it is a wrong expression, but I see what you mean.
You use assign the lower case character like 'c' and when you compare if it the right answer try the lower or the upper.
like this: - if(....)
-
correct = 'c'
-
.
-
.
-
.
-
if(answer == correct || answer == correct - 32)
-
{
-
// user answer is right
-
}
-
else
-
{
-
// user answer is wrong.
-
}
-
Note: number 32: is the difference between lower case char and upper case char.
Last thing about your existing code:
Did you correct it to:
or you forgot? :)
Thanks,
Bassem
Lol no I went back and changed that, but the method you suggested
if(....)
correct = 'c'
.
.
.
if(answer == correct || answer == correct - 32)
worked perfectly!
I have never tried to do it that way and didn;t even think about the numeric difference between upper and lower case. To all that have given me assistance thank you so much, not only have I work this problem out I've learned something from it as well.
"32" is a magic number. It is very hard for somebody reading your code to understand that that number accomplishes or to be sure that is in fact the right number. It would be very painful for you to change the answer key. Try something more flexible like this instead: - static const char answerKey[] = {
-
'F', 'C', 'B', 'B', 'C', 'A', 'D', 'B', 'T', 'B', // 0-9
-
...
-
};
-
static const int numAnswers = (int) (sizeof(answerKey)/sizeof(answerKey[0]));
-
-
...
-
if ((i < 1) || (i > numAnswers))
-
<trap the error condition>
-
correct = answerKey[i];
-
if (toupper(answer) == correct)
-
<answer is correct>
-
else
-
<answer is incorrect>
Now you have a table of answers that mirrors your question table and option tables. Notice there is no need to hardcode the number of questions into your program as the magic number "45".
Is i==0 a legal question?
Very useful note, one shouldn't forget. Thanks donbock.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Charles Alexander |
last post by:
Hello
I am new to php & MySQL - I am trying to retrieve some records from a MySQL
table and redisplay them. The data in list form looks like this:
Sample_ID Marker_ID Variation
...
|
by: build |
last post by:
G'day All,
I have a problem with this loop.
There are a number of .txt files in 'myPath'.
tmpFile = Dir(myPath & "\*.txt")
'PROCESS FOLDER
Do Until tmpFile = ""
<lottsa code>
<too much to...
|
by: ritterhaus |
last post by:
Just a simple bit of code to toggle between two state at intervals...
import time
for i in range(4):
print 'On'
time.sleep(1)
print 'Off'
time.sleep(1)
.... SHOULD toggle On and Off four...
|
by: reynoldscraigr |
last post by:
Hi All,
hope someone can see what wrong here I have the following function
function RemoveMenuFromHoldArray(menuName) {
var i = 0;
for (i=0;i<=MenusToHoldOpen.length-1;i++) {
if...
|
by: Gremlin |
last post by:
If you are not familiar with the halting problem, I will not go into
it in detail but it states that it is impossible to write a program
that can tell if a loop is infinite or not. This is a...
|
by: Aaron Ackerman |
last post by:
What is the sytax for exiting a for loop in C#?
|
by: Mike Lansdaal |
last post by:
I came across a reference on a web site
(http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines )
that said to speed up access to a rich text box's lines that you needed to
use a...
|
by: d3vkit |
last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies.
I am using PHP5 with...
|
by: sgurukrupagmailcom |
last post by:
Hi,
I haven't come accross an elegant solution to a design problem that I show below. Have a look at the piece of code here:
class Exc
{
Exc ()
{
System.out.println ("Haribol");
}
|
by: JavaJon |
last post by:
Hello, I'm Jon.
I've recently picked up Java after using a "gimmick" programming language called GML ( Game Maker Language ). I've read a lot of tutorials and even a Java for Dummies *.pdf book....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |