0

Good day. I was tasked to create a "list of programs" that will display all programs I created. At first I will the user for a password, and then if they input the incorrect password, the program will exit. That works fine, but the problem comes when they do input the correct password. I used switch case for this problem and had a while loop and another switch case inside the former switch case. The problem is, after inputting the correct password, C prints out the text but doesn't let me input anything, it just ends the program. Here is the code:

#include <stdio.h>
#include <string.h>
int main()
{ 
    char name [64], program, prog1;
    int password;
    
    printf("Good day! What is your name? ");
    scanf("%s", &name);
    printf("Good day, %s! Please input the password for this program. ", name);
    scanf("%i", &password);
    
    switch(password) {
        case 1234:
            system("CLS");
            printf("MENU OF PROGRAMS");
            printf("\n(1) BMI Calculator ");
            printf("\n(2) Program 2");
            printf("\n(3) Program 3");
            
            printf("Would you like to test my programs? Input Y or N: ");
            scanf("%c", &prog1);
            
            while (prog1=='Y') {
                
                printf("\nInput desired program: ");
                scanf("%i", &program);
                system("CLS");
                switch(program) {
                    case 1:
                        // Program 1
                    
                    case 2:
                        // Program 2
                    
                    case 3-10:
                        // Code

                    default: 
                        system("color 2");
                        printf("Thank you for trying out my menu of programs! :)");
                        return 0;
                }
        
    }
                    break;
                    
                    default:
                        printf("Incorrect Password.");
}
} 

The code starting from the while loop was working fine until I added the option for inputting the password, the only inputs and outputs for this code are the ones asking for name and the password, but after I input the password, it only outputs this and does not let me continue further into the program:

                MENU OF PROGRAMS

        (1) BMI Calculator
        (2) Program 2
        (3) Program 3

Would you like to test my programs? Input Y or N:
--------------------------------
Process exited after 5.908 seconds with return value 10
Press any key to continue . . .

Is there any workaround for this? So far the only functions we're allowed to use are those included in the code. Any help will be greatly appreciated, thank you so much!

Binary
  • 29
  • 4
  • 1
    Add a `printf("char = '%c'\n", prog1);` right after `scanf` to see what's going on. – dxiv Oct 25 '20 at 02:40
  • 2
    Try `scanf(" %c", &prog1);` to skip whitespace left in the buffer from the previous input. https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer and https://stackoverflow.com/questions/20306659/the-program-doesnt-stop-on-scanfc-ch-line-why for details – Retired Ninja Oct 25 '20 at 02:42
  • 1
    You MUST ***check the return*** of every user-input function used to determine if the input succeeded or failed BEFORE you use that value in your program. Recommended user-input is with `fgets()` into a sufficiently sized array and then using `sscanf()` to parse any value so that what is left in the input stream doesn't depend on the conversion specifier used and whether or not the next ignores leading whitespace. – David C. Rankin Oct 25 '20 at 05:02
  • 1
    `scanf("%s", ...` without a *field-width* modifier is no safer than `gets()`. See [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/q/1694036/3422102) `scanf("%s", &name);` will happily attempt to store an UNLIMITED number of characters at ***the address of*** `name` (which is wrong, remove the `'&'`, `name` is already a pointer) when only `63` characters are available. Better `if (scanf("%63s", name) != 1) { /* handle manual EOF */ }`. – David C. Rankin Oct 25 '20 at 05:05

2 Answers2

1

The scanf() usually terminates with a enter. When you give enter after entering the correct password, the last character is '\n'. The next scanf() for getting the input 'Y' or 'N' will get the '\n' and gets terminated. So in this case there is no values saved in prog1. When the while(prog1=='Y') is executed, the condition fails and the program is terminated.

In order to avoid this you can use a single space before \c in scanf(). scanf(" \c",&prog1);

prithvi
  • 51
  • 1
  • 1
    What's wrong with `scanf("%s", &name);`?? Isn't `name` already a pointer due to [C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3)](http://port70.net/~nsz/c/c11/n1570.html#6.3.2.1p3)?? – David C. Rankin Oct 25 '20 at 05:07
0

It should be %c in scanf() with the variable program or change the variable type of the variable program to int.

Hans
  • 5
  • 4