1 Attachment(s)
Weird 'Enter' key glitches
C program. IDE used Xcode v12.2
The problem I'm facing:
I need to press the 'Enter' key twice after typing the letter 'P' if I want to output the next sentence. I'm pretty sure that previously I just needed to press the 'Enter' key once in order to do so. What's surprising is that typing the letters 'S' or 'R' will only require pressing the 'Enter' key once to get the desired results. What could have possibly gone wrong?
Code:
void quiz(char name[])
{
int rounds = 0;
int highest = 0;
int lowest = INT_MAX;
float allScore = 0;
int avg = 0;
int i, j, g = 0;
struct struc test[MAX_TESTS];
srand((unsigned) time(NULL));
for (;;)
{
rounds++;
for (i = 0; i < MAX_TESTS; i++) // generate all questions
{
ctm_i(&test[i]);
for (j = 0; j < i; j++)
if (test[i].a == test[j].a && test[i].b == test[j].b && test[i].c == test[j].c)
//if question is already present
ctm_i(&test[i]); //then re-generate
}
char x;
x = getchar();
printf("\n Are you ready? Press Enter key to continue. ");
fflush(stdin);
while (x != '\n') {}
while (getchar() != '\n') {}
for (i = 1; i <= 5; i++)
{
printf(" *******************************************************************"
"**"
"***********\n");
printf(" ..................................................................."
".."
"...........\n");
}
// Take quiz
for (i = 0; i < MAX_TESTS; i++)
tcm_i(&test[i], i);
printf(" End\n\n");
bool done = false;
bool unsure = true;
bool showS = true;
while (unsure)
{
unsure = false;
puts("\n");
if (showS)
{
puts(" Enter 'S' to show results");
}
puts(" Enter 'P' to play another round");
puts(" Enter 'R' to return to main menu");
char choice;
printf(" ");
myread("%c", &choice);
if (choice == 'r' || choice == 'R')
{
done = true;
}
else
{
///////////////////////// Changes /////////////
g = 0;
// calculate total score for current round
for (i = 0; i < MAX_TESTS; i++)
{
g += test[i].grade; //add score of each question
}
allScore += g; //add current round's score to total
avg = allScore / rounds; //average of all rounds
if (g > highest)
{
highest = g;
}
if (g < lowest)
{
lowest = g;
}
if (choice == 'S' || choice == 's')
{
showS = false;
if (rounds == 1)
{
printf(" Final score: %d/100\n", g); //display round score
printf(" ******Player: %s ******\n", name);
}
else
{
printf(" Round %d score: %d/100\n", rounds, g); //display round score
printf(" Highest score: %d/100\n", highest);
printf(" Lowest score: %d/100\n", lowest);
printf(" Average score: %d/100\n", avg);
printf(" ******Player: %s ******\n", name);
}
unsure = true;
}
else if (choice == 'P' || choice == 'p')
{
/// nothing to be done here
//we will display next test
}
else
{
puts(" Invalid input!");
unsure = true;
}
////////////////////////////////////
}
}
if (done)
break;
}
}
Attachment 16268
Any help would be greatly appreciated.