Hi,
Please consider the following SHORT code:
If you run the program the first time around and type in 'yes' when asked, it will go back and do what it's suppose to... The second time it gets to the switch statement NO MATTER what you type - the switch goes do default!
I've run the code in Xcode 4 under gdb and looked at the answers array the first and the consecutive times around... At first all seems normal, but then the array is filled up with jumble!
Does the fgets() fail? Does the array needs to be cleared or something?
Thanks in advance!
~confuded
Please consider the following SHORT code:
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#define kCups 3
int RandomNumber(int l_limit, int u_limit);
void ClearArray(bool array[], int size);
int main (int argc, const char * argv[]){
//initialise array of cups (boolean)
bool cups[kCups];
//initialise menu loop
bool exit = false;
do{
printf("There are %d cups. One of them contains the ball.\n"
"Which cup contains the ball?\n", kCups);
//play again?
bool validAnswer = false;
do{
printf("Play the game again?\n");
char answer[4];
fgets(answer, 4, stdin);
switch(answer[0]){
case 'y': case 'Y':
validAnswer = true;
break;
case 'n': case 'N':
validAnswer = true;
exit = true;
break;
default:
printf("Please enter a valid answer!\n");
}
}while(!validAnswer);
}while(!exit);
return 0;
}
void ClearArray(bool array[], int size){
for(int i = 0; i < size; i++)
array[i] = false;
}
If you run the program the first time around and type in 'yes' when asked, it will go back and do what it's suppose to... The second time it gets to the switch statement NO MATTER what you type - the switch goes do default!
I've run the code in Xcode 4 under gdb and looked at the answers array the first and the consecutive times around... At first all seems normal, but then the array is filled up with jumble!
Does the fgets() fail? Does the array needs to be cleared or something?
Thanks in advance!
~confuded
Last edited: