Help Understanding Bool statement

Hello all,

I need help understanding what is happening in a code I wrote. This is the answer I wrote for the following question:

"Write a program that gives and takes advice on program writing. The pro-gram starts by writing a piece of advice to the screen and asking the user to type in a different piece of advice. The program then ends. The next person to run the program receives the advice given by the person who last ran the program. The advice is kept in a file, and the contents of the file change after each run of the program. You can use your editor to enter the initial piece of advice in the file so that the first person who runs the program receives some advice. Allow the user to type in advice of any length so that it can be any number of lines long. The user is told to end his or her advice by pressing the Return key two times. Your program can then test to see that it has reached the end of the input by checking to see when it reads two consecutive occurrences of the character '\n'."

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
#include <iostream>
#include < fstream>


std::ifstream fin;
std::ofstream fout;

void AdviceGiver(std::ifstream& fin);
void AdviceGetter(std::ifstream& fin, std::ofstream& fout);


int main()
{
	AdviceGiver(fin);
	AdviceGetter(fin, fout);
}


// Read the contents from the file and output on the screen
void AdviceGiver(std::ifstream& fin)
{
	fin.open("Advice.txt");
	if (fin.fail())
	{
		std::cout << "The input file has failed to open";
		exit(1);
	}
	
	char x;
	fin.get(x);
	while (!fin.eof())
	{
		
		std::cout << x;
		fin.get(x);
	}
	std::cout << '\n';
	
	fin.close();
}

//Obtain new input from user, replace old input of file with new input
void AdviceGetter(std::ifstream& fin, std::ofstream& fout)
{
	fout.open("Advice.txt");
	if (fout.fail())
	{
		std::cout << "The output file has failed to open";
	}
	
	std::cout << "Please type in your advice now, and press enter twice when you are finished" << std::endl;
	

	//While there is more input from the keyboard, Keep reading until two \n occur
	char x;
	std::cin.get(x);
	while (!fout.eof())
	{
		
		fout.put(x);
		std::cin.get(x);
		//check for double \n
		if (x == '\n')
		{
			std::cin.get(x);
			//if only one \n start a new line
			if (x != '\n')
			{
				fout << '\n';
			}
			//if two \n end the output
			else if (x == '\n')
			{
				break;
			}
		}
	}
	
	fin.close();
}




My question is about this following block of code. Can someone help me understand what triggers the bool statement in the while loop? Am I right in thinking it is saying "While there is more to be output, keep running the loop"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
char x;
	std::cin.get(x);
	while (!fout.eof())
	{
		
		fout.put(x);
		std::cin.get(x);
		//check for double \n
		if (x == '\n')
		{
			std::cin.get(x);
			//if only one \n start a new line
			if (x != '\n')
			{
				fout << '\n';
			}
			//if two \n end the output
			else if (x == '\n')
			{
				break;
			}
		}
	}


Also any comments/critiques would be well appreciated, thanks.
Last edited on
Can someone help me understand what triggers the bool statement in the while loop?

If you're talking about while (!fout.eof()), an output stream will probably never trigger eof().


Also any comments/critiques would be well appreciated, thanks.

Why the global variables?

Why are you passing stream reference variables into the functions? Why not just open a stream instance in the function, then you don't need to worry about closing the streams, the destructor will do it for you.

By the way the function parameters should really be std::istream or std::ostream instances, then you can use any type of stream like std::stringstream, or std::ifstream.

Last edited on
@jlb
Thanks for the response, I see what you are saying about localizing the stream functions, and I'll implement that. Right now the only streams I learned about were the ifstream and ofstream so good to know, thanks for the advice.
Topic archived. No new replies allowed.