I've hit a wall.

I have been tasked with creating a sort of "Game Show" type of program. The program is meant to read a text file, find the character '@', log whether or not it appears on a certain line or not, and then compare that to a user input. However, currently, the program will always use the '@' character found at the end of the text file for the final problem.

The Main File:
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
 #include <string>
#include <iostream>
#include <fstream>
#include <time.h>
#include <sstream>
#include "GameShow.h"

using namespace std;

int main() 
{

	srand(time(NULL));

	readQuestionsFromFile();
	printQestionToConsole(1);
	if (validateUserAnswer(0,0) == true)
		cout << "memes" << endl;

	int score = 0;

	system("pause");

}

The Header File:

#pragma once

#include <string>
#include <iostream>
#include <fstream>
#include <time.h>
#include <sstream>
using namespace std;

int switchStatePicker(int limit);

void fileReader();

char answerFinder();

int readUserInput();

void displayGame(int answer, int guess, int score);

bool validateUserAnswer(int answer, int guess);

bool isCorrectAnswer(string text);

void printQestionToConsole(int qIndex);
void readQuestionsFromFile();


The functions file: 

#include <string>
#include <iostream>
#include <fstream>
#include <time.h>
#include "GameShow.h"
#include <sstream>

using namespace std;
string questionName = "Questions.txt";

string question[100];
string answer1[100];
string answer2[100];
string answer3[100];
string answer4[100];
//string correctIndex[100];
int correctIndex;

int switchStatePicker(int limit)
{

	return rand() % limit + 1;

}


bool validateUserAnswer(int answer, int guess)
{
	answer = correctIndex;
	guess = readUserInput();

	if (answer == guess) {
		return true;
	}
	return false;
}

bool isCorrectAnswer(string text) {
	
	int symbol = 3;

	if (text.at(symbol) == '@')
		return true;

	return false;
}

string removeLastChar(string text) {

	int length = text.length();

	text.erase(length - 1);

	return text;
}


void printQestionToConsole(int qIndex) {

	qIndex = switchStatePicker(6);

	cout << question[qIndex] << endl;
	cout << answer1[qIndex] << endl;
	cout << answer2[qIndex] << endl;
	cout << answer3[qIndex] << endl;
	cout << answer4[qIndex] << endl;
	cout << endl;

	//cout << correctIndex[qIndex] << endl;;




}

void readQuestionsFromFile() {

	string line = "";
	ifstream input;
	input.open(questionName);
	int i = 0;
	int totalQuestionCount = 0;

	while (!input.eof()) {

		getline(input, line);
		question[i] = line;

		getline(input, line);
		if (isCorrectAnswer(line)) {
			correctIndex = 1;
			line = removeLastChar(line);
		}
		answer1[i] = line;

		getline(input, line);
		if (isCorrectAnswer(line)) {
			correctIndex = 2;
			line = removeLastChar(line);
		}
		answer2[i] = line;;

		getline(input, line);
		if (isCorrectAnswer(line)) {
			correctIndex = 3;
			line = removeLastChar(line);
		}
		answer3[i] = line;;

		getline(input, line);
		if (isCorrectAnswer(line)) {
			correctIndex = 4;
			line = removeLastChar(line);
		}
		answer4[i] = line;	
		i++;
	}
	totalQuestionCount = i + 1;
}

void fileReader() 
{
	
	ifstream myfile;

	switch (1) 
	{

	case 1: 
		myfile.open("Question 1.txt");

	case 2: 
		myfile.open("Question 2.txt");

	case 3:
		myfile.open("Question 3.txt");

	case 4: 
		myfile.open("Question 4.txt");

	case 5:
		myfile.open("Question 5.txt");

	case 6:
		myfile.open("Question 6.txt");

	case 7:
		myfile.open("Question 7.txt");

	}

}

char answerFinder()
{

	return 'c';

}

bool convertStr2Int(string stringValue, int &intValue)
{
	stringstream str;
	bool valid = true;
	str << stringValue;
	str >> intValue;
	if (str.fail())
		valid = false;
	return valid;
}

int readUserInput()
{

	string testAnswer;
	int number;
	do {

		cout << "Please, enter your answer (1, 2, 3, 4): " << endl;
		getline(cin, testAnswer);
	} while (!convertStr2Int(testAnswer, number));

	return number;

}


And the Questions .txt: 

What is the capital of Vermont?
1. @Montpelier 
2. Burlington 
3. Essex 
4. Manchester 
What is the capital of New Jersey?
1. Red Bank 
2. Hoboken 
3. Jersey City 
4. @Trenton 
What is the capital of Oregon?
1. Portland 
2. Roseburg 
3. @Salem 
4. Milwaukie 
What is the capital of Michigan?
1. Detroit 
2. @Lansing 
3. Fling 
4. Southfield 
What is the capital of Pennsylvania?
1. Philadelphia 
2. Pittsburgh 
3. Scranton 
4. @Harrisburg 
What is the capital of Alabama?
1. @Montgomery 
2. Birmingham 
3. Mobile 
4. Auburn 
What is the capital of North Dakota?
1. Fargo 
2. Mandan 
3. @Bismarck 
4. Grand Forks 


Last edited on
I've used a different tack to reading the file, viz. with a struct and looping every five lines – the first line is the question and then we use std::string::find to see which of the 4 lines has the character '@' and use the question line and part of this latter, answer, line to construct the struct objects and emplace them in a std::vector:
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
#include <iostream>
#include <string>
#include <fstream>
#include <utility>
#include <vector>

struct QandA
{
    std::string m_question;
    std::string m_answer;
};

int main()
{
    std::ifstream inFile{"D:\\test.txt"};
    std::vector<QandA> quiz{};
    while(inFile)
    {
        std::string query{}, option1{}, option2{}, option3{}, option4{};
        getline(inFile, query);
        std::vector<std::string> tempStrings{};
        getline(inFile, option1);   tempStrings.push_back(std::move(option1));
        getline(inFile, option2);   tempStrings.push_back(std::move(option2));
        getline(inFile, option3);   tempStrings.push_back(std::move(option3));
        getline(inFile, option4);   tempStrings.push_back(std::move(option4));
        //http://en.cppreference.com/w/cpp/utility/move

        for (const auto& elem : tempStrings)
        {
            if(elem.find('@') != std::string::npos)
            //http://en.cppreference.com/w/cpp/string/basic_string/find
            {
                if(inFile)quiz.emplace_back(QandA{query, elem.substr(elem.find('@')+1)});
                //http://en.cppreference.com/w/cpp/container/vector/emplace_back
            }
        }
    }
    for (const auto& elem : quiz) std::cout << elem.m_question << ": " << elem.m_answer << "\n" ;
}

edit: you could make the code more efficient by iterating the vector tempStrings until the character '@' has been found in one of its members and then breaking instead of reading all its elements since each question has unique answer
Last edited on
Topic archived. No new replies allowed.