0

May I need help in sorting this out? Basically, I'm tasked to play around with ctype header files. I get input from the user and then I give the output depending on the input provided.

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

int main(void)
{
    char savedPassword[] = "123456";
    char username[100], password[100], charInput, decision;

    printf("Username: ");
    fgets(username, sizeof(username)/sizeof(username[0]), stdin);
    printf("Password: ");
    scanf(" %s", &password);

    if (strcmp(savedPassword, password) == 0)
    {   
        printf("Welcome, %s\n", username);

        do
        {
            printf("Enter any character: ");
            scanf(" %c", &charInput);

            if(isalpha(charInput))
            {
                if(isupper(charInput))
                {
                    printf("%c is a capital letter.\n", charInput);
                } else {
                    printf("%c is a small letter.\n", charInput);
                }
            } else if (isdigit(charInput))
            {
                printf("%c is a digit.\n", charInput);
            } else if (ispunct(charInput))
            {
                printf("%c is a punctuation.\n", charInput);
            } else if (isspace(charInput))
            {
                    printf("%c is a space.\n", charInput);  
            }

            printf("Do you want to exit (Y/N)? ");
            scanf(" %c", &decision);

            if (toupper(decision) != 'Y' && toupper(decision) != 'N')
            {
                printf("Please re-enter the correct response.\n");
                printf("Do you want to exit (Y/N)? ");
                scanf(" %c", &decision);
            }
        }while(toupper(decision) != 'Y');

        printf("Thank you for using the app!");

    } else {
        printf("Wrong password! Try again!");
    }   

    return 0;
}

I couldn't make isspace() work because it seems like scanf() doesn't detect the whitespace anymore as I have used one before the placeholder.

Thank you!

ddp17
  • 41
  • 4
  • 1
    Yes, because you used `scanf(" %c", &charInput);`, scanf will never read a space character into `charInput`. That's normally what you want. If you want it to read a space, you can use `scanf("%c", &charInput);` — but then the problem is you'll probably end up reading the `\n` from the line where the user typed the password. (This is unfortunately the kind of tangle you *always* get into when you try to use `scanf` for anything at all complicated.) – Steve Summit Jan 17 '23 at 18:02
  • Don't mix different input functions like `fgets` and `scanf`. Input everything with `fgets` and see [here](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) how to remove the newline that `username` retains. – Weather Vane Jan 17 '23 at 18:09
  • Also note that ```%s``` expects a ```char *```, not ```char(*)[]```. – Harith Jan 17 '23 at 18:22
  • 1
    1) Stop using ```scanf```. 2) Make a call to ```getchar()``` to catch the newline or call it in a loop to flush the input buffer. – Harith Jan 17 '23 at 18:28

0 Answers0