2

I am building a quiz project on C. My question is, can I use a command or use something to make the user decide if he wants to keep answering questions or not. For example here is a part of my code. And i would like to make user able to press "0" and the program stops whenever user press that button or type it. I was thinking to make with with loop but I was wondering if there is anyway to make user press a button or type something to end it, whenever he wants. Thansk for your time and sorry for my english and for the confusion.

#include <stdio.h> 



int main()
{
int a, ep;
int score;
score = 0;

printf("Choose one\n");
printf(" 1. Athletics\t 2. History\t\n 3. Internet\t 4. Greek Mythologyn\n");
printf("Pick: ");
scanf("%d",&ep);

if (ep==1) {
printf("\n");
printf("1. Ποιος διεθνής Έλληνας σκόραρε πάνω από ένα γκολ στο Μουντιάλ 2014;\n");
printf("  1. Παπασταθόπουλος\n  2. Σάμαρης\n  3. Σαμαράς\n  4. Κανένας\n");

printf("Answer: " );
scanf("%d",&a);

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: " );
scanf("%d",&a);
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);
Jens
  • 69,818
  • 15
  • 125
  • 179
pronoobgr
  • 77
  • 3
  • Do you want the user to stop answering question any time they want while they are answering a question? – Manav Dubey Mar 17 '17 at 22:23
  • 1
    Without creating something like an event generator and handler in a separate thread, you will have to live with sequential user prompts and responses to offer options to exit, or stop testing. – ryyker Mar 17 '17 at 23:46
  • The formatting makes it difficult to see, but the example code you listed is not complete. It will not compile. But there is a completed version (with the exception of wide characters) in an answer below. – ryyker Mar 17 '17 at 23:47
  • @narusin the program got like 20 questions. the user may want to stop at the 10th. I want to make him able to stop whenever he wants. It's a quiz game. – pronoobgr Mar 18 '17 at 00:44

2 Answers2

2

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;
}
Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87
2

A simple thing you can use is the ktbit function. That function checks if the user clicked a button or not.

Example:

int direction = 1; char control;
while (1)
{
    if(kbhit()){
       control = getchar(); 
       switch (control){
              case 'x': return 0;
       }
    }
}

Also in the beginnig, you need to add #include <conio.h>

Manav Dubey
  • 780
  • 11
  • 26