0

I need to find the sum, diff, etc for two numbers that user input. But program first displays the menu, after that ask a user to input those numbers and do the operation. After that, it should ask a user if want to calculate something again, and if they want did it want with the old numbers or new.

I'm a total beginner at C, so I need a help with a few thing. I got an error at Cases :

cfinal.c:49:14: warning: multi-character character constant [-Wmultichar]

And this one:

cfinal.c:49:14: warning: overflow converting case value to switch condition type (21075 to 83) [-Wswitch]

There is something wrong and I can't figure it out. And the second problem is that I need to ask the user if they want to repeat all over again and if they choose Yes, did they want to do it with the new numbers or numbers used in previously calculation. That's something I didn't even start, I don't know how. Thanks.

Code I've so far:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main (void)
{
    char choice;                //Define choice character type
    int first_number, second_number, firstSquare, secondSquare, firstQube, secondQube, cont;  //define intigers
    printf("Select operation you would like to do by typing shortcut:\n");
    printf("SU - sum\n");
    printf("DI - difference\n");
    printf("MU - multiplication\n");
    printf("RA - ratio\n");
    printf("SS - Sum of Squares\n");
    printf("SQ - Sum of Qubes\n");
    printf("RS - Sum of Squares\n");
    printf("RQ - Sum of Qubes\n");
    choice = (char)toupper(getchar());
    getchar ();
    printf("Enter the first number: ");
    scanf("%d", &first_number);
    getchar ();
    printf("Enter the second number:");
    scanf("%d", &second_number);
    getchar ();
    firstSquare = first_number * first_number;
    secondSquare = second_number * second_number;
    firstQube = first_number * first_number * first_number;
    secondQube = second_number * second_number * second_number;
    printf("\n");
    printf("Result is");
    switch(choice)
    {
        case 'SU':
            printf("%d + %d = %d", first_number, second_number, first_number + second_number);
            break;
        case 'DI':
            printf("%d - %d = %d", first_number, second_number, first_number - second_number);
            break;
        case 'MU':
            printf("%d * %d = %d", first_number, second_number, first_number * second_number);
            break;
        case 'SS':
            printf("%d * %d + %d * %d = %d", first_number, first_number, second_number, second_number);
            break;
        case 'SQ':
            printf("%d * %d * %d + %d * %d * %d = %d", first_number, first_number, first_number, second_number, second_number, second_number, firstQube + secondQube);
            break;
        case 'RS':
            printf("(%d * %d) / (%d * %d) = %d", first_number, first_number, second_number, second_number, firstSquare / secondSquare);
            break;
        case 'RQ':
            printf("(%d * %d * %d) / (%d * %d * %d) = %f", first_number, first_number, first_number, second_number, second_number, second_number, firstQube / secondQube);
            break;
        case 'RA':
            if (second_number == 0)
                fprintf(stderr, "The divisor can't be 0");
            else
            {
                printf("%d / %d = %f", first_number, second_number, (double)first_number / second_number);
            }
            break;
        default :
            fprintf(stderr, "Invalid entry");
            break;
    }
    printf("\n");
    for (;;)
    {
      printf("Continue (Type Y for yes or N for no)? ");
      cont = toupper(getchar());
      getchar ();
      if (cont == 'Y')
          return main(); // the difference.
      else if (cont == 'N')
          return EXIT_SUCCESS;
    }
}
Aaqib
  • 9,942
  • 4
  • 21
  • 30
jovicbg
  • 1,523
  • 8
  • 34
  • 74

1 Answers1

2

getchar returns a single character. if you want to use string 'xy', 'd99' etc you will have to read a string. How to read a line from the console in C?

Second you cannot switch on a string you need to do

if(strcmp(input,"SU")==0)
{
....
}
else if (strcmp(input, "DQ") == 0)
{
}
...
else
{
... error

}

pm100
  • 48,078
  • 23
  • 82
  • 145