Thread: Why wont this simple calculator program using the switch function work?

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    1

    Why wont this simple calculator program using the switch function work?

    Code:
    /*program to evaluate simple expresion of the form
    value operator value*/
    
    
    #include <stdio.h>
    
    
    int main(void)
    {
        float value1, value2;
        char operator;
    
    
        printf("Type in your expression.\n");
        scanf("%f %c % f", &value1, &operator, &value2);
    
    
        switch (operator)
        {
            case '+':
                printf("%.2f\n", value1 + value2);
                break;
            case '-':
                printf("%.2f\n", value1 - value2);
                break;
            case '*':
                printf("%.2f\n", value1 * value2);
                break;
            case '/':
                if (value2 == 0)
                    printf("Division by zero\n");
                else
                    printf("%.2f\n", value1 / value2);
                break;
            default:
                printf("Unknown operator\n");
                break;
        }
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It looks like either you're ignoring your compiler's warnings/errors or you have a defective compiler.

    Here is what my compiler says about your code:

    ||=== Build: Debug in chomework (compiler: gcc9-2) ===|
    /main.c||In function ‘main’:|
    /main.c|11|warning: unknown conversion type character ‘ ’ in format [-Wformat=]|
    /main.c|11|warning: too many arguments for format [-Wformat-extra-args]|
    /main.c|26|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
    ||=== Build finished: 0 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
    ||=== Run: Debug in chomework (compiler: gcc9-2) ===|
    You need to see about fixing these warnings. You have a "typo" on line 11.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf("%f %c % f", &value1, &operator, &value2);
    There is a space between "%" and "f" which should not be there.

    > I'm using Codeblocks.
    Go into project settings and put -Wall -Wextra on the compiler options.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 06-30-2013, 02:32 PM
  2. Replies: 5
    Last Post: 04-03-2013, 10:03 PM
  3. program wont work, why?
    By chasingxsuns in forum C Programming
    Replies: 11
    Last Post: 02-02-2006, 04:38 PM
  4. Novice Beginner: Simple hello world wont work
    By hern in forum C++ Programming
    Replies: 8
    Last Post: 06-25-2005, 12:16 PM
  5. My Pop function wont work - can you help?
    By lindilou in forum C++ Programming
    Replies: 0
    Last Post: 05-04-2003, 02:31 PM

Tags for this Thread