I'm a first year CS student, and we have our first assignment. We have to make a quiz with a menu that will keep re-displaying
1.
2. and so on. But i can't enter 2 without choosing 1 and i cant display 3 without choosing 1 and answering 2. I've written the whole code..but the validation is getting to me. I have no idea what to do..i have used if and while loops but i keep getting blocked out. If use characters like underscore the loop i use to redisplay the menu goes crazy..so i have to find a way to fix that/ but I'm not sure how to tell the scanf to display error if character is inputted. Also, I'm thinking of using char option..as you'll see in my source code..well i used that..but default: always exits out of the loop and i cant tell it to redisplay instead of exiting.
Please help and i hope u understand what i'm trying to say lol..
Oh and however this code is crap im proud of it..its the longest code i've ever written..so no plagiarism! I have till Nov. 19 so ur help will be appreciated as soon as possible thanks.
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
/*this code is for to ask a number of questions in a quiz contest re-displaying a menu */
main()
{
int i ;
int one, two, three, four ;
int answer1 = 2 ;
int answer2 = 9 ;
int answer3 = 24;
int answer4 = 100;
char yesno ;
int num1 ;
int option = 3 ;
do
{
printf(" \n ") ;
printf(" ((----))\n") ;
printf(" <--<\\\\_//>->\n") ;
printf(" | | \n") ;
printf(" (===)\n") ;
printf("\n") ;
printf(" QUIZZITCH CUP\n") ;//This is just some drawing of the prize cup that the winner would have
printf("\n") ;
printf("1. Enter the number of questions to be asked for this round of the quiz\n") ;
printf("2. Start quiz\n") ;
printf("3. Display the numbers of questions that were (i) correctly (ii) incorrectly\n") ;
printf("4. Exit Program\n") ;
scanf("%d", &option) ;//waits for an input for 1, 2, 3 or 4
while(option!=1 && option!=4)
{
printf("You must choose 1 before you can proceed to quiz\n") ;
scanf("%d", &option) ;
i++ ;
}
switch(option) //the switch option allows me to choose number of questions i want .
{
case 1:
{
printf("1. One question\n") ;
printf("2. Two questions\n") ;
printf("3. Three questions\n") ;
printf("4. Four questions\n") ;
scanf("%d", &num1) ; //this scanf to read in the option for the number questions to be answered
}
case 2:
{
switch(num1)//start of option 2 using num1 as a variable of numbers of questions to be asked
{
case 1:
{
printf("What is 1+1: *\n") ;
scanf("%d", &one) ;
if(one == answer1)
{
printf("%d is Correct!\n",answer1) ;
}
else
{
printf("Incorrect!Answer is: %d\n",answer1) ;
}
break;
}//end of case 1 of question 1
case 2:
{
printf("1. what is 1+1: *\n") ;
scanf("%d", &one) ;
if(one == answer1)
{
printf("Correct!\n") ;
}
else
{
printf("Incorrect! Answer is: %d\n", answer1) ;
}
printf("2. what is 3 * 3: *\n") ;
scanf("%d", &two) ;
if(two == answer2)
{
printf("Correct!\n") ;
}
else
{
printf("Incorrect! Answer is: %d\n", answer2) ;
}
break ;
}//end of question 2
//case 3
case 3:
{
printf("1. what is 1+1: *\n") ;
scanf("%d", &one) ;
if(one == answer1)
{
printf("Correct!\n") ;
}
else
{
printf("Incorrect! Answer is %d\n", answer1) ;
}
printf("2. what is 3 * 3: *\n") ;
scanf("%d", &two) ;
if(two == answer2)
{
printf("Correct!\n") ;
}
else
{
printf("Incorrect! Answer is %d\n", answer2) ;
}
printf("3. what is 4!: *\n") ;
scanf("%d", &three) ;
if(three == answer3)
{
printf("Correct!\n") ;
}
else
{
printf("Incorrect! Answer is %d\n",answer3) ;
}
break ;
}
case 4:
{
printf("1. what is 1+1: *\n") ;
scanf("%d", &one) ;
if(one == answer1)
{
printf("Correct!\n") ;
}
else
{
printf("Incorrect! Answer is %d\n", answer1) ;//one
}
//two
printf("2. what is 3 * 3: * \n") ;
scanf("%d", &two) ;
if(two == answer2)
{
printf("Correct!\n") ;
}
else
{
printf("Incorrect! Answer is %d\n",answer2) ;
}
//two
printf("3. what is 4!: *\n") ;
scanf("%d", &three) ;
if(three ==answer3)
{
printf("Correct!\n") ;
}
else
{
printf("Incorrect! Answer is %d\n", answer3) ;
}
printf("4. what is 800/2*4: *\n") ;
scanf("%d", &four) ;
if(four == answer4)
{
printf("Correct!\n") ;
}
else
{
printf("Incorrect! Answer is %d\n", answer4) ;
}
break ;
}
}
break ;
}//end of case 2 of switch(option)
case 3: //beginning of case 3. case 3 shows how many questions the user answer correctly
{
if(num1==1)
{
//by checking the various possibilities the user could have answered the question
if(one==2)
{
printf("i. You got 1 Correct!\n") ;
}
else
{
printf("ii. You got 1 Incorrect!!\n") ;
}
break ;
}
if(num1==2)
{
if(one == 2 && two == 9)
{
printf("i. You got 2 Correct!\n") ;
printf("ii. You got none Incorrect!!\n") ;
}
else if(one == 2 && two!= 9||one!=2 && two == 9)
{
printf("i. You got 1 Correct!\n") ;
}
else
{
printf("ii. You got Everything Incorrect!!\n") ;
}
break ;
}
if(num1==3)
{
if(one != 2 && two !=9 && three == 24||one != 2 && two == 9 && three != 24||one == 2 && two != 9 && three != 24)
{
printf("i. You got 1 Correct!\n") ;
printf("ii. You got 2 Incorrect!!\n") ;
break ;
}
else if(one == 2 && two == 9 && three != 24||one != 2 && two == 9 && three == 24||one == 2 && two != 9 && three == 24)
{
printf("i. You got 2 Correct!\n") ;
printf("ii. You got 1 Incorrect!!\n") ;
break ;
}
else if(one==2 && two==9 && three==24)
{
printf("i. You got Everything Correct\n") ;
}
else
{
printf("i. You got Everything Incorrect!!\n") ;
}
}
if(num1==4)
{
if(one == 2 && two !=9 && three != 24 && four != 100||one != 2 && two == 9 && three != 24 && four != 100||one != 2 && two != 9 && three == 24 && four!= 100||one != 2 && two != 9 && three != 24 && four==100)
{
printf("i. You got 1 correct!\n") ;
printf("ii. You got 3 incorrect!!\n") ;
break ;
}
else if(one != 2 && two != 9 && three == 24 && four == 100||one == 2 && two == 9 && three != 24 && four != 100|| one != 2 && two == 9 && three == 24 && four != 100|| one == 2 && two != 9 && three != 24 && four == 100)
{
printf("i. you got 2 correct\n") ;
printf("ii. you got 2 incorrect\n") ;
break ;
}
else if(one != 2 && two == 9 && three == 24 && four == 100||one == 2 && two != 9 && three == 24 && four == 100||one == 2 && two == 9 && three != 24 && four== 100||one == 2 && two == 9 && three == 24 && four!= 100)
{
printf("i. You got 3 Correct!\n") ;
printf("ii. You got 1 Incorrect!!\n") ;
break ;
}
else if(one == 2 && two == 9 && three == 24 && four == 100)
{
printf("i. You got everything Correct\n") ;
break ;
}
else
{
printf("i. you got everything Incorrect\n") ;
break ;
}
}
}//end of switch(num1)
case 4:
{
printf("Are you sure you want to exit quiz?\n") ;
printf("Y/N") ;
printf("\n") ;
scanf("%1s", &yesno) ;
if(yesno== 'Y'|| yesno == 'y')
{
exit(0) ;
}
clrscr() ;
break ; /*user must input y to quit*/
}
default:
{
printf("Can't you follow simple instructions?\n") ;
break ;
}
}
}while(option==1 || option==2 ||option==3 || option == 4) ;
getch() ;
}