Thread: Comand line Calculator

  1. #1
    Registered User
    Join Date
    Jan 2023
    Posts
    3

    Unhappy Comand line Calculator

    Code:
    int main (int argc, char* argv[]){
        char *ptr = NULL;
        double OPE1, OPE2;
        char OPERATOR;
        double ERG;
    
    
        if(argc < 4)
        {
            printf("Error! Nicht genug inputs\n");
            return 0;
        }
    
    
        OPE1 = strtod(argv[1], &ptr);
        OPE2 = strtod(argv[3], &ptr);
        OPERATOR = argv[2][0];
    
    
        switch(*argv[2])
        { ...... }

    • I have been given these instructions : Use strtod() to convert operand1 and operand2 to double. Use strtod() 's parameter endptr to check if the operands were valid.


    • 1) This is my code. Can anyone tell me if i have implemented the instructions correct?

      2) I think that switch(argv[2]) is not correct because one of the switch cases is xx and i get as a warning : case label value exceeds maximum value for type

      3) I also get the warning : multi-character character constant

      I would really appreciate any feedback!




    Last edited by Noizy1; 01-06-2023 at 01:06 PM.

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    121
    "2) I think that switch(argv[2]) is not correct because one of the switch cases is xx and i get as a warning : case label value exceeds maximum value for type"

    I don't see any "switch cases".

  3. #3
    Registered User
    Join Date
    Jan 2023
    Posts
    3
    Code:
    case'+':        ERG=OPE1+OPE2;
            printf("Ergebnis = %.1lf \n", ERG);
            break;
    
    
        case '-':
            ERG=OPE1-OPE2;
            printf("Ergebnis = %.1lf \n", ERG);
            break;
    
    
        case '/':
            ERG=OPE1/OPE2;
            if(OPE2==0)
            {
                printf("Error! Division durch  Null ist nicht moeglich!\n");
                break;
            }
            else
                printf("Ergebnis = %.1lf \n", ERG);
            break;
    
    
        case 'x':
            ERG=OPE1 * OPE2;
            printf("Ergebnis = %.1lf \n", ERG);
            break;
    
    
        case '_/':
            ERG=pow(OPE1, 1/OPE2);
            if(OPE2==0)
            {
                printf("Error!\n");
                break;
            }
    
    
            else if(ERG<0)
            {
                printf("Error!");
                break;
            }
            else
                printf("Ergebnis = %.1lf \n", ERG);
    
    
        case 'xx':
            ERG=pow(OPE1, OPE2);
            printf("Ergebnis = %.1lf \n", ERG);
    these are my switch cases

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > case '_/':
    > case 'xx':
    These are out of bounds for things enclosed in single quotes.

    It would be useful if you showed us some example command lines, so we can tell you what the best way to handle them would be.
    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.

  5. #5
    Registered User
    Join Date
    Jan 2023
    Posts
    3
    This is how the execution should look like :

    $ calc.exe 7 + 8
    15
    $ calc.exe 25 m 100
    62.5
    $ calc.exe 2 xx 4
    16

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    What does m stand for?

    A switch only works for integers, so it can work for single characters.
    But for strings of characters you need to use if/else if and strcmp.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WIP: Command line calculator. New to C
    By ImageJPEG in forum C Programming
    Replies: 17
    Last Post: 09-28-2017, 12:38 PM
  2. comand line argument
    By Harith in forum C Programming
    Replies: 6
    Last Post: 09-02-2013, 08:26 AM
  3. Command-line Calculator Error:
    By mmario10 in forum C Programming
    Replies: 2
    Last Post: 10-31-2011, 04:29 AM
  4. comand read, write. use in socket
    By draxent in forum C Programming
    Replies: 5
    Last Post: 08-10-2011, 03:14 AM
  5. proxy comand line in c
    By enlinux in forum Linux Programming
    Replies: 2
    Last Post: 07-18-2003, 11:21 PM

Tags for this Thread