I am trying to write this program but i am struggling tremendously. I am not very good at c++ and i was hoping someone could point me in the right direction. Here are my instructions.
Prompt the user for a file that contains the questions – use getline(cin, fname) instead of cin >> fname to read the file name from the user. This will keep you in sync with the user’s input for later in the program
The first line is the number of questions (just throw this line away this week)
The second line is question 1
The third line is answer 1
The fourth line is question 2
The fifth line is answer 2 and so on,
Read the first question/answer from the file
Note you can use getline(yourInputStream, yourString) to read an entire line
Prompt the user for the answer to the question
The prompt is the question from the file, followed by “? “
The user gets 3 tries to get it correct
Make an enum to remember whether the user is answering, correct, or incorrect
Initially the user is answering
If answered incorrectly 3 times, then the user is incorrect
If answered correctly, then the user is correct
Checking if the answer is correct includes some string manipulation
Take the line that the user entered, remove all leading whitespace, remove all trailing whitespace, and convert everything to lowercase
By doing this, you can simply compare the user’s answer to the correct answer with an ==
Note: isspace(c) returns true if c is a whitespace character like a space or tab, false otherwise
Use functions as appropriate – you should have more than just main()
This is what the .txt file contains:
4
child
small fry
happy
tickled pink
mock
poke fun at
dough puncher
baker
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
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
enum ChoiceType { ANSWERING, CORRECT, INCORRECT};
int main()
{
ifstream inputFile;
string fName, question, answer, junk, guess;
cout << "File?" << " ";
getline(cin, fName);
inputFile.open(fName);
getline(inputFile, junk);
getline(inputFile, question);
getline(inputFile, answer);
cout << question << "? ";
cin >> guess;
cout << endl;
cin.ignore(100, '\n');
cin.get();
return 0;
}
|