Let me state it again. Make sure you can do each step before you try the next.
It looks like you are trying to do steps 2 and 3 without getting step 1 to work.
Step 1: For this step you don't need lines 10 - 12. The "isWanted" function is not needed at all, and is just screwing things up. In step 1 you only want to read 1 line, so the loop in line 21 is wrong. So, if you change lines 21 - 25 to a
getline
and a
cout <<
, you will have step 1 working.
Seriously, work is small steps and don't add stuff until you need it. My idea of step 1 is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <fstream>
int main(){
std::string sLine;
std::ifstream inFile("answers.txt");
if(!inFile){
std::cerr << "Error! cannot open the file";
return EXIT_FAILURE;
}
getline(inFile, sLine);
std::cout << sLine << std::endl;
return 0;
}
|
Error checking on line 13 would be more robust, but as an initial step to make sure you can read a file it is not necessary.
Step 2: Now you need your line 10 (sort of). However, answerKey will be an array of chars, not an array of strings. You will be reading in an array of chars, so you probably want to use a loop. (Hint: arrays and loops almost always go together.) So, the simplest thing to do is to change your getline call to a loop that reads 10 characters from the file.
Oh, and please change the hard-coded '10's in your code to a constant of some sort.
const int numAnswers = 10;