SImple code, need help

This is a discussion on SImple code, need help within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, Just started really getting into C++ and i need a bit of help with a code: Code: #include ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    SImple code, need help

    Hey everyone,
    Just started really getting into C++ and i need a bit of help with a code:

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    
    #define lastQuestion 4
    
    const int addition = 1;
    const int subtraction = 2;
    const int multiply = 3;
    const int division = 4;
    
    void askQuestion(int one, int two, int three, int x)
    {
        int answer, input;
        
        switch (x)
        {
            case addition:
                    answer = one + two;
                    cout << one << " + " << two << " = ";
                    break;
            case subtraction:
                    answer = one - two;
                    cout << one << " - " << two << " = ";
                    break;
            case multiply:
                    answer = one * two;
                    cout << one << " * " << two << " = ";
                    break;
            case division:
                    answer = one / two;
                    cout << one << " / " << two << " = ";
                    break;      
                    
        }
    
        cin >> input;
        
        if (input == answer)
            cout << "You are right!" << endl;
        else
            cout << "You are wrong, the right answer is " << answer << endl;
      
    }
    
    int main(int argc, char *argv[])
    {
      int one[lastQuestion], two[lastQuestion],three[lastQuestion], x[lastQuestion];
      
      one[1] = 8; two[1] = 3; x[1] = addition;
      one[2] = 4; two[2] = 2; x[2] = subtraction;  
      one[3] = -3; two[3] = -3; x[3] = multiply;   
      one[4] = 15; two[2] = 5; x[2] = subtraction; 
        
      for(int i = 1; i <= lastQuestion; i++)
        askQuestion(one[i], two[i], three[i], x[i]);
    
      system("PAUSE");	
      return 0;
    }
    The program allows you to answer 3 math problems, but when I add the fourth, it skips over it. Any help is appreciated.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    arrays are numbered 0 to but not includeing lastQuestion
    Code:
      one[0] = 8; two[0] = 3; x[0] = addition;
      one[1] = 4; two[1] = 2; x[1] = subtraction;  
      one[2] = -3; two[2] = -3; x[2] = multiply;   
      one[3] = 15; two[3] = 5; x[3] = subtraction; 
    
    for(int i = 0; i <  lastQuestion; i++)

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    one [4] has bounds 0 - 3, not 1 - 4

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    Thanks, I got it working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 11-23-2005, 08:53 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 02:29 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 11:34 AM

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