Trying to make a quiz with switch
Oct 29, 2015 at 6:49am UTC
I was trying to make a quiz with a switch statement...but I realize that it's going to take a lot of work because you will have to paste the same questions over and over again as seen in the code. It's not even nearly finished yet and I'm trying to let the user answer each questions by inputting a number. Is there any way to make this shorter besides pasting the questions again and again? Besides switch, can while loop method make this easier? Thanks in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include <iostream>
#include <string>
using namespace std;
int main()
{
enum datatype { choice1, choice2, choice3, choice4, choice5 };
int choice11,
choice22;
cout << "Hello and good luck taking this quiz." << endl;
"\n" ;
cout << "Which of the following is a datatype?" << endl;
cout << "1. int " << endl;
cout << "2. doubletriple" << endl;
cout << "3. setdecimal" << endl;
cout << "4. Valuemore" << endl;
cout << "\n" << endl;
cin >> choice11;
switch (choice11)
{
case 1:
{
cout << "\n" << endl;
cout << "What datatype do you need to use when you're dealing with numbers that has decimals?" << endl;
cout << "1. bool" << endl;
cout << "2. void" << endl;
cout << "3. double" << endl;
cout << "4. char" << endl;
cout << "\n" << endl;
cin >> choice22;
}
switch (choice22)
{
case 1:
{
cout << "\n" << endl;
cout << "How many byte does char have?" << endl;
cout << "1. 5" << endl;
cout << "2. 3" << endl;
cout << "3. 1" << endl;
cout << "4. None of the above" << endl;
cout << "\n" << endl;
}
break ;
case 2:
{
cout << "\n" << endl;
cout << "What datatype do you need to use when you're dealing with numbers that has decimals?" << endl;
cout << "1. bool" << endl;
cout << "2. void" << endl;
cout << "3. double" << endl;
cout << "4. char" << endl;
cout << "\n" << endl;
}
break ;
case 3:
{
cout << "\n" << endl;
cout << "What datatype do you need to use when you're dealing with numbers that has decimals?" << endl;
cout << "1. bool" << endl;
cout << "2. void" << endl;
cout << "3. double" << endl;
cout << "4. char" << endl;
cout << "\n" << endl;
}
break ;
case 4:
{
cout << "\n" << endl;
cout << "What datatype do you need to use when you're dealing with numbers that has decimals?" << endl;
cout << "1. bool" << endl;
cout << "2. void" << endl;
cout << "3. double" << endl;
cout << "4. char" << endl;
cout << "\n" << endl;
}
}
return 0;
}
Oct 29, 2015 at 7:27am UTC
Hi ruroni24
Friend, you have the wrong concept about switch statements. That's not how they work.
Think of a switch statement like an if/else staement.
1 2 3 4 5 6 7 8
if (x == 1){
cout << "one" ;}
else {if (x == 2)
cout << "two" ;}
else {if (x == 3)
cout << "three" ;}
You can write this as a switch statement, which is somewhat more readable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
switch (x) {
case 1:
cout << "one" ;
break ;
case 2:
cout << "two" ;
break ;
case 3:
cout << "three" ;
break ;
default :
}
}
So a switch statement could look a little more like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <iostream>
#include <string>
using namespace std;
void answer(int , int );
int main()
{
cout << "Hello and good luck taking this quiz." << endl;
"\n" ;
int choice, score=0;
cout << "Which of the following is a datatype?" << endl;
cout << "1. int " << endl;
cout << "2. doubletriple" << endl;
cout << "3. setdecimal" << endl;
cout << "4. Valuemore" << endl;
cout << "\n" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "You got it!" << endl;
break ;
case 2:
cout << "Come on you can do better!" << endl;
break ;
case 3:
cout << "Really? Are you on crack?" << endl;
break ;
case 4:
cout <<"Do you even know how to turn on a computer?" << endl;
break ;
default :
cout << "Geeze you're an idiot!" << endl;
}
return 0;
}
Now to answer your other question, there are many approaches to solve your problem. You could read questions in from a text file... make an array, read your questions from a list... but let's stick to the basics for now...
You could use the direct approach...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Hello and good luck taking this quiz." << endl;
"\n" ;
int choice, score=0;
cout << "Which of the following is a datatype?" << endl;
cout << "1. int " << endl;
cout << "2. doubletriple" << endl;
cout << "3. setdecimal" << endl;
cout << "4. Valuemore" << endl;
cout << "\n" << endl;
cin >> choice;
if (choice!=1)
{
cout << "Sorry, you are incorrect." <<endl;
}
else
{
cout << "Way to go!" << endl;
score++;
}
cout << "What datatype do you need to use when you're dealing with numbers that has decimals?" << endl;
cout << "1. bool" << endl;
cout << "2. void" << endl;
cout << "3. double" << endl;
cout << "4. char" << endl;
cout << "\n" << endl;
cin >> choice;
if (choice!=3)
{
cout << "Sorry, you are incorrect." <<endl;
}
else
{
cout << "Way to go!" ;
score++;
}
return 0;
}
or the function approach...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <iostream>
#include <string>
using namespace std;
void answer(int , int );
int main()
{
cout << "Hello and good luck taking this quiz." << endl;
"\n" ;
int choice, score=0;
cout << "Which of the following is a datatype?" << endl;
cout << "1. int " << endl;
cout << "2. doubletriple" << endl;
cout << "3. setdecimal" << endl;
cout << "4. Valuemore" << endl;
cout << "\n" << endl;
cin >> choice;
answer (choice,1);
cout << "\n" << endl;
cout << "What datatype do you need to use when you're dealing with numbers that has decimals?" << endl;
cout << "1. bool" << endl;
cout << "2. void" << endl;
cout << "3. double" << endl;
cout << "4. char" << endl;
cout << "\n" << endl;
cin >> choice;
answer (choice, 3);
}
void answer(int choice, int answer)
{
if (choice==answer)
{
cout << "WAY TO GO!!! WOO HOO!!!" << endl;
}
else
{
cout << "Sorry! You missed that one..." << endl;
}
}
Last edited on Oct 29, 2015 at 7:44am UTC
Oct 30, 2015 at 2:28am UTC
Thank you!! :)
Topic archived. No new replies allowed.