Thread: scanf problem

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    12

    Question scanf problem

    Hello,
    When I select a 2 option in a menu it runs a simple calculator program where I suppose scanf() causes a problem with reading numbers I input. The second number isn't the one I input but it's 0.00 and calculation always displays a result as 0.00.. Where did I make a mistake?

    Code:
    void hello_world();
    float calculator(op, in1, in2);
    
    int main()
    {
        float input1, input2;
        char calc_operator;
        int in;
    
        printf("Menu:\n");
        printf("1. Hello World!\n");
        printf("2. Calculator\n");
        printf("Enter:\n");
        scanf(" %d", &in);
    
        switch(in)
        {
            case 1 : hello_world();
            break;
            case 2 : printf("Calculator mode\n Enter '+' '-' '*' '/'\n");
            scanf(" %c", &calc_operator);
            printf("Enter 1 number:\n");
            scanf(" %f", &input1);
            printf("Enter 2 number:\n");
            scanf(" %f", &input2);
            calculator(calc_operator,input1,input2);
            break;
        }
        return 0;
    }
    
    float calculator(op, in1, in2)
    {
        switch(op)
        {
            case '+' : printf("%.2f + %.2f = %.2f", in1, in2, in1+in2);
            break;
            case '-' : printf("%.2f - %.2f = %.2f", in1, in2, in1-in2);
            break;
            case '*' : printf("%.2f * %.2f = %.2f", in1, in2, in1*in2);
            break;
            case '/' : printf("%.2f / %.2f = %.2f", in1, in2, in1/in2);
            break;
        }
    }
    
    void hello_world()
    {
        printf("Hello World!\n");
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Use a compiler with more warnings enabled.
    Code:
    $ gcc -Wall foo.c
    foo.c:3:1: warning: parameter names (without types) in function declaration
     float calculator(op, in1, in2);
     ^
    foo.c: In function ‘calculator’:
    foo.c:33:7: warning: type of ‘op’ defaults to ‘int’ [-Wimplicit-int]
     float calculator(op, in1, in2)
           ^
    foo.c:33:7: warning: type of ‘in1’ defaults to ‘int’ [-Wimplicit-int]
    foo.c:33:7: warning: type of ‘in2’ defaults to ‘int’ [-Wimplicit-int]
    foo.c:37:27: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
             case '+' : printf("%.2f + %.2f = %.2f", in1, in2, in1+in2);
                               ^
    foo.c:37:27: warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘int’ [-Wformat=]
    foo.c:37:27: warning: format ‘%f’ expects argument of type ‘double’, but argument 4 has type ‘int’ [-Wformat=]
    foo.c:39:27: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
             case '-' : printf("%.2f - %.2f = %.2f", in1, in2, in1-in2);
                               ^
    foo.c:39:27: warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘int’ [-Wformat=]
    foo.c:39:27: warning: format ‘%f’ expects argument of type ‘double’, but argument 4 has type ‘int’ [-Wformat=]
    foo.c:41:27: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
             case '*' : printf("%.2f * %.2f = %.2f", in1, in2, in1*in2);
                               ^
    foo.c:41:27: warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘int’ [-Wformat=]
    foo.c:41:27: warning: format ‘%f’ expects argument of type ‘double’, but argument 4 has type ‘int’ [-Wformat=]
    foo.c:43:27: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
             case '/' : printf("%.2f / %.2f = %.2f", in1, in2, in1/in2);
                               ^
    foo.c:43:27: warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘int’ [-Wformat=]
    foo.c:43:27: warning: format ‘%f’ expects argument of type ‘double’, but argument 4 has type ‘int’ [-Wformat=]
    foo.c:46:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    The two main issues being
    - your function definition and declaration lack any types.
    - a lot of printf/scanf calls have inconsistent type/formats.
    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.

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    12
    Thanks. Now it works.
    Code:
    double calculator(char op, double in1, double in2);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf problem
    By muhtar in forum C Programming
    Replies: 2
    Last Post: 06-09-2015, 03:24 PM
  2. problem with scanf() in while
    By hugoguan in forum C Programming
    Replies: 9
    Last Post: 11-23-2010, 10:17 AM
  3. scanf problem
    By vikash in forum C Programming
    Replies: 1
    Last Post: 05-08-2007, 04:37 AM
  4. scanf problem
    By gsoft in forum C Programming
    Replies: 3
    Last Post: 01-05-2005, 12:42 AM
  5. scanf problem
    By boylemonlaw in forum C Programming
    Replies: 3
    Last Post: 11-22-2003, 11:29 PM

Tags for this Thread