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

int main()
{
        char letter;
        char answer[10];
        char affirmation[10] = "yes";

        do
        {
                printf("Enter a character: \n");
                scanf("%c",&letter);

                if(letter>=65 && letter<=90)
                {
                        printf("UPPERCASE! \n");
                }
                else if(letter>=97 && letter<=122)
                {
                        printf("LOWERCASE \n");
                }
                else
                {
                        printf("Not an Alphabet \n");
                }

                printf("Wanna try for another Character ? [yes/no] \n");
                scanf("%s",&answer);

        }
        while(strcmp(affirmation,answer) == 0);

        return 0;
}

I was trying to make a program which would tell if the character entered is uppercase or lowercase and then ask if the user wants to try again for another character. But it is not reiterating in the desired way.Sometimes it says "terminated with signal 11". Here it is.And sometimes it acts completely different. See here.What should be done in order to get desired results?

Raja
  • 1
  • 1
  • this will help fix your second problem https://stackoverflow.com/questions/29775323/scanf-function-seems-to-be-skipped-in-c – yano May 29 '21 at 03:54
  • change `scanf("%s",&answer);` --> `scanf("%s",answer);` https://www.geeksforgeeks.org/not-used-strings-scanf-function/ – yano May 29 '21 at 03:58
  • After those changes, unable to reproduce the segfault (and don't see how that could've happened to begin with unless you're fuzzing the input). – yano May 29 '21 at 04:05
  • 1
    `if(letter>='A' && letter<='Z')` is much much more readable. – David C. Rankin May 29 '21 at 04:51

1 Answers1

1

You should include a space before %c in scanf(" %c",&letter);. The space before %c is needed to skip the white space in buffer memory. Take a look at this post for more information. https://www.codesdope.com/discussion/why-are-you-using-a-space-before-c-in-scanf-c-ch/. Then you should also change scanf("%s",&answer); to scanf("%s",answer); because an array is already a pointer to its first element. Take a look at Reading a string with scanf.

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

int main()
{
        char letter;
        char answer[10];
        char affirmation[10] = "yes";

        do
        {
                printf("Enter a character: \n");
                scanf(" %c",&letter);

                if(letter>=65 && letter<=90)
                {
                        printf("UPPERCASE! \n");
                }
                else if(letter>=97 && letter<=122)
                {
                        printf("LOWERCASE \n");
                }
                else
                {
                        printf("Not an Alphabet \n");
                }

                printf("Wanna try for another Character ? [yes/no] \n");
                scanf("%s",answer);

        }
        while(strcmp(affirmation,answer) == 0);

        return 0;
}
  • 1
    Bonus points: `scanf("%9s",answer);` to ensure you do not overwrite your array bounds. When reading into an array with `"%s"` unless a *field-width* modifier is used, `scanf()` is no safer than `gets()` -- see [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/q/1694036/3422102) A citation to why answer is already a pointer is [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 May 29 '21 at 04:53