0

First off, I am relatively new to C programming, and wanted to write a program that would be able to accept user input and then pass that on to a switch/case and return output based on the initial choice. I was able to do this while using the int data type, but I would like some help doing the same while taking input as a char.

#include <stdio.h>

double x = 1;
double y = 3;
char inputLet[1];

double chooseEquation(char notLet){
    char notNotLet = notLet;
    printf("notLet here is: %c \n", notLet);
    switch(notLet){
        case 'A':
            x += y;
            printf("Case1 reached! \n");
            break;
        case '2':
            x += -y;
            break;
        case '3':
            x -= y;
            break;
        case '4':
            x -= -y;
            break;
        case '5':
            x *= y;
            break;
        case '6':
            x *= -y;
            break;
        default :
        printf("defaulting! btw: %c \n", notNotLet);
        x = 0;
    }
    printf("x has been set? Here: %.2f\n", x);
    return x;
}

int main(){
    printf("Welcome, please pick a letter from A to F (uppercase for now) in order to choose an equation: \n");
    scanf(" %c", &inputLet);
    printf("The letter you chose is: %s \n", inputLet);
    double outputLet = chooseEquation(inputLet);
    printf("Your equation evaluated to: %.2f \n", outputLet);
    return 0;
}


Somehow, no matter the input, the character that the switch looks at becomes Q

However, if I replace this line: char inputLet[1];

with this line: char inputLet;

The program segmentation faults.

Any help would be much appreciated.

MrOverflow
  • 49
  • 6
  • `%c` is the specifier for a single `char`, `%s` is the specifier for a null-terminated `char` array. Don't mix them, they use different types – UnholySheep Sep 02 '20 at 07:28
  • And the line `double outputLet = chooseEquation(inputLet);` should at least give a compiler warning, since you are passing a `char*` where the function expects a `char` – UnholySheep Sep 02 '20 at 07:29
  • @UnholySheep Well, that's why I mentioned my edit where I removed the [1] which should have made it a single char datatype, which then results in the aforementioned segmentation fault. – MrOverflow Sep 02 '20 at 07:30
  • @UnholySheep Also, it does throw a compiler warning, but again matching my data types results in a segmentation fault which gets me nowhere – MrOverflow Sep 02 '20 at 07:32
  • 1
    The point being made to you is that `printf("The letter you chose is: %s \n", inputLet);` is wrong because `%s` requires a string. `inputLet` is a single character array which is not a string (as it is not a NULL terminated sequence). – kaylum Sep 02 '20 at 07:35
  • 1
    If you remove the `[1]` from the declaration then `printf("The letter you chose is: %s \n", inputLet);` should be complaining - because you have `%s` instead of `%c` here – UnholySheep Sep 02 '20 at 07:35
  • Alrighty, it seems that was the problem. I appreciate you guys pointing this out – MrOverflow Sep 02 '20 at 07:39

1 Answers1

3
printf("The letter you chose is: %s \n", inputLet);

When you use char inputLet then use %c. This is causing the error.

And also if it is a single character then just use char inputLet not a character array.

After changing it, I tested it locally and got this output for 'A'

Welcome, please pick a letter from A to F (uppercase for now) in order to choose an equation: 
A
HelloThe letter you chose is: A 
notLet here is: A 
Case1 reached! 
x has been set? Here: 4.00
Your equation evaluated to: 4.00 
rootkonda
  • 1,700
  • 1
  • 6
  • 11
  • Interesting that if scanf with %c it will only input the first character typed and will not append a null terminator. Learn something new every day :) – Den-Jason Sep 02 '20 at 09:44