Loop problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dbarten1982
    New Member
    • Feb 2010
    • 8

    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.

    Code:
    int triviaGame(int score)
    {
    	char answer;
    	char correct;
    	int i = 0;
    	int strike = 0;
    	score = 0;
    	string Questions[46];
    	string optionA[46];
    	string optionB[46];
    	string optionC[46];
    	string optionD[46];
    	string question;
    	ifstream questIn;
    	ifstream aIn;
    	ifstream bIn;
    	ifstream cIn;
    	ifstream dIn;
    
    	questIn.open("Questions.txt");
    	aIn.open("OptionA.txt");
    	bIn.open("OptionB.txt");
    	cIn.open("OptionC.txt");
    	dIn.open("OptionD.txt");
    
        while(questIn && i < 45)
    	{
    		i++;
    		getline(questIn,Questions[i]);
        }
    	i = 0;
    	while(aIn && i < 45)
    	{
    		i++;
    		getline(aIn,optionA[i]);
    	}
    	i = 0;
    	while(bIn && i < 45)
    	{
    		i++;
    		getline(bIn,optionB[i]);
    	}
    	i = 0;
    	while(cIn && i < 45)
    	{
    		i++;
    		getline(cIn,optionC[i]);
    	}
    	i = 0;
    	while(dIn && i < 45)
    	{
    		i++;
    		getline(dIn,optionD[i]);
    	}
    	do
    	{
    		i =  rand() % 45;
    		cout << Questions[i] << endl
    			 << endl
    			 << optionA[i] << "      " << optionB[i] << endl
    			 << optionC[i] << "      " << optionD[i] << endl
    			 << endl
    			 << "What is your answer?" << endl
    			 << endl;
    		cin >> answer;
    
    		if( i = 5 || 11 || 21 || 25 || 33 || 38 || 43 )
    			correct = 'a' || 'A';
    		else if( i = 2 || 3 || 7 || 9 || 20 || 22 || 24 || 30 || 36 || 39 || 41 || 42 || 45 )
    			correct = 'b' || 'B';
    		else if( i = 1 || 4 || 10 || 12 || 14 || 16 || 17 || 23 || 26 || 29 || 31 || 40 || 46 )
    			correct = 'c' || 'C';
    		else if( i = 6 || 15 || 18 || 27 || 32 || 35 || 37 )
    			correct = 'd' || 'D';
    		else if( i = 8 || 13 || 28 || 34 )
    			correct = 't' || 'T';
    		else
    			correct = 'f' || 'F';
    		if( answer = correct )
    		{
    			score = score + 1;
    			cout << "That's right, you get a point." << endl
    				 << endl;
    		}
    		if( answer != correct)
    		{
    			strike = strike + 1;
    			cout << "That's not the right answer. You get a strike." << endl
    				 << endl;
    		}
    
    		
    			
    	}while(strike < 3);	
    	
    
    	questIn.close();
    	aIn.close();
    	bIn.close();
    	cIn.close();
    	dIn.close();
    
    	return score;
    }
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    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?

    Comment

    • Dbarten1982
      New Member
      • Feb 2010
      • 8

      #3
      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.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        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.

        Comment

        • Dbarten1982
          New Member
          • Feb 2010
          • 8

          #5
          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.

          Comment

          • Bassem
            Contributor
            • Dec 2008
            • 344

            #6
            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:
            Code:
            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:
            Code:
            if( answer = correct )
            Did you correct it to:
            Code:
            if( answer == correct )
            or you forgot? :)

            Thanks,
            Bassem

            Comment

            • Dbarten1982
              New Member
              • Feb 2010
              • 8

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

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                "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:
                Code:
                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?

                Comment

                • Bassem
                  Contributor
                  • Dec 2008
                  • 344

                  #9
                  Very useful note, one shouldn't forget. Thanks donbock.

                  Comment

                  Working...