Mwigs
Mwigs

Reputation: 231

Scanf_s keeps returning \0 instead of input

I've become stuck on a, in my mind, pretty simple task. I have written a program that picks a random number, and makes you guess which one it is and exit the program at the end if the characters 'n' or 'N' is entered. The code is as follows:

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

int input_switcher(int input, int answer)
{
    if (input == answer)    {return 1;} 
    else if(input < answer) {return 2;}
    else if(input > answer) {return 3;}
    else                    {return 0;}
}

int main(void)
{
    int input, answer, tries;
    char keepGoing = 'J';
    srand((int)time(0));

    while(42)
    {
        input = 0, tries = 0;
        answer = 1;//rand()%100+1;

        printf("Jag \"t\x84nker\" p\x86 ett tal mellan 1 och 100, gissa vilket!\n");

            while(42)
            {
                printf("Gissning: ");
                scanf_s("%d", &input);

                switch (input_switcher(input, answer))
                {
                case 1:
                    printf("Grattis, det var r\x84tt!\n");
                    printf("Du gjorde %d f\x94rs\x94k.", tries);
                    break;
                case 2:
                    printf("Du gissade f\x94r l\x86gt, f\x94rs\x94k igen!\n");
                    tries++;
                    break;
                case 3:
                    printf("Du gissade f\x94r h\x94gt, f\x94rs\x94k igen!\n");
                    tries++;
                    break;
                default:
                break;
                }
                if(input == answer)     {break;}
            }
        printf("Ska vi spela en g\x86ng till(J/N)? ");
        scanf_s("%*c%c", &keepGoing);
        if(keepGoing == 'N' || keepGoing == 'n')        {return 0;}
        else if(keepGoing == 'J' || keepGoing == 'j')   {system("cls");}
    }   
    return 0;
}

The problem I'm having is that my last scanf_s gives me \0 instead of the character i enter. Have I identified the problem or am I wrong? What can I change to remedy this problem?

Upvotes: 2

Views: 385

Answers (2)

haccks
haccks

Reputation: 106012

Your last scanf_s receives \n and not \0. To eat up this \n change

printf("Ska vi spela en g\x86ng till(J/N)? ");
scanf_s("%*c%c", &keepGoing);

to

 printf("Ska vi spela en g\x86ng till(J/N)? ");
 scanf_s(" %c", &keepGoing);
          ^
          |
   space before %c specifier

It is working.
Output: On Win7 with GCC 4.7.1

enter image description here

Upvotes: 5

Gangadhar
Gangadhar

Reputation: 10516

scanf() is Just enough in this instead of scanf_s ==>

You can modify scanf in the two places

Like this:

scanf("%d", &input);  
scanf(" %c", &keepGoing); 
         ^  //use space here , to consume the leftover \n from previous scanf entry

And if you still face issue You can use fflush(stdin); before last scanf_s() In Windows this would works.

             fflush(stdin);  
             scanf("%c", &keepGoing);

Upvotes: 1

Related Questions