Reading specific lines from file

I am working on a homework assignment and i am stuck and was hoping that someone could point me in the right direction. This is what i have so far and this is what we are being asked to do.
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 “? “

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	ifstream inputFile;
	string fName;
	string str;

	cout << "Enter the file name: " << endl;
	getline(cin, fName);

	inputFile.open(fName.c_str());

	getline(cin, str);
	cout << str << endl;
Last edited on
Hello mxracer321,

You have a good start, but off a bit. This should give you an idea of what you need:
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
#include <iostream>
#include <string>
#include <fstream>
#include <chrono>
#include <thread>

//using namespace std;  // <--- Best not to use.

int main()
{
	std::ifstream inputFile;
	std::string fName, question,answer, junk;

	std::cout << "Enter the file name: " << std::endl;
	std::getline(std::cin, fName);

	inputFile.open(fName);  // <--- Some how I deleted this line. Now its back.

	if (!inputFile)
	{
		std::cout << "\n File \"" << fName << "\" did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		return 1;  //exit(1);  // If not in "main".
	}

	std::getline(inputFile, junk);  // <--- Reads the first line and is not used.

	std::getline(inputFile, question);
	std::getline(inputFile, answer);

	std::cout << question << "? ";
	// <--- Add for input of answer and other processing.

	return 0;
}

Line 20 is optional along with the header files "chrono" and "thread". With my IDE's set up I use this to keep the console window open long enough to read the message.

When opening a file stream for input it is a must to check and make sure it is open before you try to use it. Not quite as necessary with a stream for output as if the file does not exist it will create the file before writing to it. It is still a good idea to check if an output file is open because it can fail to open, but harder for this to happen.

Hope this gives you a better idea of what to do,

Andy

Edit:
Last edited on
So my teacher wants us to use namespace std, im not sure why. So i tried what you said but when i compile the program the only thing that it outputs is the number 4.
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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	ifstream inputFile;
	string fName, question, answer, junk;

	cout << "Enter the file name: " << endl;
	getline(cin, fName);

	inputFile.open(fName);

	getline(inputFile, junk);

	getline(inputFile, question);
	getline(inputFile, answer);

	cout << question << "?";




	cin.ignore(100, '\n');
	cin.get();
	return 0;
}
Hello mxracer321,

According to your instructions line 22 is wrong. You need a space after the "?".

After line 22 you need to prompt the user for their answer. Following the instructions this all you need to do for now, but you could add code to actually get the user's answer and compare it to the "answer" read from the file.

Line 27 is most likely to cause you to press "Enter" twice as you do not use "cin >>" and there is nothing to ignore.

The way I read the instructions this is a very simple program for now. Just open the file, read and prompt the user with the first question.

You are most of the way there. You just need to finish it.

Any questions let me know.

Hope that helps,

Andy
Hey Andy, so the program is actually much larger than what i posted but i just needed to start somewhere. But like i said in the last post, when i compiled what i had so far, instead of it outputting the question in the file, which should have been "child? ". it output "4".

Here is the full list of instructions:
For this part, the program will
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()
Hello mxracer321,

I understand the bigger program. I will look into the added pars shortly.

Using your input file what I did down to line 22 worked.

The only two things I can think of is the input file is not right and there is more after the "4" than you can see. Or something went wrong in reading the file.

What I copied and pasted into my input file check out with the first line being "4" and a new line character combo. followed by the first question.

Short of actually seeing what is on your computer you could try removing the "4" from the input file and putting a comment on line 17 and see what happens.

Another choice is to put a break point in the program and see what the variables "question", "answer" and "junk" contain.

It leads me to believe that line 17 is not reading the first line properly.

You could also follow the lines that read the file with "cout" statements printing each variable to see what you get.

Hope that helps,

Andy
Topic archived. No new replies allowed.