Write a simple Mathematics quiz application which has six selections as below:
1) To register number of participants and name
2) To set level of difficulty (level: Beginner or Advanced)
3) To start the quiz (default: Beginner level)
4) To display the scores
5) To show ranking
6) To quit

Your program has to ask the user to key in the following details:
i) An integer (from 1 to 6) representing the program menu selection options.
ii) An integer (from 1 to 5) representing number of participants (maximum 5 participants).
iii) A character representing the level of difficulty: ‘B’ for Beginner; ‘A’ for Advanced.
a. Beginner level: Participants are required to answer as many questions as possible in 60 seconds. Questions involving addition, subtraction or multiplication operation are chosen randomly. The two operands in this level have to be set within 1 to 50 inclusive.
b. Advanced level: Participants are required to answer as many questions as possible in 60 seconds. Questions involving square or square root operation are chosen randomly. The operand for questions involving square ranges from 1 to 30 inclusive. The operand for questions involving square root ranges from 1 to 900 inclusive and must be a perfect square number.
iv) A character (any key) to start the quiz
v) A string representing the response to the quiz question.
User can either key in the answer for the question or key in ‘n’ to skip the question. If an invalid response is detected, user will be asked to re-enter the string until it is valid. The answer for the question will then be converted from string to integer so that it can be compared with the actual answer. Each correct answer gives the participant 4 marks. 2 marks will be deducted for each wrong answer. 1 mark will be deducted for each skipped question.
The scores analysis for the participants will then be tabulated and the participants will be ranked from the highest score to the lowest score.

• Loop your program so that the user will be asked to choose from the selection menu above after completing each selection task. Stop the loop only when the user select option 6 to quit.
• When the user quits the loop, display “Bye!”
• Your program must contain at least 8 functions excluding the main function.
• Do not use any global variables.
• Include as well necessary input validations/ error checking in the program.

so basically this is the question. Im taking this step by step and had did a switch loop for the menu choices but the problem is i dont know how to loop it so the user will be asked to choose from the selection menu above after completing each selection task.
and
btw, a simple guide on how to continue would be a great help. I will show my work after i get the general idea of how the whole program should be written.
Thanks in advance for the help.

Recommended Answers

All 7 Replies

Do you not have any code to show? We are not allowed to do homework FOR you.. we can help but not do it.

i dont know how to loop it so the user will be asked to choose from the selection menu above after completing each selection task

the answer(s) is/are here:

• Loop your program so that the user will be asked to choose from the selection menu above after completing each selection task. Stop the loop only when the user select option 6 to quit.
• When the user quits the loop, display “Bye!”

One approach is to use a sentinel variable that is set to true before the loop containing the switch statement is started. Then when the user selects option 6 from the menu the swtich statement for case 6 will change the sentinel variable to false and display the word "Bye!".

#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include<string.h>
int main ()
{
    int option=0;
    int y,x,ans,sum=0,f,correct=0,wrong=0,skipped=0;
    int score=0;
    char stringname_1[80];
    char stringname_2[80];
    char stringname_3[80];
    char stringname_4[80];
    char stringname_5[80];
    char diff,B=0,A=0,n=0;
    int num1,num2,num3;
    num1=rand()%(50-1)+1;
    num2=rand()%(50-1)+1;
    num3=rand()%(30-1)+1;
    srand(time(NULL));  
    while(option!=6)
{
    printf("Welcome to Mathematics Quiz\n\n");
    printf("Menu\n");
    printf("-----\n");
    printf("1. To register number of participants and names\n");
    printf("2. To set level of difficulty\n");
    printf("3. To start the quiz (default: Beginner level)\n");
    printf("4. To display the scores\n");
    printf("5. To show ranking\n");
    printf("6. To quit\n");
    printf("Please enter option :");
    scanf("%d",&option);

    switch (option)
    {   case 1: printf("Please enter number of participants (1-5) :");
                scanf("%d",&x);
                if (x==1)
                     {printf("Please enter name 1:");
                      scanf("%s",stringname_1);}
                else if (x==2)
                     {printf("Please enter name 1:");
                      scanf("%s",stringname_1);                  
                      printf("Please enter name 2:");
                      scanf("%s",stringname_2);}                      
                else if (x==3)
                     {printf("Please enter name 1:");
                      scanf("%s",stringname_1);
                      printf("Please enter name 2:");
                      scanf("%s",stringname_2);
                      printf("Please enter name 3:");
                      scanf("%s",stringname_3);}
                else if (x==4)
                     {printf("Please enter name 1:");
                      scanf("%s",stringname_1);
                      printf("Please enter name 2:");
                      scanf("%s",stringname_2);
                      printf("Please enter name 3:");
                      scanf("%s",stringname_3);
                      printf("Please enter name 4:");
                      scanf("%s",stringname_4);}
                else if (x==5)
                     {printf("Please enter name 1:");
                      scanf("%s",stringname_1);
                      printf("Please enter name 2:");
                      scanf("%s",stringname_2);
                      printf("Please enter name 3:");
                      scanf("%s",stringname_3);
                      printf("Please enter name 4:");
                      scanf("%s",stringname_4);
                      printf("Please enter name 5:");
                      scanf("%s",stringname_5);}
                else if (x>5)
                      printf("Invalid option.Please enter again.\n");
                      break;
       case 2:printf("Beginner level     : Questions involve addition, subtraction and multiplication\n");
              printf("Advanced level     : Questions involve square and square root\n");
              printf("Please enter the level(B-Beginner, A-Advanced):");
              scanf("%s",&diff);    
              break;
       case 3:for(y=0;y<x;y++)
                 { printf("%s it's your turn. You have to answer as many questions as possible within 60 seconds. Each correct answer gives you 4 marks. 2 marks will be deducted for each wrong answer. You can skip a particular question by entering 'n', but 1 mark will be deducted for each question skipped. Press any key to start the quiz.\n",stringname_1);
                   switch(diff)
                   {
                   case'B':for(f=0;f<10;f++)
                           {
                           printf("%d+%d=\n?",num1,num2);
                           scanf("%d",&ans);
                           if(ans==num1+num2)
                                 {printf("Yes, you are right!\n");
                                  score+=4;correct++;}
                           else if(ans!=num1+num2)
                                 {printf("Sorry, you are wrong!\n");
                                  score-=2;wrong++;}
                           else if(ans==n)
                                 {printf("Question skipped!\n");
                                  score--;skipped++;break;}
                           else
                                 {printf("Wrong input! Please re-enter the answer.\n");}
                           }
                           sum+=score;
                           printf("Your score is:%d",sum);
                           break;
                   case'A':for(f=0;f<10;f++)
                           {
                           printf("%d^2?",num3);
                           scanf("%d",&ans);
                           if(ans==pow(num3, 2.0))
                                 {printf("Yes, you are right!\n");
                                  score+=4;correct++;}
                           else if(ans!=pow(num3, 2.0))
                                 {printf("Sorry, you are wrong!\n");
                                  score-=2;wrong++;}
                           else if(ans==n)
                                 {printf("Question skipped!\n");
                                  score--;skipped++;break;}
                           else
                                 {printf("Wrong input! Please re-enter the answer.\n");}
                           }
                           break;

                   default:for(f=0;f<10;f++)
                           {
                           printf("%d+%d=\n?",num1,num2);
                           scanf("%d",&ans);
                           if(ans==num1+num2)
                                 {printf("Yes, you are right!\n");
                                  score+=4;correct++;}
                           else if(ans!=num1+num2)
                                 {printf("Sorry, you are wrong!\n");
                                  score-=2;wrong++;}
                           else if(ans==n)
                                 {printf("Question skipped!\n");
                                  score--;skipped++;break}
                           else
                                 {printf("Wrong input! Please re-enter the answer.\n");}
                           }
                   }    
              }
              break;
       case 4:printf("Score List\n");
              printf("----------\n");
              printf("                                      Correct   Wrong   Skipped   Score\n");
              printf("                                      -------   ------  --------  ------\n");
              printf("%s                                    %d        %d       %d        %d\n\n",stringname_1,correct,wrong,skipped,score);
              break;
       case 5:printf("Ranking list\n");
              printf("------------\n");
              printf("1.  %s\n\n",stringname_1);
              break;
       case 6:printf("Bye!\n");
              break;

       default: printf("Invalid option.Please enter again\n");
    }
}
return 0;

    }

little question here:
1.How to get the name including the spaces?
2.How to press make program to skip question when pressed "n" and when invalid answer like 17u to show error?
3.How to generate random perfect square no. and how to generate random numbers for (num1,num2,num3)[I always get the same no. when running the program]
4.How to make a timer for the quiz (am currently limiting it to 10 question but assignment need it to end after 60 sec)

THANKS for the help in advance and sorry for the messy codes and errors(maybe)...im still new to the programming and hope to learn more with your helps.

1.How to get the name including the spaces?

Use fgets()

2.How to press make program to skip question when pressed "n" and when invalid answer like 17u to show error?

The question is unintelligible.

3.How to generate random perfect square no. and how to generate random numbers for (num1,num2,num3) [I always get the same no. when running the program]

When looking up rand(), you undoubtedly came across srand()

4.How to make a timer for the quiz (am currently limiting it to 10 question but assignment need it to end after 60 sec)

use time()

I have only been at C++ for about a year

missing a ";" on line 133 at the end of the break

For you menu i would add a system("cls"); that will wipe out the screen

to satisfy the multiple functions I would have each option in a separate function

1 - string str; getline(cin, str); will set str to user input (including spaces)

2 - use getch() maybe, or string input you could use a function to convert the string into a number (I use sstream) and then use a while loop that would exit the time exceeds 60 seconds

4 - These are what i use it may help you

string fetchTime() {//grabs current time [day] [Month] dd hh:mm:ss
    time_t rawtime;
    struct tm * timeinfo;
    time(&rawtime);
    return asctime(localtime(&rawtime));
}

how do you do the option to skip a question for the quiz???

For you menu i would add a system("cls"); that will wipe out the screen

NO NO NO NO!!!!!! It's bad form and non portable to call system commands...

2 - use getch() maybe,

I repeat, NO NO NO NO!!!!!! getch() is only available in a couple compilers, therefore non portable and non standard...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.