Thread: Need help with C

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    Need help with C

    Hey I'm suppose to make a calculator for one of my assignments I'm just having this annoying problem and can't get it fixed.

    I'm trying to use and if-else statement but the else portion keeps executing regardless. please help. I'm not asking you to do my homework for me just correct syntax.

    thanks

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <ctype.h>
    
    
    double main(void)
    {
       double result = 0.0, num1; char operator, end;
       
       
       printf("Calculator is on.\n\n");
       
       
       do {
       
        printf("Enter an operator symbol:\n");
        scanf("%c",&operator);
        if(operator == '+'){
            scanf("%lf",&num1); 
            result = result + num1;
            printf("%lf",result);}
        if(operator == '-'){
            scanf("%lf",&num1); 
            result = result - num1;
            printf("%lf",result);}
        if(operator == '*'){
            scanf("%lf",&num1); 
            result = result * num1;
            printf("%lf",result);}
        if(operator == '/'){
            scanf("%lf",&num1); 
            result = result / num1;
            printf("%lf",result);}
        else{
            printf("UnknownOperatorException is thrown\n");}
            
        }while(end!='r' ||end!='R');
        
        
       return 0;
       }

    Also would i have to put a getchar statement for the while statement to end or can i just move down the while statement after the scanf statement and input 'operator' instead of 'end'
    Last edited by Rosas; 10-08-2012 at 05:32 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You have a leftover new line in your input. We have plenty of info on dealing with this. It's not an official FAQ article yet (but soon it will be), so I will link you to the discussion of the rough draft: http://cboard.cprogramming.com/c-pro...-expected.html.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    i still don't understand why it does that i read the post but can't seem to eliminate the problem. I've been at this problem for 4 hours. i'm just wasting my time

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    main has to return an int (not a double).

    Your scanf call with the %c format should have a space before the %c:
    Code:
        scanf(" %c",&operator);
    Your printf's should use the format %f, not %lf (although scanf does need %lf).

    You need to use "else if". I.e.:
    Code:
        if(operator == '+'){
            scanf("%lf",&num1); 
            result = result + num1;
            printf("%f",result);
        }
        else if(operator == '-'){
            scanf("%lf",&num1); 
            result = result - num1;
            printf("%f",result);
        }
        //etc.
    Your end condition should use && not ||.

    And your brace placement is idiotic. It's hard to read, which defeats the purpose.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Thanks a bunch your my hero.

    *bows*

    Now I'll never make those mistakes again. muahahaha

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    i finished it now time for the next 3 programs.

Popular pages Recent additions subscribe to a feed