2

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?

Mwigs
  • 231
  • 1
  • 14
  • 1
    You may want to explain your reasoning for why you chose that specific format specifier, `"%*c%c"` in the one-place you suspect as the source of your problem. – WhozCraig Sep 16 '13 at 19:04
  • @WhozCraig The `%*c` is there to consume the leftover \n from previous scanf entry. – Mwigs Sep 16 '13 at 19:14
  • @MarcusWigert; Use an space before `%c` specifier to consume `\n`. – haccks Sep 16 '13 at 19:38

2 Answers2

5

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

haccks
  • 104,019
  • 25
  • 176
  • 264
1

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);
Gangadhar
  • 10,248
  • 3
  • 31
  • 50