The output should be showing
Question 1
1a
1b
1c
1
as a string, string array of 3 choices and an int in a struct
getline(triviaFile, tempFile.tempQuestion);
for(int i = 0; i < NUM_CHOICES; i++)
{
getline(triviaFile, tempFile.tempChoices[i]);
}
triviaFile >> tempFile.tempAnswer;
The output after the first loop through
//empty line
Question 2
2a
2b
2 //should have been 2c
c
2
question 3
3a
3
As far as I can tell the problem is with the >> but I can't figure out what it is exactly I am doing wrong, any assistance would be welcome.
Last edited on
code tags please. and full code wouldn't hurt.
Here is the full code.
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int NUM_QUESTIONS = 10;
const int NUM_CHOICES = 3;
const int NUM_PLYRS = 2;
// To be used to keep track of which player's turn it is
enum turn { USER1, USER2 };
class Question
{
private:
string question;
string choices[NUM_CHOICES];
int answer;
public:
// Accessor
string getQuestion();
string getChoices( int cCount );
int getAnswer();
// Mutator
void setQuestion( string tempQ );
void setChoices( string tempC[] );
void setAnswer( int tempA );
};
//******************************************************************************
string Question::getQuestion()
{
return question;
}
string Question::getChoices( int cCount )
{
return choices[cCount];
}
int Question::getAnswer( )
{
return answer;
}
void Question::setQuestion( string tempQ )
{
question = tempQ;
}
void Question::setChoices( string tempC[] )
{
for(int i = 0; i < NUM_CHOICES; i++)
{
choices[i] = tempC[i];
}
}
void Question::setAnswer( int tempA )
{
answer = tempA;
}
//Structure to hold the temporary questions to be assigned into the game array
struct tempHolder
{
string tempQuestion;
string tempChoices[NUM_CHOICES];
int tempAnswer;
};
void readQuestions(fstream &triviaFile, Question game[], int NUM_QUESTIONS, int count);
//******************************************************************************
int main()
{
Question game [NUM_QUESTIONS];
fstream triviaFile;
int count = 0;
triviaFile.open("questions.txt", ios::in | ios::out);
// To check if the file failed to open and if so exits the program.
if(triviaFile.fail())
{
cout << "Trivia questions file failed to load.";
return 0;
}
char again = 'y';
while( again == 'y' || again == 'Y' )
{
//Function to read in questions from file
while(count < NUM_QUESTIONS)
{
readQuestions(triviaFile, game, NUM_QUESTIONS, count);
count++;
}
triviaFile.close();
cout << "Enter Y to continue to the next round\n"
<< "Anything else to exit the game." << endl;
cin >> again;
}
cout << " end";
system ("Pause");
return 0;
}
//******************************************************************************
void readQuestions(fstream & triviaFile, Question game[], int NUM_QUESTIONS, int count)
{
// Local scope holds the temporary questions and answers.
tempHolder tempFile;
triviaFile.clear();
getline(triviaFile, tempFile.tempQuestion);
for(int i = 0; i < NUM_CHOICES; i++)
{
getline(triviaFile, tempFile.tempChoices[i]);
}
triviaFile >> tempFile.tempAnswer;
// Accessor functions to put the tempfile's info into the game array
game[count].setQuestion(tempFile.tempQuestion);
game[count].setChoices(tempFile.tempChoices);
game[count].setAnswer(tempFile.tempAnswer);
cout << game[count].getQuestion() << endl;
for(int cCount = 0; cCount < NUM_CHOICES; cCount++)
{
cout << game[count].getChoices(cCount) << endl;
}
cout << game[count].getAnswer() << endl;
triviaFile.clear();
return;
}
|
Last edited on