Hi. My school assignment is to write a program to help someone learn their time tables. It works as expected however when trying to replace a system("pause") at the end of the program with a getchar() alongside press enter instructions I find that that part of the code is skipped over and the program instantly returns zero after my printf "press enter to exit" function. Any help would be much appreciated thanks.
Sorry for the lack of comments but the variables are pretty obvious and its really a simple program.
(advice on the rand() would be nice too but I want to understand the lack of chance for user input on the getchar first)
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1,num2,answer,guess,menu,diff;
printf("Please select from the following...");
printf("\n1. Calculator\n2. Quiz\n0. Exit\n");
scanf("%i",&menu);
while(menu)
{
if(menu==1)
{
printf("\nEnter a number:");
scanf("%i",&num1);
printf("\nEnter another number:");
scanf("%i",&num2);
answer=num1*num2;
printf("\nWhat is %iX%i? ",num1,num2);
scanf("%i",&guess);
if (guess==answer)
{
printf("\nCORRECT");
}
else
{
printf("\nIncorrect, it is %i",answer);
diff=guess-answer;
printf("\nYou were off by %i",diff);
}
}
else if(menu==2)
{
num1=rand() % 12 + 1;
num2=rand() % 12 + 1;
printf("\n%i X %i =",num1,num2);
scanf("%i",&guess);
answer=num1*num2;
if (guess==answer)
{
printf("\nCORRECT");
}
else
{
printf("\nIncorrect, it is %i",answer);
diff=guess-answer;
printf("\nYou were off by %i",diff);
}
}
else
{
printf("\nInput Error, Please Try Again");
}
printf("\n\nPlease select from the following...");
printf("\n1. Calculator\n2. Quiz\n0. Exit\n");
scanf("%i",&menu);
}
printf("\nPress Enter to Exit\n");
getchar();
return 0;
}
p.s. also how to take the magnitude of a variable(i.e. "diff"). Square and Sqrt?