Aug 23, 2012 at 9:30am UTC
i wanted to do like a get line to print out the text from a .txt file but after printing the first question and after answering, it prints out the next one but when i enter the answer it does not respond and just keep allowing me to cin.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main () {
vector<string> questionset;
vector<string> choices;
int question_no;
int answer;
int correct = 0;
string line;
ifstream myfile ("Questions.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
if ( line[0] == '-')
questionset.push_back(line);
if ( line[0] == 'a' || line[0] == 'b' || line[0] == 'c' )
choices.push_back(line);
}
myfile.close();
}
else cout << "Unable to open file";
for (int x = 0; x < 10; x++)
{
cout << questionset[x];
cout << "\n";
for (int y = 0; y < 3; y++)
{
question_no = 3 * x + y;
cout << choices[question_no];
cout << "\n";
if (choices[question_no * 4 + y][2] == '#')
correct = y + 1;
}
while(true){
cin >> answer;
if (answer == correct) {
correct = NULL;
break;
}
}
}
return 0;
}