473,395 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Loop problem

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.

Expand|Select|Wrap|Line Numbers
  1. int triviaGame(int score)
  2. {
  3.     char answer;
  4.     char correct;
  5.     int i = 0;
  6.     int strike = 0;
  7.     score = 0;
  8.     string Questions[46];
  9.     string optionA[46];
  10.     string optionB[46];
  11.     string optionC[46];
  12.     string optionD[46];
  13.     string question;
  14.     ifstream questIn;
  15.     ifstream aIn;
  16.     ifstream bIn;
  17.     ifstream cIn;
  18.     ifstream dIn;
  19.  
  20.     questIn.open("Questions.txt");
  21.     aIn.open("OptionA.txt");
  22.     bIn.open("OptionB.txt");
  23.     cIn.open("OptionC.txt");
  24.     dIn.open("OptionD.txt");
  25.  
  26.     while(questIn && i < 45)
  27.     {
  28.         i++;
  29.         getline(questIn,Questions[i]);
  30.     }
  31.     i = 0;
  32.     while(aIn && i < 45)
  33.     {
  34.         i++;
  35.         getline(aIn,optionA[i]);
  36.     }
  37.     i = 0;
  38.     while(bIn && i < 45)
  39.     {
  40.         i++;
  41.         getline(bIn,optionB[i]);
  42.     }
  43.     i = 0;
  44.     while(cIn && i < 45)
  45.     {
  46.         i++;
  47.         getline(cIn,optionC[i]);
  48.     }
  49.     i = 0;
  50.     while(dIn && i < 45)
  51.     {
  52.         i++;
  53.         getline(dIn,optionD[i]);
  54.     }
  55.     do
  56.     {
  57.         i =  rand() % 45;
  58.         cout << Questions[i] << endl
  59.              << endl
  60.              << optionA[i] << "      " << optionB[i] << endl
  61.              << optionC[i] << "      " << optionD[i] << endl
  62.              << endl
  63.              << "What is your answer?" << endl
  64.              << endl;
  65.         cin >> answer;
  66.  
  67.         if( i = 5 || 11 || 21 || 25 || 33 || 38 || 43 )
  68.             correct = 'a' || 'A';
  69.         else if( i = 2 || 3 || 7 || 9 || 20 || 22 || 24 || 30 || 36 || 39 || 41 || 42 || 45 )
  70.             correct = 'b' || 'B';
  71.         else if( i = 1 || 4 || 10 || 12 || 14 || 16 || 17 || 23 || 26 || 29 || 31 || 40 || 46 )
  72.             correct = 'c' || 'C';
  73.         else if( i = 6 || 15 || 18 || 27 || 32 || 35 || 37 )
  74.             correct = 'd' || 'D';
  75.         else if( i = 8 || 13 || 28 || 34 )
  76.             correct = 't' || 'T';
  77.         else
  78.             correct = 'f' || 'F';
  79.         if( answer = correct )
  80.         {
  81.             score = score + 1;
  82.             cout << "That's right, you get a point." << endl
  83.                  << endl;
  84.         }
  85.         if( answer != correct)
  86.         {
  87.             strike = strike + 1;
  88.             cout << "That's not the right answer. You get a strike." << endl
  89.                  << endl;
  90.         }
  91.  
  92.  
  93.  
  94.     }while(strike < 3);    
  95.  
  96.  
  97.     questIn.close();
  98.     aIn.close();
  99.     bIn.close();
  100.     cIn.close();
  101.     dIn.close();
  102.  
  103.     return score;
  104. }
Feb 25 '10 #1
8 2470
whodgson
542 512MB
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?
Feb 25 '10 #2
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.
Feb 25 '10 #3
donbock
2,426 Expert 2GB
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.
Feb 25 '10 #4
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.
Feb 25 '10 #5
Bassem
344 100+
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:
Expand|Select|Wrap|Line Numbers
  1. if(....)
  2.    correct = 'c'
  3. .
  4. .
  5. .
  6. if(answer == correct || answer == correct - 32)
  7. {
  8.     // user answer is right
  9. }
  10. else
  11. {
  12.    // user answer is wrong.
  13. }
  14.  
Note: number 32: is the difference between lower case char and upper case char.

Last thing about your existing code:
Expand|Select|Wrap|Line Numbers
  1. if( answer = correct )
Did you correct it to:
Expand|Select|Wrap|Line Numbers
  1. if( answer == correct )
or you forgot? :)

Thanks,
Bassem
Feb 25 '10 #6
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.
Feb 25 '10 #7
donbock
2,426 Expert 2GB
"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:
Expand|Select|Wrap|Line Numbers
  1. static const char answerKey[] = {
  2.    'F', 'C', 'B', 'B', 'C', 'A', 'D', 'B', 'T', 'B',      // 0-9
  3.    ...
  4. };
  5. static const int numAnswers = (int) (sizeof(answerKey)/sizeof(answerKey[0]));
  6.  
  7. ...
  8. if ((i < 1) || (i > numAnswers))
  9.    <trap the error condition>
  10. correct = answerKey[i];
  11. if (toupper(answer) == correct)
  12.    <answer is correct>
  13. else
  14.    <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?
Feb 25 '10 #8
Bassem
344 100+
Very useful note, one shouldn't forget. Thanks donbock.
Feb 25 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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 ...
5
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...
11
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...
12
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...
43
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...
63
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
15
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...
2
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...
5
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"); }
1
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....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.