| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
trying to get the following work but 5 err/1 warning it seems futile.
The struct is not working. How can I reduce the default constructor seems long. is PROBLEM /*You will be writing a “Quiz Tutor” program. The program should begin by reading a question and 4 possible answers from a text file. It should display these to the user. The user will select the answer from the choices. The program will determine if the answer is correct and indicate this to the user. If the answer is correct, the next question will be displayed. If it is incorrect, the incorrect question will be stored in a list in order before displaying the next question. At the end of the file the user has completed round one, the program should display the number of correct answers and a score (percent correct). The program will then begin round 2. The questions from the list of incorrectly answered questions will be redisplayed in order for the user to try again. If the question is answered correctly this time the number of correct answers is increased. If the user answers incorrectly the second time, the correct answer is displayed. At the end of round 2, the quiz is over. The program should display the new score along with the number of correct answers from the first round plus the number of correct answers from the second round. */ code: Code:
/****Driver for quiz program. ****/
#include<iostream.h>
using namespace std;
#include "game.h"
void rnd_1();
void rnd_2(question &);
void main()
{
rnd_1();
}
void rnd_1()
{
question quiz;
cout << "round 1 \n";
for ( int i =0; i < quiz.get_quest_num(); i++)
{
quiz.display_question();
quiz.process_rdn1();
}
quiz.display_score();
if (get_ans_cor() == get_quest_num())
{
cout << " You got everythin right in first round \n";
exit(1);
}
else
rnd_2(quiz);
}
void rnd_2( question & quiz2)
{
cout << "round 2 \n";
for ( int i =0; i < quiz2.get_ans_wrg(); i++)
{
quiz2.display_wrg();
quiz2.process_rdn2();
}
quiz2.display_score();
cout << "Thank you for playing \n";
exit(1);
}//end of driver
/***Game.cpp ----------------
This file implements Game member functions.
****/
#include <iostream>
using namespace std;
#include "game.h"
question::question() // default constructor
{
ifstream in;
string filename;
quest_num = 0;
ans_cor = 0;
ans_wrg = 0;
cout <<"Enter file name";
cin >> filename;
in.open(filename.c_str());
assert(in.open());
while(!in.eof())
{
getline(in, q_sub.quest);
getline(in, q_sub.ans1);
getline(in, q_sub.ans2);
getline(in, q_sub.ans3);
getline(in, q_sub.ans4);
getline(in, q_sub.answer);
rdone.enqueue(q_sub); //enque all question submitted
quest_num++; // increase questions read from file
}
}
void question::display_question()
{
x_file q_disp = rdone.front();
cout << q_disp.quest <<endl;
cout << q_disp.ans1 <<endl;
cout << q_disp.ans2 <<endl;
cout << q_disp.ans3 <<endl;
cout << q_disp.ans4 <<endl;
}
void question::process_rdn1()
{
string ans;
x_file q_disp = rdone.front();
cout <<"Give your answer";
cin >> ans;
if ( ans == q_disp.answer)
{
cout<<"correct answer\n";
ans_cor++;
rdone.dequeue();
}
else
{
cout <<"Incorrect answer\n";
ans_wrg++;
rdtwo.enqueue(q_disp);
rdone.dequeue();
}
}
void question::display_wrg()
{
x_file q_disp = rdtwo.front();
cout << q_disp.quest <<endl;
cout << q_disp.ans1 <<endl;
cout << q_disp.ans2 <<endl;
cout << q_disp.ans3 <<endl;
cout << q_disp.ans4 <<endl;
}
void question::process_rdn2()
{
string ans;
x_file q_disp = rdtwo.front();
cout <<"Give your answer";
cin >> ans;
if ( ans == q_disp.answer)
{
cout<<"correct answer\n";
ans_cor++;
ans_wrg--;
rdtwo.dequeue();
}
else
{
cout <<"Incorrect answer\n";
cout<<"The correct answer is "<< q_disp.answer<<endl;
rdtwo.dequeue();
}
}
void question::display_score()
{
cout <<"Your total correct answer: "<< ans_cor << endl;
cout <<"Your total incorrect answer: "<<ans_wrg; <<endl;
cout <<" Your Score is "<< ans_cor << quest_num <<endl; // need type casting to double
}
int question::get_quest_num()
{
return quest_num;
}
int question::get_ans_cor()
{
return ans_cor;
}
int question::get_ans_wrg()
{
return ans_wrg;
} //end of game.cpp
#include <iostream.h>
#include <string>
#include <fstream.h>
#include <assert.h>
#include "Queue.h"
#ifndef game
#define game
struct x_file
{
string quest;
string ans1;
string ans2;
string ans3;
string ans4;
string answer;
}; // end of struct
class Question
{
public:
//--Constructors
Question();
//--Mutators
void process_rdn1();
void process_rdn2();
//--Output
void display_score();
void display_wrg()
void display_question();
//--Accessors
int get_quest_num();
int get_ans_cor();
int get_ans_wrg();
private:
x_file q_sub; //struct from file
queue<x_file> rdone; //initial questions
queue<x_file> rdtwo; // incorrect question
int ques_num;
int ans_cor;
int ans_wrg;
}; //end of class
#endif
|
|
#2
|
|||
|
|||
|
Quote:
[code] #include <iostream> #include "game.h" using namespace std; also i cant help cause i dont know what it game.h nor the other .h cause first u have have them also it do it yeah thats my answer remember to drop the h in iostream etc |
|
#3
|
|||
|
|||
|
program
Quote:
3 files game.h, game.cpp and quiz.cpp (driver) the program basically reads questions from a file and requires the user to select correct answer. the questions are initially placed in a queue, hence struct. wrong answers are placed in another until user first set of question, then question that was wrong are repeated for the user to get correct answer. |
|
#4
|
||||
|
||||
|
What exactily is your problem? At first glance, everything looks fine....
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#5
|
|||
|
|||
|
I am not clear on your problem.
However, it is not a good idea to have input/output statements in the constructor. You should avoid that. Additionally, please have a design around the problem. Obviously you have a slution in the program but the mapping from the problem to program is difficult to make. ----------------------------- Programming ( Assignment / Project ) Help |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help with school assignment |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|