Simple Math App

stdntmp

I am new in Objective C, and I need to do a basic math application within a 5 times loop. Users will need to state what operation will perform (+,-,*,/) and the program will generates two random numbers to do math operations. Then the user will do the math and the program will compare his input with the correct answer. At the end the user will receive a score % and a custom message according to right or wrong answer. I am a bit stuck. First I did the program which generates two random numbers and run nicely. However when I added the rest of the code I have a warning message that says that the implementation is incomplete. I am also unable to see what is happening whit the else structure within the case because I received errors indicating "Expected expression" in each one of them. Any help is very much appreciated. Here is my code:

#import <Foundation/Foundation.h>
@interface myMath : NSObject  
  //methods     
  -(int) getRandomNumber;
  -(void) add: (double) result;
  -(void) subtract: (double) result;
  -(void) multiply: (double) result;
  -(void) divide: (double) result;
@end  
@implementation myMath
  //returns a random number between 1 and 100     
  -(int) getRandomNumber{         
  return (arc4random()%(100-1))+1;     
 }
@end  
@interface Calculator : NSObject   
   @property double setaccumulator, accumulator; //Synt all methods.    
@end    
@implementation Calculator{     
    double accumulator;  
  }  
  -(void) setAccumulator: (double) value;
  {     accumulator = value;  }  
  -(void) clear  
  {      accumulator = 0;  }  
  -(double) accumulator  
  {     return accumulator;  }  
  -(void) add: (double) value  
  {     accumulator += value;  }  
  -(void) subtract: (double) value  
  {     accumulator -= value;  }  
  -(void) multiply: (double) value  
  {     accumulator *= value;  }  
  -(void) divide: (double) value  
  {     accumulator /= value;  }  
@end  
int main(int argc, const char * argv[]) {     

@autoreleasepool {         
  myMath *myMathStuff;         
  myMathStuff = [[myMath alloc] init];         

  int rnum1 = [myMathStuff getRandomNumber];         
  int rnum2 = [myMathStuff getRandomNumber];          
  double result;          
  char operator;               

  Calculator *myCalculator = [[Calculator alloc]init];          
  int n, right, wrong, cont;          
  n = 0, right, wrong, cont = 0;          

  NSLog(@"The random numbers are %i and %i", rnum1, rnum2);
  while (n <= 5){                    
    NSLog(@"What operation do you want to perform? (+ . - . * . / .");
      scanf(" %c", &operator);
      [myCalculator setAccumulator:(double) rnum1];

      switch (operator) {                                
         case '+':                  
           [myCalculator add: rnum2];                  
           NSLog(@"Please type the addition result:      ");
           scanf("%lf", &result);                  

           if (result == rnum1)                     
             right =+1;                      
             NSLog(@"Congratulations, you did it right!");
              else                       
                wrong =+1;                      
                NSLog(@"Sorry, the addition result is: %.2f", [myCalculator accumulator]);
           break;
         case '-':                  
           [myCalculator subtract: rnum2]
              NSLog(@"Please type the subtraction:      ");
              scanf("%lf", &result);                  
              if (result == rnum1)                      
                right =+1;                  
                   NSLog(@"Congratulations, you did it right!");                  
              else                      
                wrong =+1,                      
                NSLog(@"Sorry, the subtraction result is: %.2f", [myCalculator accumulator]);                  
             break;                                
          case '*':                  
             [myCalculator multiply: rnum2];                  
             NSLog(@"Please type the multiplication result:      ");
             scanf("%lf", &result);                  
             if (result == rnum1)                   
               right =+1;                  
               NSLog(@"Congratulations, you did it right!");                  
             else                      
               wrong =+1,                      
               NSLog(@"Sorry, the multiplication result is: %.2f", [myCalculator accumulator]);                  
             break;              
             case '/':                  
               [myCalculator divide: rnum2];
              NSLog(@"Please type the division result:      "); 
              scanf("%lf", &result);                  
              if (result == rnum1)                      
                right =+1;                  
                NSLog(@"Congratulations, you did it right!");                  
              else                      
                wrong =+1,                      
                NSLog(@"Sorry, the division result is: %.2f", [myCalculator accumulator]);                  
              break;              
          default:                  
         break;          
        }          
   ++n;             
 }        
 NSLog(@"You were right %i", right);      
 NSLog(@"You were wrong  %i", wrong);      
 if (right == 4)              
   NSLog(@"Excellent you have a perfect 100 percent score");    
 else if (right == 3)          
   NSLog(@"Good job you have a 80 percent score");      
 else if (right == 2)       
   NSLog (@"Well, you have a 60 percent score");      
 else if (right ==1)          
   NSLog(@"Sorry, you have received a 20 percent score");  
 }      
 return 0;  
}

I made some changes on the myMath implementation and I also changed the lines where the "else" appears as I show below. I am still getting the expected expression. If somebody else can see what I am not seeing I would appreciate your input. Part of the code I changed: switch (operator) { case '+':

                [myMathStuff add: rnum2];                    
                NSLog(@"Please type the addition result:      ");                    
                scanf("%lf", &result);                    
                if (result == rnum1)

                    right =+1;
                    NSLog (@"Congratulations, you did it right!");

                else {

                    wrong =+1;
                    NSLog (@"Sorry, the addition result is: %.2f", [myMathStuff accumulator]);
                }

                break;

I was able to find the solution.

Jason Renaldo

You are getting implementation is incomplete because in your MyMath class you are saying that you have 5 methods at the class level, and only one of those (GetRandomNumber) is actually implemented.

You need to finish the contract - that is - code out what the other four methods are going to do (add, subtract, multiply, divide).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related