1

I want the program to terminate as soon as the user enters a letter which isn't 'A' or 'B'. Is it possible to only print the default case for input 1 even if the user enters 'A' or 'B' for input 2? I tried the exit() function from stdlib.h but I can't seem to find a placement in the code that would work. I understand that a nested switch statement could also work but the full program takes in 8 inputs and I'd have to nest 8 times

So for example: Enter a value: C Enter a value: A Invalid letter for first input! Terminating program...

#include <stdio.h>

int main()
{
    char input1;
    printf("Enter a value: ");
    scanf(" %c", &input1);

    char input2;
    printf("Enter a value: ");
    scanf(" %c", &input 2);

    char A = 'A';
    char B = 'B';

    switch(input1){
        case 'A':
        printf("Your letter is A");
        break;

        case 'B':
        printf("Your letter is B");
        break;

        default: 
        printf("Invalid letter for first input! Terminating program...");
        break;
    }

    switch(input2){
        case 'A':
        printf("First letter of the alphabet");
        break;

        case 'B':
        printf("Second letter of the alphabet");
        break;

        default: 
        printf("Invalid letter for second input! Terminating program...")
        break;
    }

    return 0; 
}
kat
  • 23
  • 5
  • Why does putting the exit in your first default case not work? Just put it after your print if you need that output. Or restructure your code to verify inputs as they are entered and not after all are entered. – Andrew Monshizadeh Jan 26 '20 at 21:09
  • I ran the code and it prints out the second case even if the exit is in the first default case. The assignment requires us to structure the program so the user would have to enter all values before something is returned – kat Jan 26 '20 at 21:14
  • 1
    scanf is triggered only after press enter, read unbeffered is not so easy, you can use ncurses or https://stackoverflow.com/questions/1798511/how-to-avoid-pressing-enter-with-getchar – Ôrel Jan 26 '20 at 21:18

1 Answers1

0

If I correctly understood your question, this should work

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

int main()
{
    char input1;
    printf("Enter a value: ");
    scanf(" %c", &input1);

    char input2;
    printf("Enter a value: ");
    scanf(" %c", &input2);

    char A = 'A';
    char B = 'B';

    switch(input1){
        case 'A':
        printf("Your letter is A");
        break;

        case 'B':
        printf("Your letter is B");
        break;

        default: 
        printf("Invalid letter for first input! Terminating program...");
        exit(1);
    }

    switch(input2){
        case 'A':
        printf("First letter of the alphabet");
        break;

        case 'B':
        printf("Second letter of the alphabet");
        break;

        default: 
        printf("Invalid letter for second input! Terminating program...");
        exit(1);
    }

    return 0; 
}
maxrt
  • 19
  • 4
  • I would assume that the second switch default should also call exit(). That way any code following the switch would not be executed if the input was incorrect. Also, I would not use zero as the argument for exit() when there is an.error. – FredK Jan 26 '20 at 22:21
  • @maxrt `exit(1)` is non-portable. I would suggest `exit(EXIT_FAILURE)` – Ardent Coder Mar 27 '20 at 15:30