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?