Basically I want to turn my religion based game into a game that uses classes. This is for an assignment however I have no clue where to start switching things around. Here is the main code for the game as I have it right now. I need it to have 2.cpp files which I have named main.cpp and questions.cpp and questions.h. All I really need is a basis on where to start and I am sure I can get it from there.
#include <iostream>
using std::cerr;
#include <string>
#include <fstream>
using std::ofstream;
#include <cstdlib> // for exit function
usingnamespace std;
void menu ()
{
ifstream inData;
int score = 0;
inData.open("userScore.txt");
inData >> score ;
cout << "The last score saved is " << score << endl;
cout << "Welcome to Religion True or False." << endl;
cout << "If you would like to play the religion game then please select option A." << endl;
cout << "If you would like to end the game please select option B." << endl;
}
void questions()
{
string userinput1, userinput2, userinput3, userinput4, userinput5, userinput6;
string ans1 = "n" ;
string ans2 = "n" ;
string ans3 = "n" ;
string ans4 = "y" ;
string ans5 = "n" ;
int score = 0;
cout<< "Hello this is your standard Yes and No quiz about religion." << endl;
cout<< "Please enter 'y' or 'n'" << endl;
cout << "If you enter anything else it will count as a wrong answer." << endl;
//Question 1
cout << "No#1 Is Jesus God?" << endl;
cin >> userinput1;
if ( userinput1 == ans1)
{
cout << "You entered " << userinput1 << " that was Correct! Time for the next question." << endl;
score++;
cout << "Your score is " << score << "." << endl;
cout << "Please press enter to continue." << endl;
// Pauses the screen to allow the user to read
std::cin.get();
}
else
{
cout << "You entered " << userinput1 << " that was wrong." << endl;
cout << "No, As you are not YOUR father neither is Jesus HIS father!" << endl;
cout << "Your score is " << score << "." << endl;
cout << "Please press enter to continue." << endl;
// Pauses the screen to allow the user to read
std::cin.get();
}
// Pauses the screen to allow the user to read
std::cin.get();
//Question 2
cout << "No#2 Does religion matter more than faith?"<< endl;
cin >> userinput2;
if ( userinput2 == ans2)
{
cout << "You entered " << userinput2 << " that was Correct! Time for the next question."<< endl;
score++;
cout << "Your score is " << score << "." << endl;
cout << "Please press enter to continue." << endl;
// Pauses the screen to allow the user to read
std::cin.get();
}
else
{
cout << "You entered " << userinput2 << " that was wrong." << endl;
cout << "Your score is " << score << "." << endl;
cout << "No Faith is all that truly matters." <<endl;
cout << "Nowhere in the Bible does it say you must be some religion." <<endl;
cout << "Please press enter to continue." << endl;
// Pauses the screen to allow the user to read
std::cin.get();
}
// Pauses the screen to allow the user to read
std::cin.get();
//Question 3
cout << "No#3 Is Jesus God’s only son?"<< endl;
cin >> userinput3;
if ( userinput3 == ans3)
{
cout << "You entered " << userinput3 << " that was Correct! Time for the next question."<< endl;
score++;
cout << "Your score is " << score << "." << endl;
cout << "Please press enter to continue." << endl;
// Pauses the screen to allow the user to read
std::cin.get();
}
else
{
cout << "You entered " << userinput3 << " that was wrong." << endl;
cout << "Your score is " << score << "." << endl;
cout << "No, While Jesus is God’s only begotten son, " <<endl;
cout << "we are all his creations and therefore we are all his sons and daughters." <<endl;
cout << "Please press enter to continue." << endl;
// Pauses the screen to allow the user to read
std::cin.get();
}
// Pauses the screen to allow the user to read
std::cin.get();
//Question 4
cout << "No#3 Does God grant the power to heal?"<< endl;
cin >> userinput4;
if ( userinput4 == ans4)
{
cout << "You entered " << userinput4 << " that was Correct! Time for the next question."<< endl;
score++;
cout << "Your score is " << score << "." << endl;
cout << "Please press enter to continue." << endl;
// Pauses the screen to allow the user to read
std::cin.get();
}
else
{
cout << "You entered " << userinput4 << " that was wrong." << endl;
cout << "Your score is " << score << "." << endl;
cout << "Yes, He states that those that believe in me will have the power to heal the sick, " <<endl;
cout << "cast out demons, handle snakes without fear, and speak in tounges." <<endl;
cout << "Please press enter to continue." << endl;
// Pauses the screen to allow the user to read
std::cin.get();
}
// Pauses the screen to allow the user to read
std::cin.get();
//Question 5
cout << "No#3 Is God the only God that has ever been?"<< endl;
cin >> userinput5;
if ( userinput5 == ans5)
{
cout << "You entered " << userinput5 << " that was Correct! Time for the next question."<< endl;
score++;
cout << "Your score is " << score << "." << endl;
cout << "Please press enter to continue." << endl;
// Pauses the screen to allow the user to read
std::cin.get();
}
else
{
cout << "You entered " << userinput4 << " that was wrong." << endl;
cout << "Your score is " << score << "." << endl;
cout << "No, The fallen angels was considered Gods at one point in time. " <<endl;
cout << "Even God refers to other Gods in the Bible." <<endl;
cout << "Please press enter to continue." << endl;
// Pauses the screen to allow the user to read
std::cin.get();
}
{
ofstream outData;
cout << "Your Final score is " << score << "." << endl;
outData.open("userScore.txt");
outData << score << endl;
// Pauses the screen to allow the user to read
std::cin.get();
}
}
int main()
{
char choice;
bool quit = false;
while(!quit)
{
menu ();
cin >> choice; //input
cout << endl; // put to create a blank line
switch (choice)
{
case'A':
case'a':
questions ();
cout << endl;
break;
case'B':
case'b':
quit = true;
break;
}
}
// Pauses the screen to allow the user to read
std::cin.get();
return 0;
}
Any help with this project would be appreciated.
Basically I want to change the questions part into a class and maybe the answers also. While leaving the rest in the main area for now. Later on I might end up putting all of it into their own classes.
Well I have succeded without help from here to get it broken into classes. only problem I'm having withthe game now is it won't compile unless i comment out three lines in my main.cpp. below is the main.cpp if you need more of the files please ask I will post them also.
//main.cpp
#include <iostream>
#include "questions.h"
#include "answers.h"
#include "menu.h"
using std::cerr;
#include <string>
#include <fstream>
using std::ofstream;
#include <cstdlib> // for exit function
void game()
{
questions iquestions; //instance of questions
answers ianswers; //instance of answers
ianswers.askQuestion(); //Here is the inheritance- ianswers is accessing a function in questions
//ianswers.correctAnswer(); // Pull the message saying correct and add one to score.<--not working
//ianswers.setAnswer();//Pull the compair answers from answers.cpp <-- not working right
//ianswers.wrongAnswer();//Pull the real answers from answers.cpp <--not working right
cout << "Hello this is your standard Yes and No quiz about religion." << endl;
cout << "Please enter 'y' or 'n'" << endl;
cout << "If you enter anything else it will count as a wrong answer." << endl;
cout << "Please press enter to continue.";
// Pauses the screen to allow the user to read
cin.get();
//Question 1
cout << ianswers.Q1 ; //<-- not showing the question
cout << endl;
cin >> ianswers.uAnswer ; //<-- not asking for input i think
// Pauses the screen to allow the user to read
cin.get();
// Checks if the user input equals the answer for said question.
if (ianswers.uAnswer == ianswers.ans1)
{
answers correctAnswer() ; //<-- not showing reposnse saying correct answer and increasing the score.
}
else
{
cout << ianswers.A1; //<-- not showing the answer
}
}
int main()
{
char choice;
bool quit = false;
while(!quit)
{
Menu menu;//instance of Menu
menu.setMenu();//Calls the function to display the menu
menu.setChoice();//sets users input
choice = menu.getChoice();//get input from keyboard
cout << endl; // put to create a blank line
switch (choice)
{
case'A':
case'a':
game();
cout << endl;
break;
case'B':
case'b':
quit = true;
break;
}
}
// Pauses the screen to allow the user to read
cin.get();
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//questions.cpp
#include "questions.h"
#include <iostream>
usingnamespace std;
questions::questions()
{
}
void questions::askQuestion()
{
string Q1 = "No#1 Is Jesus God?" ;
string Q2 = "No#2 Does religion matter more than faith?" ;
string Q3 = "No#3 Is Jesus God’s only son?" ;
string Q4 = "No#4 Does God grant the power to heal?" ;
string Q5 = "No#5 Is God the only God that has ever been?" ;
}
//answers.cpp
#include "answers.h"
#include "questions.h"
#include <iostream>
usingnamespace std;
void answers::userAnswer()
{
}
void answers::setAnswer()
{
ans1 = 'n' ;
ans2 = 'n' ;
ans3 = 'n' ;
ans4 = 'y' ;
ans5 = 'n' ;
}
void answers::wrongAnswer()
{
A1 = "No, As you are not YOUR father neither is Jesus HIS father!";
A2 = "No Faith is all that truly matters. Nowhere in the Bible does it say you must be some religion.";
A3 = "No, While Jesus is God’s only begotten son, we are all his creations and therefore we are all his sons and daughters.";
A4 = "Yes, He states that those that believe in me will have the power to heal the sick, cast out demons, handle snakes without fear, and speak in tounges.";
A5 = "No, The fallen angels was considered Gods at one point in time.";
}
void answers::correctAnswer()
{
int score = 0;
cout << "You entered " << uAnswer << " that was Correct! Time for the next question." << endl;
score++;
cout << "Your score is " << score << "." << endl;
cout << "Please press enter to continue." << endl;
}
//menu.cpp
#include "Menu.h"
#include <fstream>
using std::ofstream;
string Menu::getMenu()
{
return menu;
}
void Menu::setMenu()
{
ifstream inData;
int score = 0;
inData.open("userScore.txt");
inData >> score ;
cout << "The last score saved is " << score << endl;
cout << "Welcome to Religion True or False." << endl;
cout << "If you would like to play the religion game then please select option A." << endl;
cout << "If you would like to end the game please select option B." << endl;
}
char Menu::getChoice()
{
return choice;
}
void Menu::setChoice()
{
cin>>choice;
}
then there is also a score.txt just to keep score.
If I un-comment those 3 lines I get 3 unresolved external file errors. So far no-one has been able to help explain why it is doing that, how to fix it, or a workaround other than hard coding it back in like I had before. I have ran it by 3 tutors, AND my teacher none of which can figure this problem out. So please people any help here would be grateful.
Also let me add while this is still an assignment there is MUCH more to be added and changed to this game before it is done. Only thing I am having problems with right now is this strange error that no-one can figure out, and will cause me not to be able to continue without hard coding it and me nor my teacher wants it hard-coded. If we wanted that I could've kept it the way it was lol. So please no-one stating garbage about doing my work for me, for obviously i have done my work, just can't solve this problem.
In the end i will have around 5-6 classes writing to 1-2 files and many many more questions and maybe even a GUI.
Please copy and paste these unresolved external errors.
Also, inheriting answers from questions is not only confusing, it's in my humble opinion bad design, I would separate the two objects. Question object that handles all question behavior and Answer object handling all answer behavior. If they need some type of communication you can pass one to the other as a reference (or const reference and vice versa).
e.g. - Having the answer object ask questions makes no sense.
Also, it looks like your using your uAnswer in some places before it is every initialized.
The code you've posted for the most part looks like you should not be getting this error.
On line 38 in main.cpp chage this: answers correctAnswer() ;
to ianswers.correctAnswer();
As it was, that code was incorrect, may have been causing you problems.
The reason i am inheriting the questions from answers is because I have ot have 2 classes inheriting from 2 other classes. That would be the first one. The second will end up being the void game(). Me myself I would have to agree about using the reference instead but that is what I have to have for the class.
I changed the part of the code you mentioned ianswers.correctAnswer(); and it is still giving me the unresolved external error.
Error 2 error LNK2019: unresolved external symbol "public: void __thiscall
answers::correctAnswer(void)" (?correctAnswer@answers@@QAEXXZ) referenced in function "void
__cdecl game(void)" (?game@@YAXXZ) C:\Users\David\Documents\westwood college\Term
4\Programming\test4\test4\main.obj test4
then it shows a warning under that
1 2 3
Warning 1 warning C4930: 'answers correctAnswer(void)': prototyped function not called (was a
variable definition intended?) c:\users\david\documents\westwood college\term 4\programming
\test4\test4\main.cpp 37 1 test4
Now if I recomment that line you had me change the errors vanishes. If I uncomment the 2 lines under it gives me 3 unresolved errors.
I tried to add in ianswers.userAnswer(); so as to call it before the rest of the variables get passed for the uAnswer but that gave me a external file error also. So it would appear it won't let me call ANY function from answers.cpp but from what I have been told that file is coded correctly.
OK I am starting to think it has something to do with my actual project files. I tried using another computer and copied and pasted the code into it instead of using hte zipped version of hte game. Guess what it loaded, but still doesn't ask the question, nor does it actuall compair the uanswer to ans1 like it is sopposed too. I have moved a few things around since it compiled and managed to get the correct answer AND wrong answer both to show problem there is it shows them no matter what you type in which means it is ignoring the if statement and just running the functions. any ideal why it is doing this?