Need help with making a basic trivia game in c++, just a beginner and still learning.

Programming Help - Its basic for regular c++ coders but i having a little trouble

im making a trivia game as practice but im having trouble, i want it to have 5 questions but in between questions i want the user to be able to continue or to not continue (its their choice) but im having a little trouble. Heres my code so far:

//Introduction
#include <iostream>
using namespace std;

//setup

//use
int main ()
{
int var1;
int var2;
int var3;
int var4;
int var5;
char cont3;
char start;
char cont;
char cont2;
char cont4;
char cot;


cout<<"Welcome to Romels Trivia game:click S to begin:";
cin>>start;

do{
if(start == 's'){
cout<<"Game Initiated"<<endl;
cout<<"Question #1: Berlin is the captal of what Country:"<<endl;
cout<<"1)Germany"<<endl;
cout<<"2)Denmark"<<endl;
cout<<"3)Japan"<<endl;
cout<<"4)Belgium"<<endl;
}


cin>>var1;
if (var1 == 1){
cout<<"Correct!to continue T"<<endl;
cin>>cont;
}else if(var1 ==2,3,4){
cout<<"Incorrect!!!!!! Do you want to try that question again y/n?"<<endl;
cin>>cont;
}

}while (cont == 'y');

if (cont =3,4,2){
}

cin>>cont2;
if (cont2 == 'd'){
cout<<"GAME OVER"<<endl;
} else if (cont2 == 't');{

do{

cout<<"Question #2: Tokyo is the captal of what Country:"<<endl;
cout<<"1)Germany"<<endl;
cout<<"2)Denmark"<<endl;
cout<<"3)Japan"<<endl;
cout<<"4)Belgium"<<endl;

cin>>var2;
if (var2 == 3){
cout<<"Correct!Do you want to continue yes (click t), no (click d)?"<<endl;
cin>>cont2;
}else if(var1 ==1,2,4){
cout<<"Incorrect!!!!!! Do you want to try that question again y/n?"<<endl;
cin>>cont2;
}
}
while (cont2 == 'y');
if (cont2 =3,4,2){
}



do{
cin>>cont3;
if (cont2,cont3== 'd'){
cout<<"GAME OVER"<<endl;

}else if (cont3 == 't');{
cout<<"Question #3: Copenhagen is the captal of what Country:"<<endl;
cout<<"1)Belarus"<<endl;
cout<<"2)Denmark"<<endl;
cout<<"3)Ireland"<<endl;
cout<<"4)Scotland"<<endl;

cin>>var3;
if (var3 == 2){
cout<<"Correct!Do you want to continue yes (click t), no (click d)?"<<endl;
cin>>cont3;
}else if(var1 ==1,3,4){
cout<<"Incorrect!!!!!! Do you want to try that question again y/n?"<<endl;
cin>>cont3;
}
}
}while (cont3 == 'y');
do{
(cont2 == 't');{
cout<<"Question #4: City of Brussels is the captal of what Country:"<<endl;
cout<<"1)Belarus"<<endl;
cout<<"2)Denmark"<<endl;
cout<<"3)Ireland"<<endl;
cout<<"4)Scotland"<<endl;

cin>>var4;
if (var3 == 1){
cout<<"Correct!Do you want to continue yes (click t), no (click d)?"<<endl;
cin>>cont4;
}else if(var1 ==3,2,4){
cout<<"Incorrect!!!!!! Do you want to try that question again y/n?"<<endl;
cin>>cont4;
}
}
}while (cont3 == 'y');
system ("pause");
}

can someone help improve this code so it actually works well and is not too buggy, thank you in advance. Also maybe some tips
(Note: I am a newbie coder, and just started learning)
well first let me cook something up. but first, system is not good to use it is slow and unsafe. put this line in:#include <cstdio> and use the getchar();function instead
you could create a class with a function in it that reads a question, list of possible answers and a correct answer from a text file, you could then manage your class with various commands,
you could also put all the class objects in a while loop, after five or so objects you could have an option to change the bool value keeping the while loop active so as to exit the loop to a main menu;If you think your too nooby this is easier than it sounds.

Edit: i will give you some links and examples if you think this is hard.
Last edited on
OK I've made something. I will not do every thing for you but this will help you figure out some neat tricks. first off just for practice we will make a header file

Game.h:
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
#ifndef "Game.h" //if Game.h is not defined (#if NOT defined)
#define "Game.h" //define it

#include <cstdio>

char q1(const char Anwr)
{
 cout<<"Question #1: Berlin is the captal of what Country:"<<endl;
 cout<<"1)Germany"<<endl;
 cout<<"2)Denmark"<<endl;
 cout<<"3)Japan"<<endl;
 cout<<"4)Belgium"<<endl;
 
 return cin>>Anwr
}


void die()
{
 cout<<"Game Over\n";
 cout<<"press any key to continue\n";
 getchar(); //dont use system()
}

#endif


main.cpp:
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
#include <iostream>
#include <cstdio>
#include <cctype> //explain this later
#include "Game.h" //header file we will create for our game

int main()
{
 int var[4];  //only 4 needed cause array counts at 0
 char cont[3]; //again array starts at 0
 char cot;
 char start;

 cout<<"Welcome to Romels Trivia game: Press S to begin\n";
 cin>>start;
 toupper(start); //i beleive c++ is case sensitive so make start upper it is the 
                //reason for cctype

 while (start=="S"){
   var[0] = q1();

   if (var[0] != 1)
   {
    cout<<"Incorrect!!!!!! Do you want to try that question again y/n?\n";
    cin>>cont[0]
    toupper(cont[0]);
    if (cont[0]=="Y")
       q1(); // if only one thing needs to be done no need for braces 
 
   }
   else
   {
    cout<<"Correct! do you want to continue yes(t) no(d)?\n";
    cin>>cont[1];
    toupper(cont[1]);
   }

   if (cont[1]=="D")
   {
     die();
   }
   else if (cont[1]=="T")
   {
    return; //nothing important here
   }
   else
   {
    cout<<"invalid command now exiting\n";
    die();
   }
   // rest of game to be implemented
 }
}
Last edited on
Thank-you a lot James, this helped a lot. I still have a lot to learn, but i just have one question, what does the syntax toupper mean?
its telling me that char and const char are incompatible?In line 18 and 19 for the main.cpp

while (start=="S"){
var[0] = q1();

it also says that q1 is unidentified? any suggestions
toupper takes a char and converts it to uppercase. this is because c++ is case sensitive. if a user types "s" it is not the same as "S" so we validate input. if the char is already upper it wont do anything bad it will just keep it that way. in simple terms we make sure user input is correct
cin might actually return a string you could try using getchar instead.


sorry bout that :-\
Topic archived. No new replies allowed.