-
C string problem
Hello guys i just started learning C 1 week ago i m trying to do a program for guessing i done it but i wanna try make it like if user put 3 times wrong program will close i did but i have 2 errors in them >>thanks in advanced for your help
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char szKey[100]="mangos";
char inPut[100];
do{
printf("Please Enter your favorite fruit: ");
fgets (inPut, 100, stdin);
}
while(strcmp(szKey,inPut));
puts("Correct answer");
if(inPut==3)
exit(0);}return 0;}
-
You will need to remove the newline that comes along in fgets().
--
Mats
-
yes thanks didnt notice that i fixed that also exit function didnt need a brace after it but problem is it keeps exuting even after 3rd line how can i make at at 3rd question it will exit ?
genie
-
You'll need a counter variable to keep track of how many times you've gone through the loop. Initialize it to 0 outside the loop and then increment it each time you go through the loop. You can alter the conditional part of the loop to include a check of this value (such that it will exit said loop once the count is 3) or you can put a check inside the loop after the increment step which calls break to exit the loop.