I was wondering if there is anyway to make user press a button or type something to end it, whenever he wants.
Yes, there is. But first although scanf()
is a good way to get user input in a console application, such as in your example code, care should be taken to avoid buffer overflows, unwanted white-space, etc. The following links address these and other related issues.
Interesting discussions about sscanf(...) and its format strings:
And regarding a good method for console user input:
Suggestion:
Use a simple character value test in a forever loop. This snippet illustrates the essential elements of one technique that can be used during user input to leave a loop, or stay in:
int main()
{
char c[10];
char go[10];
//Place the rest of your variable declarations and initializations here
for(;;)
{
c[0]=0;
//...
//The bulk of your code here...
//...
//Place this section at the bottom of your questions:
printf("enter q to exit, or <enter> to continue\n");
fgets(c, sizeof(c), stdin);//reads string input from stdin
sscanf(c, " %9s", go);//string 'c' is evaluated according to the
//contents of the format string: " %9s"
//and parsed into the buffer 'go'.
//
//Note the space in the format string just
//prior to %9s. It causes any white space,
//including the newline character, \n, to
//be consumed, effectively removing it from
//being written into the 'go' buffer.
//
//The '9' in " %9s" prevents user input beyond
//9 characters to be read into the buffer,
//thus preventing buffer overflow
//and allows room for NULL termination.
if(strstr(go, "q")) break;//if 'q' is in string 'go' exit loop
//...
}
return 0;
}
A complete version of your code, based on your original construct, primarily using sequential flow
and if(.){...}else(.){...}
statements (including edits demonstrating methods discussed in links) is below. The execution flow is improved over original by using concepts discussed in links. It prompts user for test, for answers to questions, and finally offers option to exit.
Note, these edits offer user option to exit only at the end of a test. There is a version at the bottom that shows other construct options, including ability to leave at any point in the program.
Note the comments to indicate where changes have been made:
int main()
{
char c[10]; ///added
char go[10]; ///added
int a, ep;
int score;
score = 0;
for(;;) ///added
{ ///added
printf("Choose one\n");
printf(" 1. Athletics\t 2. History\t\n 3. Internet\t 4. Greek Mythologyn\n");
printf("Pick: ");
fgets(c, sizeof(c), stdin); //edited
sscanf(c, " %d",&ep); //edited
if (ep==1)
{
printf("\n");
printf("1. Ποιος διεθνής Έλληνας σκόραρε πάνω από ένα γκολ στο Μουντιάλ 2014;\n");
printf(" 1. Παπασταθόπουλος\n 2. Σάμαρης\n 3. Σαμαράς\n 4. Κανένας\n");
printf("Answer: " );
fgets(c, sizeof(c), stdin); //edited
sscanf(c, " %d",&a); //edited
if (a==4)
{
printf("Correct!!!\n");
score = score +1;
}
else
{
printf("Wrong\n");
score = score -1;
}
printf("Your score is: %d\n\n\n",score);
printf("2. Ποιος κέρδισε τον δεύτερο απο τους πέντε τελικούς της σειράς για την Α1 τη\nσεζόν 2013-14;\n");
printf(" 1. Ολυμπιακός\n 2. Παναθηναϊκός\n");
printf("Answer: " );
fgets(c, sizeof(c), stdin); //edited
sscanf(c, " %d",&a); //edited
if (a==1)
{
printf("Correct!!!\n");
score = score +1;
}
else
{
printf("Wrong\n");
score = score -1;
}
printf("Your score is: %d\n\n\n",score);
} ///added
printf("enter q to exit, or c to continue\n"); //added
fgets(c, sizeof(c), stdin); //added
sscanf(c, " %9s", go); //added - note space
//in format string
//to consume \n
//character if there
if(strstr(go, "q")) break; //added
}
return 0;
}
Alternate constructs:
The C switch() {...}; statement and ternary operator. constructs are used as an option for improved readability.
int main()
{
char c[10];
char go[10];
int a, ep;
int score;
score = 0;
for(;;)
{
printf("Categories:\n\n");
printf(" 1. Athletics\t 2. History\t\n 3. Internet\t 4. Greek Mythology\n\n");
printf("Choose a category (or 'q' to exit) : ");
fgets(c, sizeof(c), stdin); //edited
(isalpha(c[0])) ?
(sscanf(c, " %9s",go), ep=0) :
(sscanf(c, " %d",&ep), go[0]=0); //using ternary operator -> ?:
if(strstr(go, "q")) break;
switch(ep) {
case 1:
// questions for First category
printf("Make selection: (or 'q' to exit)\n");
printf("1. Ποιος διεθνής Έλληνας σκόραρε πάνω από ένα γκολ στο Μουντιάλ 2014;\n");
printf(" 1. Παπασταθόπουλος\n 2. Σάμαρης\n 3. Σαμαράς\n 4. Κανένας\n");
printf("Answer: " );
fgets(c, sizeof(c), stdin); //edited
(isalpha(c[0])) ?
(sscanf(c, " %9s",go), a=0) :
(sscanf(c, " %d",&a), go[0]=0); //using ternary operator -> ?:
if(strstr(go, "q")) break;
if (a==4)
{
printf("Correct!!!\n");
score = score +1;
}
else
{
printf("Wrong\n");
score = score -1;
}
printf("Your score is: %d\n\n\n",score);
printf("2. Ποιος κέρδισε τον δεύτερο απο τους πέντε τελικούς της σειράς για την Α1 τη\nσεζόν 2013-14;\n");
printf(" 1. Ολυμπιακός\n 2. Παναθηναϊκός\n");
printf("Answer: " );
fgets(c, sizeof(c), stdin); //edited
(isalpha(c[0])) ?
(sscanf(c, " %9s",go), a=0) :
(sscanf(c, " %d",&a), go[0]=0); //using ternary operator -> ?:
if(strstr(go, "q")) break;
if (a==1)
{
printf("Correct!!!\n");
score = score +1;
}
else
{
printf("Wrong\n");
score = score -1;
}
printf("Your score is: %d\n\n\n",score);
break;
case 2:
// questions for second category
break;
case 3:
// questions for third category
break;
case 4:
// questions for forth category
break;
default:
printf("Wrong selection. Try again. (1 - 4)\n");
break;
}
printf("\n\nenter q to exit, or c to continue\n");
fgets(c, sizeof(c), stdin);
sscanf(c, " %9s", go);
if(strstr(go, "q")) break;
}
return 0;
}