Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

What is wrong with this Switch statement???????

  • 12-11-2008 6:41pm
    #1
    Registered Users, Registered Users 2 Posts: 1,164 ✭✭✭


    #include <stdio.h>
    main()
    {
    char operator ;
    float num1, num2, answer ;

    printf("Please enter an arithmetic expression (e.g. 1 + 2) " ) ;
    scanf ( "%f%1s%f", &num1, &operator, &num2 ) ;

    switch ( operator )
    {
    case '+' :
    answer = num1 + num2 ;
    printf( "%f plus %f equals %f\n", num1, num2, answer ) ;
    break ;
    case '-' :
    answer = num1 - num2 ;
    printf( "%f minus %f equals %f\n", num1, num2, answer ) ;
    break ;
    case '*':
    answer = num1 * num2 ;
    printf ( "%f multiplied by %f equals %f\n", num1, num2, answer ) ;
    break ;
    case '/' :
    answer = num1 / num2 ;
    printf("%f divided by %f equals %f\n", num1, num2, answer ) ;
    break ;
    default:
    printf("Invalid operator\n") ;
    }
    }

    7 Errors
    Error E2076 calculator.c 6: Overloadable operator expected in function main()
    Error E2139 calculator.c 7: Declaration missing ; in function main()
    Error E2451 calculator.c 10: Undefined symbol 'num1' in function main()
    Error E2451 calculator.c 10: Undefined symbol ',' in function main()
    Error E2451 calculator.c 10: Undefined symbol 'num2' in function main()
    Error E2076 calculator.c 12: Overloadable operator expected in function main()
    Error E2377 calculator.c 13: Switch statement missing ) in function main()


Comments

  • Registered Users, Registered Users 2 Posts: 2,082 ✭✭✭Tobias Greeshman


    That code compiles just fine in gcc on linux.

    What compiler did you try and compile this on?


  • Registered Users, Registered Users 2 Posts: 621 ✭✭✭Peadar06


    #include <stdio.h>
    main()
    {
    char option ;
    float num1, num2, answer ;

    printf("\nPlease enter an arithmetic expression (e.g. 1 + 2)\n" ) ;
    scanf ( "%f%1s%f", &num1, &option, &num2 ) ;

    switch ( option )
    {
    case '+' :
    answer = num1 + num2 ;
    printf( "%.2f plus %.2f equals %.2f\n", num1, num2, answer ) ;
    break ;

    case '-' :
    answer = num1 - num2 ;
    printf( "%.2f minus %.2f equals %.2f\n", num1, num2, answer ) ;
    break ;

    case '*':
    case 'x':
    case 'X':
    answer = num1 * num2 ;
    printf ( "%.2f multiplied by %.2f equals %.2f\n", num1, num2, answer ) ;
    break ;

    case '/' :
    answer = num1 / num2 ;
    printf("%.2f divided by %.2f equals %.2f\n", num1, num2, answer ) ;
    break ;

    default:
    printf("Invalid operator\n") ;
    }
    }

    This works compiles perfectly now in Borland C++ and Dev C++, you originally had "char operator ;", which can't be used as operator is a key word in C programming. I hope this is of some help to you.

    Peter


  • Registered Users, Registered Users 2 Posts: 111 ✭✭Boxman


    The switch statement is fine but assuming you are using a c++ compiler you need to rename your operator variable to something else as 'operator' is a reserved keyword in c++.


  • Registered Users, Registered Users 2 Posts: 621 ✭✭✭Peadar06


    Yes of course, that is true, that is the reason that I changed operator to option.

    Peter


  • Registered Users, Registered Users 2 Posts: 111 ✭✭Boxman


    I didn't see your post Peadar - You just beat me to it! :-)


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 111 ✭✭Boxman


    Also OP there is another problem - with your scanf line.

    scanf ( "%f%1s%f", &num1, &operator, &num2 ) ;

    You are attempting to copy a string of length 1 into your 'operator' variable. Your 'operator' variable only occupies 1 char of space in memory.

    Leaving the name 'operator' as it is for the moment, the string of length 1 will actually take up 2 chars of space - 1 char for the user inputted '+' or whatever, and 1 char for the '\0' special char which terminates the string.

    Now 2 into 1 wont go which results in memory after the address of the 'operator' variable being overwritten.

    While you might be lucky and not see any side effects, it is a crash waiting to happen and can lead to unpredictable behaviour. e.g. Segmentation faults.

    So , you should rename your 'operator' variable to 'option' (if using a c++ compiler), and allocate 2 chars for it. i.e

    char option[2].

    Change your scanf line to

    scanf ( "%f%1s%f", &num1, option, &num2 ) ;
    (the name of an array is in fact a pointer to the first location)

    and 'switch' on the first element.

    switch (option[0])


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    OP have a read of the forum charter please, and next time could you make an effort to explain the problem you are experiencing instead of just posting the code, the errors and getting somebody else to do your homework for you.


  • Registered Users, Registered Users 2 Posts: 1,164 ✭✭✭BaRcOe


    Evil Phil wrote: »
    OP have a read of the forum charter please, and next time could you make an effort to explain the problem you are experiencing instead of just posting the code, the errors and getting somebody else to do your homework for you.

    Sorry im new to C thats all. it was just me trying to get used of switch statements, not homework, ill try be more specific from now on?


  • Registered Users, Registered Users 2 Posts: 1,164 ✭✭✭BaRcOe


    Evil Phil wrote: »
    OP have a read of the forum charter please, and next time could you make an effort to explain the problem you are experiencing instead of just posting the code, the errors and getting somebody else to do your homework for you.

    Homework & Learning to code: How to ask a question! (The Rules)

    * Try and give a good description of your problem in the thread title.
    * Post a sample of your code no matter how poor you may think it is. It shows an effort on your behalf and that's really important
    * Try and explain your attempt in the previous point
    * Now ask any questions you may have

    I gave a sample of my code. I made an effort.
    I showed my errors and got help. I thanked the posters for it and am satisfied

    a warning is harsh


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Posting code, posting errors but not posting an explanation of what your problem is is just plain lazy. So you get a warning.

    Complaining about that gets you banned.


  • Advertisement
This discussion has been closed.
Advertisement