Thread: Math Quiz program

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    10

    Math Quiz program

    I am programming a math quiz program for the college assignment below are a part of my code, the question is if user key in a char for the answer the program goes wrong, how to detect if the user keyed in char so i can send a message to user and say "Please enter a digit"

    Code:
    do    {
            no1 = rand() % 50 + 1;
            no2 = rand() % 50 + 1;
            pcAns = no1 + no2;
    
    
            printf("%d + %d = ", no1, no2);
            scanf("%d", &userAns);
             
            if (pcAns == userAns)
            {
                printf("Congratulation, your have entered the correct answer!!\n");
                mark = mark + 10;
            }
            else
                printf("Sorry the correct answer is %d \n", pcAns);
    
    
            count++;
        } while (count <= 10);

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    To do that, you need to check the return value of scanf. It returns the number of successfully read items (or EOF).

    In your case, if scanf returns something other than 1, you need to clear the remaining input and ask for a digit.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    10
    Quote Originally Posted by GReaper View Post
    To do that, you need to check the return value of scanf. It returns the number of successfully read items (or EOF).

    In your case, if scanf returns something other than 1, you need to clear the remaining input and ask for a digit.
    i tried this its not working properly tho

    Code:
    do    {
            no1 = rand() % 50 + 1;
            no2 = rand() % 50 + 1;
            pcAns = no1 + no2;
            test:
            printf("%d + %d = ", no1, no2);
            scanf("%d", &userAns);
             
            if (scanf("%d", &userAns) == 1)
            {
                if (userAns == pcAns)
                {
                    printf("Congratulation, your have entered the correct answer!!\n");
                    mark = mark + 10;
                }
                else
                    printf("Sorry the correct answer is %d \n", pcAns);
            }
            else if (scanf("%d", &userAns) != 1)
            {
                printf("Please enter yur answer in digit form");
                goto test;
            }
            count++;
        } while (count <= 10);

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > else if (scanf("%d", &userAns) != 1)
    This should be just an else, as in
    Code:
    else {
        int ch;
        while ( (ch=getchar()) != '\n' && ch != EOF );  // throw away the line
        printf("Please enter yur answer in digit form");
    }
    Also, you only call scanf ONCE to read a number, not three times as in your code.

    > goto test;
    Don't use goto's to make a while loop.
    Code:
    do {
      // prompt etc
    } while ( userAns != pcAns );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2017
    Posts
    10
    Quote Originally Posted by Salem View Post
    > else if (scanf("%d", &userAns) != 1)
    This should be just an else, as in
    Code:
    else {
        int ch;
        while ( (ch=getchar()) != '\n' && ch != EOF );  // throw away the line
        printf("Please enter yur answer in digit form");
    }
    Also, you only call scanf ONCE to read a number, not three times as in your code.

    > goto test;
    Don't use goto's to make a while loop.
    Code:
    do {
      // prompt etc
    } while ( userAns != pcAns );
    initially my code is this
    Code:
    do
    	{
    		no1 = rand() % 51 + 50;
    		no2 = rand() % 51 + 50;
    		pcAns = no1 + no2;
    
    
    		printf("%d + %d = ", no1, no2);
    		scanf("%d", &userAns);
    
    
    		if (pcAns == userAns)
    		{
    			printf("Congratulation, your have entered the correct answer!!\n");
    			mark = mark + 10;
    		}
    		else
    			printf("Sorry the correct answer is %d \n", pcAns);
    
    
    		count++;
    	} while (count <= 10);
    everything runs fine, but if user enter an alpha the program goes wrong how should i fix that

  6. #6
    Registered User
    Join Date
    Aug 2017
    Posts
    10
    i need to test wether the user entered digit or an alpha

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Like this
    Code:
    int scanStatus = scanf("%d", &userAns);
    if ( scanStatus == 1 ) {
      // check userAns
    } else if ( scanStatus == 0 ) {
      // get rid of the junk characters
    } else if ( scanStatus == EOF ) {
      // user has lost interest in your quiz and has quit
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Aug 2017
    Posts
    10
    owh so we need to declare snother variables to store and see wether the scan status is 1(yes) or 0(no)?

  9. #9
    Registered User
    Join Date
    Aug 2017
    Posts
    10
    Quote Originally Posted by Salem View Post
    Like this
    Code:
    int scanStatus = scanf("%d", &userAns);
    if ( scanStatus == 1 ) {
      // check userAns
    } else if ( scanStatus == 0 ) {
      // get rid of the junk characters
    } else if ( scanStatus == EOF ) {
      // user has lost interest in your quiz and has quit
    }
    Code:
    do
    	{
    		no1 = rand() % 50 + 1;
    		no2 = rand() % 50 + 1;
    		pcAns = no1 + no2;
    		printf("%d + %d = ", no1, no2);
    		scanf("%d", &userAns);
    		 
    		if (scanStatus == 1)
    		{
    			if (userAns == pcAns)
    			{
    				printf("Congratulation, your have entered the correct answer!!\n");
    				mark = mark + 10;
    			}
    			else
    				printf("Sorry the correct answer is %d \n", pcAns);
    		}
    		else if (scanStatus == 0)
    		{
    			printf("Please enter yur answer in digit form");
    		}
    		count++;
    	} while (count <= 10);
    i changed the code to this but the output is
    Please enter 1, 2, 3 or 41
    You have selected ADDITION
    Please select a level of difficulty: 1-Beginner, 2-Intermediate, or 3-Advance
    1
    1
    ADDITION BEGINNER SELECTED
    Please answer the 10 quetions given
    16 + 50 = a
    Sorry the correct answer is 66
    50 + 22 = Sorry the correct answer is 72
    11 + 28 = Sorry the correct answer is 39
    13 + 40 = Sorry the correct answer is 53
    30 + 40 = Sorry the correct answer is 70
    44 + 2 = Sorry the correct answer is 46
    23 + 25 = Sorry the correct answer is 48
    3 + 25 = Sorry the correct answer is 28
    34 + 10 = Sorry the correct answer is 44
    26 + 22 = Sorry the correct answer is 48
    YOUR TOTAL SOCORE IS 0/100
    I am sorry, you does not perform well in this level, whould you like to restart the level? 1(yes)/2(no)
    Press any key to continue . . .

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You forgot to add this line!

    while ( (ch=getchar()) != '\n' && ch != EOF ); // throw away the line
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Aug 2017
    Posts
    10
    add at? and whats this line for?

  12. #12
    Registered User
    Join Date
    Aug 2017
    Posts
    10
    i added it infinite loop happens

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    One can only guess as to what you've done wrong.
    Post your latest code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Aug 2017
    Posts
    10
    Code:
    	do
    	{
    		no1 = rand() % 50 + 1;
    		no2 = rand() % 50 + 1;
    		pcAns = no1 + no2;
    		printf("%d + %d = ", no1, no2);
    		scanf("%d", &userAns);
    		 
    		if (scanStatus == 1)
    		{
    			if (userAns == pcAns)
    			{
    				printf("Congratulation, your have entered the correct answer!!\n");
    				mark = mark + 10;
    			}
    			else
    			{
    				printf("Sorry the correct answer is %d \n", pcAns);
    			}
    		}
    		else if (scanStatus == 0)
    		{	
    			int ch;
    			while ((ch = getchar()) != '\n' && ch != EOF);
    			printf("Please enter yur answer in digit form\n");
    		}
    		count++;
    	} while (count <= 10);
    i change something and the infinite loop disappear here is the output
    Please enter 1, 2, 3 or 4
    1
    You have selected ADDITION
    Please select a level of difficulty: 1-Beginner, 2-Intermediate, or 3-Advance
    1 //i ned to type the choice here twice in order to sleect
    1
    ADDITION BEGINNER SELECTED
    Please answer the 10 quetions given
    23 + 17 = a //after i typed 'a' all of the other questions just pop out
    Sorry the correct answer is 40
    6 + 9 = Sorry the correct answer is 15
    20 + 10 = Sorry the correct answer is 30
    4 + 26 = Sorry the correct answer is 30
    48 + 40 = Sorry the correct answer is 88
    5 + 14 = Sorry the correct answer is 19
    47 + 5 = Sorry the correct answer is 52
    43 + 14 = Sorry the correct answer is 57
    15 + 31 = Sorry the correct answer is 46
    5 + 9 = Sorry the correct answer is 14
    YOUR TOTAL SOCORE IS 0/100
    I am sorry, you does not perform well in this level, whould you like to restart the level? 1(yes)/2(no)
    Press any key to continue . . .

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You need to pay close attention to the detail.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ( ) {
      int mark = 0;
      int count = 0;
      do
      {
          int no1 = rand() % 50 + 1;
          int no2 = rand() % 50 + 1;
          int pcAns = no1 + no2;
          printf("%d + %d = ", no1, no2);
          int userAns;
          int scanStatus = scanf("%d", &userAns); //!! yes, you forgot this assignment
    
          if (scanStatus == 1)
          {
              if (userAns == pcAns)
              {
                  printf("Congratulation, your have entered the correct answer!!\n");
                  mark = mark + 10;
              }
              else
              {
                  printf("Sorry the correct answer is %d \n", pcAns);
              }
          }
          else if (scanStatus == 0)
          {
              int ch;
              while ((ch = getchar()) != '\n' && ch != EOF);
              printf("Please enter yur answer in digit form\n");
          }
          count++;
      } while (count <= 10);
      printf("You scored mark %d in %d attempts\n", mark, count);
    }

    Results
    Code:
    $ gcc bar.c
    $ ./a.out 
    34 + 37 = 55
    Sorry the correct answer is 71 
    28 + 16 = a
    Please enter yur answer in digit form
    44 + 36 = a
    Please enter yur answer in digit form
    37 + 43 = 80
    Congratulation, your have entered the correct answer!!
    50 + 22 = 72
    Congratulation, your have entered the correct answer!!
    13 + 28 = 41
    Congratulation, your have entered the correct answer!!
    41 + 10 = 51
    Congratulation, your have entered the correct answer!!
    14 + 27 = 38
    Sorry the correct answer is 41 
    41 + 27 = 68
    Congratulation, your have entered the correct answer!!
    23 + 37 = 60
    Congratulation, your have entered the correct answer!!
    12 + 19 = 31
    Congratulation, your have entered the correct answer!!
    You scored mark 70 in 11 attempts
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 25
    Last Post: 07-28-2017, 04:47 PM
  2. Replies: 0
    Last Post: 07-24-2017, 07:57 AM
  3. quiz program
    By lmanukyan in forum C Programming
    Replies: 6
    Last Post: 12-22-2015, 08:50 AM
  4. Math Quiz Program Help
    By HystericalFool in forum C Programming
    Replies: 5
    Last Post: 11-17-2014, 12:00 AM
  5. making a math quiz program
    By mackieinva in forum C Programming
    Replies: 10
    Last Post: 09-17-2007, 03:38 PM

Tags for this Thread