Question

New to C still learning. The program should start the first time with out needing to be asked to do anything. Then it prompts the user to continue with a "Y/N". I keep errors could anyone tell me why it doesn't work I don't know what to do with the errors that I get from it.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h> 

void theQnA(char charIn);


int main(void)
{
    int answerint = 0;
    char charIn;
    char anwser;


    printf("Enter a character to be examined: ");
    scanf("%c", &charIn);
    theQnA(charIn);
    while (answerint == 0)
    {
        printf("Would you like to run it again? Y/N\n");
        scanf("%c", &anwser);

        if (anwser == 'y')
        {
            printf("Enter in another character buddy\n");
            scanf("%c", &charIn);
            theQnA(charIn);
        }
        else (anwser != 'y')
        {
            answerint = (answerint--);
        }
    }
    printf("Goodbye\n");

    return 0;
}


void theQnA(char charIn)
{
    if (islower(charIn))
        printf("You have entered in a lower case letter dude\n");
    else if (isdigit(charIn))
        printf("You enterd in a num man\n");
    else if (isupper(charIn))
        printf("Its upper case man\n");
    else if (ispunct(charIn))
        printf("You entered in a punctuation!\n");
    else if (isspace(charIn))
        printf("You enterd in a whitespace dude\n");
    else
        printf("control char/n");
    return;
}
Was it helpful?

Solution

You have else (anwser != 'y'). It should be else if (anwser != 'y'), or better yet just else. The prompt Would you like to run it again? Y/N will also be printed twice because of how your loop is structured. You have quite a few mistakes, but here's some advice on your loop.

You can use your anwser variable in your while condition. answerint is unnecessary. Also, when you type a character and press enter, scanf (with %c) will extract the character but leave the newline in the buffer. That means the next call to scanf will return a newline, which will make it appear as if your program is skipping your input statements. To fix this, add a space before the %c in your call:

scanf(" %c", &charIn);

Your logic was also a bit out of place. Look at how this example is structured.

printf("Enter a character to be examined: ");
scanf(" %c", &charIn);
theQnA(charIn);

printf("Would you like to run it again? y/n\n");
scanf(" %c", &anwser);
while (anwser == 'y')
{
    printf("Enter in another character buddy: ");
    scanf(" %c", &charIn);
    theQnA(charIn);

    printf("Would you like to run it again? y/n\n");
    scanf(" %c", &anwser);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top