What the heck? Why am I getting 2 errors?

This is a discussion on What the heck? Why am I getting 2 errors? within the C++ Programming forums, part of the General Programming Boards category; I tryed and tryed to see what is wrong with this code, and it just doesn't seem to want to ...

  1. #1
    Registered User Machewy's Avatar
    Join Date
    Apr 2003
    Posts
    42

    Unhappy What the heck? Why am I getting 2 errors?

    I tryed and tryed to see what is wrong with this code, and it just doesn't seem to want to compile. I get these 2 errors:
    line 124 c:\dev-c_~1\mathfun.cpp parse error before `else'
    line 142 c:\dev-c_~1\mathfun.cpp parse error at end of input
    I was making this program for my little brother to help him with his times tables.

    Here it is:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    #define MAX_RANGE 20
    
    int main()
    {
     int answer;
     int input;
     int valuex;
     int valuey;
     int points;
     int chance = 0;
    
     start: // the beginnning labeled for a goto statement
    
     for(int i = 0; i = 100; i++)
     {
      points++;
      srand ( time (NULL) );         // Initialize random generator
      valuex = rand()%MAX_RANGE+1;    // Get random between 1 and MAX_RANGE
    
      srand ( time (NULL) );         // Initialize random generator
      valuey = rand()%MAX_RANGE+1;    // Get random between 1 and MAX_RANGE
    
      answer = valuex * valuey;
      cout<<"What is"
          <<valuex<<" x "
          <<valuey<<"?";
    
      cin>>input;
    
      if(input != answer)
      {
       chance++;
       cout<<"\n Sorry! That is wrong!\n"
           <<valuex<<" x "<<valuey<<" =\n"
           <<answer<<endl;
    
           if(chance = 1)
           {
    
            cout<<"\n You have one more chance!\n"
                <<"Press any key to continue. . .";
            system("PAUSE > nul");
    
           }
    
           else if(chance = 2)
           {
    
            cout<<"\n This is your last chance... \n"
                <<"Press any key to continue. . .";
            system("PAUSE > nul");
    
           }
    
           else
    
           {
            system("CLS");
            cout<<"GAME OVER!\n"
                <<" You scored a total of "
                <<points<<"points!";
    
                if(points < 25)
                {
    
                 cout<<"\n\n You are classed as a little kid.\n"
                     <<"Press any key to exit the game. . .\n";
    
                 system("PAUSE > nul");
                 return 0;
    
                }
    
                else if(points >= 25 &&(points < 50))
                {
    
                 cout<<"\n\n You are classed as a 4th grader.\n"
                     <<"Press any key to exit the game. . .\n";
    
                 system("PAUSE > nul");
                 return 0;
    
                }
    
                else if(points >= 50 &&(points < 75))
                {
    
                 cout<<"\n\n You are classed as an average person.\n"
                     <<"Press any key to exit the game. . .\n";
    
                 system("PAUSE > nul");
                 return 0;
    
                }
    
                else if(points >= 75 &&(points < 100))
                {
    
                 cout<<"\n\n You are classed as a smarty pants.\n"
                     <<"Press any key to exit the game. . .\n";
    
                 system("PAUSE > nul");
                 return 0;
    
                }
    
                else
    
                {
    
                 cout<<"\n\nWow! You only missed 2!  Good job!\n\n"
                     <<"Press any key to exit the game. . .\n";
    
                 system("PAUSE > nul");
                 return 0;
    
                }
    
      } // end big if statement
    
      else
       cout<<"\nCorrect!\n\n";
    
    
     }//end for loop
    
     system("CLS");
     cout<<"What the world?\n"
         <<"Thats impossible!\n"
         <<"How did you beat me?\n"
         <<"\n\n You scored a total of "
         <<points<<" points!\n"
         <<"Thats amazing!!\n"
         <<"Press any key to start the game over. . .";
     system("PAUSE > nul");
     goto start;
     return 0;
    }
    I made it as neat as I could for its readablity.
    I think it has to do with the nested if statements within the for loop or something. Did you find any errors? If you did what was the error, and how do I fix it for my future programs?

    Thank-you so much,
    Best Regards,
    Machewy
    "All things come to an end"

  2. #2
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    Your braces don't match up, you're missing one here
    Code:
    } // end big if statement
    else
       cout<<"\nCorrect!\n\n";
    It should be
    Code:
        } // end big if statement
    }
    else
        cout<<"\nCorrect!\n\n";
    You also have three other comparison problems
    Code:
    for(int i = 0; i = 100; i++)
    ...
    if(chance = 1)
    ...
    else if(chance = 2)
    To test for equivalence you use ==. One = means to make an assignment.

  3. #3
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Originally posted by Casey
    [You also have three other comparison problems
    Code:
    for(int i = 0; i = 100; i++)
    ...
    if(chance = 1)
    ...
    else if(chance = 2)
    To test for equivalence you use ==. One = means to make an assignment. [/B]
    if you get use to writing constance first then these errors will be flaged by the compiler.

    if(chance = 1) // error but the compiler will not see it
    if( 1 = chance) // error
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  4. #4
    Registered User Machewy's Avatar
    Join Date
    Apr 2003
    Posts
    42
    Thanx guys! I appreciate your fast reply, and your dedication. Thank-you for the tip, and the fixed error.
    "All things come to an end"

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    About your headers:

    <iostream.h> is no longer standard, <iostream> is. All the symbols in that header are in the std namespace, so putting a 'using namespace std;' declaration right below your includes will let you access them as you currently do.

    The other two <stdlib.h> and <time.h> are still part of the C++ Standard Library (as they are part of the C Standard Library), but C++ also offers them as: <cstdlib> and <ctime> with the std namespace.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    8
    Just taking a quick look at the code, I've got a question regarding the 'points' variable. It's declared, but not initialized, and then incremented first thing in the 'for' loop (points++).

    Isn't that code incrementing a random value at that point? Is this intentional?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Winsock compilation errors
    By jmd15 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-03-2005, 08:00 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 05:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21