Thread: Calculator code help

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    Calculator code help

    I just started doing the C tutorials yesterday and decided to apply the little i have learnt. My calculator is supposed to take an input for two numbers, then it has to print a menu whit four mathematical operations and takes input of the operation selected. If conditions apply to each operation. After that has been done the result of the operation is supposed to be printed. The code complies with no errors but after the second number has been inputed it prints the menu and the result. Here is my code and any sort of help will be greatly appreciated. Thank You
    Code:
    #include <stdio.h> 
    
     int main() 
    {
      /*Author: Morpheous 11 
       14th December 2008 */
    
    
      double firstNum; 
      double secNum; 
      double result;
      char operationSelected;
      
      printf("Enter your first number:\n"); 
      scanf("%lf",&firstNum);  
      printf("Enter your second number:\n");
      scanf("%lf",&secNum);
       
      printf("Please select an operation\n"); 
      
      printf("addition(+)\n"); 
      printf("subtraction(-)\n"); 
      printf("multiplication(*)\n"); 
      printf("division(/)\n");  
    
      scanf("%c",&operationSelected); 
    
        if (operationSelected == '+')
        {
          result = firstNum + secNum;
        }// end of if statement for addition  
    
        else if (operationSelected == '-')
         {
           result = secNum - firstNum;
         }// end of if statement for subtraction
        
    
        else if (operationSelected == '*')
         {
            result = firstNum * secNum;
         }// end of if statement for multiplication     
      
        else if (operationSelected == '/')
         {  
           result = firstNum / secNum;
         }// end of if statement for division     
        
         else 
          printf("Enter a valid operation");
    
      printf("The result is %lf",result);
      getchar();  
      return 0;
    }// end of main

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your code looks good in the code tags.

    Your written description is so poor, I can't help but giggle through my pain of trying to decipher WTF you're trying to say. You can do better than this!

    Please post an input that shows the wrong output, and post that wrong output, as well.

    Don't get nervous or distracted. Just write it up, like you would say it, if you were talking to us.

    We pretty much have it figured out that all programs need to actually print out a result, so no need to belabor the obvious, please.

    And welcome to the forum. Don't take anything I post, personally - it's not meant that way.

    Oh! One problem is easy to spot - you're not pulling the newline off the keyboard's buffer.

    With scanf(), it leaves the '\n' (newline), behind. That goofs up the next scanf().

    You need one of these after every scanf(), except the last one:

    garbage = getchar();

    to pull the newline off. I use "gar" for short, but any function that pulls a char off the keyboard buffer (stdin), will do fine.
    Last edited by Adak; 12-15-2008 at 08:19 PM.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    thank you

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    Modified it lil bit id hope it could help.
    Code:
    #include <stdio.h> 
    
     int main() 
    {
      /*Author: Morpheous 11 
       14th December 2008 *
       *modifed By LOLGUY*/
    
    
      double firstNum; 
      double secNum; 
      double result;
      int operationSelected;
      
      fputs("Enter your first number:",stdout); 
      scanf("&#37;lf",&firstNum);  
      fputs("Enter your second number:",stdout);
      scanf("%lf",&secNum);
       
      puts("Please select an operation"); 
      
      puts("addition(1)"); 
      puts("subtraction(2)"); 
      puts("multiplication(3)"); 
      puts("division(4)");  
    
      scanf("%d",&operationSelected);
      switch(operationSelected)
      {
    case 1 :
    result = firstNum + secNum;
    break;
    case 2:
    result=secNum - firstNum ;
    break;
    
        
    case 3:
    result = firstNum * secNum;break;
    case 4:
    result = firstNum / secNum;break;
    
    }
    
      printf("The result is %lf",result);
      getchar();  
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    thank you for your help. I decided to use if statements becuase i dont know how to char the switch to take characters.

Popular pages Recent additions subscribe to a feed