Thread: Can someone help me with this simple C calculator program ?

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    7

    Can someone help me with this simple C calculator program ?

    Code:
    void Calc_Sales(void)
    
    
        {char o='o';
        int ch;
        float num1,num2;
        printf("Enter operator either + or - or * or divide : \n");
        scanf(" %c", &ch);
        printf("\nEnter two operands: ");
        scanf("%f%f",&num1,&num2);
        switch(o) {
            case '+':
                printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
                break;
            case '-':
                printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
                break;
            case '*':
                printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
                break;
            case '/':
                printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
                break;
            default:
                /* If operator is other than +, -, * or /, error message is shown */
                printf("Error! operator is not correct");}
                
        
    
    
        /* Flushes input buffer */
        while ((ch = getchar()) != '\n' && ch != EOF) ;
     
        printf("\n\nPress ENTER to continue.");
        while ((ch = getchar()) != '\n' && ch != EOF);
     
        getch();
    }


    This is actually a part of a program, whenever i input all the required values such as the operator and the operands, it gives the following error: "Operator is not correct"

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should post the simplest, complete (i.e. compilable) program that exhibits the problem. This will allow us to compile and run the program ourselves, to help find the trouble.

    This was easy enough to simply add a "main()" function to, so I took the liberty (this time).

    Code:
    /*
    warning: format '%c' expects type 'char *', but argument 2 has type 'int *'|
    */
    Note that "ch" is declared as an int.

    But also, you're doing something plainly wrong:

    Code:
    char o='o';
    
    // ...
    printf("Enter operator either + or - or * or divide : \n");
    scanf(" %c", &ch);
    
    // ...
    
    switch(o) {
        // ...
    You read the operand into "ch", but use the variable "o" for your switch condition.

    I'd also strongly advise you use more descriptive variable names. "o" and "ch" say nothing about what the variables are used for. How about naming it "operation" or some such?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    scanf() on line 8 is reading value into one variable. switch() on line 11 is working with another variable.

    Incidentally, scanf("%c", .... ) should be supplied a pointer to char, not a pointer to int.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Dec 2014
    Posts
    7
    Thanks a bunch guys! That solved my problem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to write a program of a simple calculator?
    By yeohwl91 in forum C Programming
    Replies: 5
    Last Post: 09-24-2011, 04:04 PM
  2. Write a program to model a simple calculator
    By Annihilate in forum C Programming
    Replies: 5
    Last Post: 11-22-2010, 01:14 PM
  3. Replies: 15
    Last Post: 12-31-2007, 06:26 AM
  4. A Simple Calculator Program Not Working
    By oobootsy1 in forum C++ Programming
    Replies: 9
    Last Post: 01-09-2004, 09:34 PM
  5. Replies: 7
    Last Post: 07-19-2002, 04:33 AM

Tags for this Thread