Write to a file and read from a file

I have created a class and a few objects using constructors, I am simply trying to create code that will open and read my output to a .txt file. Currently, the txt file will open, but nothing will write to the file. If i add a fout statement in the function such as fout << "hi"; that will write to the file. But my objects will not. Please help. Thank you

All code
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <sstream> //overloaded string for binary >>
#include <iomanip> //setprecision
#include <fstream> //ofstream ifstream
#include <string>
using namespace std;

class JellyBean
{
	friend JellyBean operator + (JellyBean thefirstJellyBean, JellyBean thesecondJellyBean);
	friend JellyBean operator - (JellyBean thefirstJellyBean, JellyBean thesecondJellyBean);
	friend JellyBean operator * (JellyBean thefirstJellyBean, JellyBean thesecondJellyBean);
	friend JellyBean operator / (JellyBean thefirstJellyBean, JellyBean thesecondJellyBean);
	friend bool operator < (JellyBean thefirstJellyBean, JellyBean thesecondJellyBean);
	friend bool operator > (JellyBean thefirstJellyBean, JellyBean thesecondJellyBean);
	friend bool operator == (JellyBean thefirstJellyBean, JellyBean thesecondJellyBean);
	friend ostream& operator << (ostream& outputStream, JellyBean JellyBeanToPrint);
	friend istream& operator >> (istream& inputStream, JellyBean& JellyBeanToInput);

public:
	JellyBean();
	JellyBean(int);
	JellyBean(string, string, int);
	void output();
	
private:
	string flavor;
	string color;
	int quantity;

};//End Class
JellyBean::JellyBean()
{
	flavor = "null";
	color = "null";
	quantity = 0;
}//End default constructor
JellyBean::JellyBean(int new_quantity)
{
	flavor = "null";
	color = "null";
	quantity = new_quantity;
}//End constructor with one int
JellyBean::JellyBean(string new_flavor, string new_color, int new_quantity)
{
	flavor = new_flavor;
	color = new_color;
	quantity = new_quantity;
}//End constructor with two strings and one int

//---------------------------JellyBean Operator Overload Functions  -----------------------------
//  Function:  JellyBean operator functions
//
//  Purpose:  Overloads different operators to allow for comparison checks, reading in, and the 
//			  output of private data members.
//
//  Parameters: One or two based upon unary or binary operator.
//	
//
//  Returns:  Streams for reading in or output, boolean for comparisons
//			  and manipulated private data member variables.
//----------------------------------------------------------------------------------------------
JellyBean operator + (JellyBean thefirstJellyBean, JellyBean thesecondJellyBean)
{
	return JellyBean (thefirstJellyBean.flavor + " plus " + thesecondJellyBean.flavor, thefirstJellyBean.color + " plus " + thesecondJellyBean.color,
					  thefirstJellyBean.quantity + thesecondJellyBean.quantity);
}//End overloaded + 
JellyBean operator - (JellyBean thefirstJellyBean, JellyBean thesecondJellyBean)
{
	return JellyBean (thefirstJellyBean.flavor + " minus " + thesecondJellyBean.flavor, thefirstJellyBean.color + " minus " + thesecondJellyBean.color,
					  thefirstJellyBean.quantity - thesecondJellyBean.quantity);
}//End overloaded -
JellyBean operator * (JellyBean thefirstJellyBean, JellyBean thesecondJellyBean)
{
	return JellyBean (thefirstJellyBean.flavor + " times " + thesecondJellyBean.flavor, thefirstJellyBean.color + " times " + thesecondJellyBean.color,
					  thefirstJellyBean.quantity * thesecondJellyBean.quantity);
}//End overloaded *
JellyBean operator / (JellyBean thefirstJellyBean, JellyBean thesecondJellyBean)
{
	return JellyBean (thefirstJellyBean.flavor + " divided by " + thesecondJellyBean.flavor, thefirstJellyBean.color + " divided by " + thesecondJellyBean.color,
					  thefirstJellyBean.quantity / thesecondJellyBean.quantity);
}//End overloaded /
bool operator < (JellyBean thefirstJellyBean, JellyBean thesecondJellyBean)
{
	return (thefirstJellyBean.quantity < thesecondJellyBean.quantity);
}//End overloaded <
bool operator > (JellyBean thefirstJellyBean, JellyBean thesecondJellyBean)
{
	return (thefirstJellyBean.quantity > thesecondJellyBean.quantity);
}//End overloaded >
bool operator == (JellyBean thefirstJellyBean, JellyBean thesecondJellyBean)
{
	return ((thefirstJellyBean.quantity == thesecondJellyBean.quantity) && (thefirstJellyBean.flavor == thesecondJellyBean.flavor)
		&& (thefirstJellyBean.color == thesecondJellyBean.color));
}//End overloaded ==
ostream& operator << (ostream& outputStream, JellyBean JellyBeanToPrint)
{
	cout <<  JellyBeanToPrint.flavor << " in flavor, "
		 << JellyBeanToPrint.color << " in color, " << endl
		 << "and I have " << JellyBeanToPrint.quantity << " jellybean(s)." << endl;
	return outputStream;
}//End overloaded <<
istream& operator >> (istream& inputStream, JellyBean& JellyBeanToInput)
{
	cout << "Enter a flavor, color, and quantity of jellybeans each" << endl
		 << "separated by pressing enter" << endl;
	cin >> JellyBeanToInput.flavor >> JellyBeanToInput.color >> JellyBeanToInput.quantity;
	return inputStream;
}//End overloaded >>
void writeToFile(JellyBean outputStream)
{
	ofstream fout;
	fout.open("file.txt", ios::app);

	if(fout.fail())
	{
		cout << "the file did not open" << endl;
		exit(1);
	}
	else
	{
	//fout.setf(ios::fixed);
	//fout.setf(ios::showpoint);
	fout.precision(2);
	
	fout << setw(15) << outputStream;
	}
	fout.close();
}
int main()
{
	JellyBean flav1("Vanilla", "White", 25);			//JellyBean objects created to be tested with overloaded operators
	JellyBean flav2("Cherry", "Red", 30);
	JellyBean flav3;
	JellyBean flav4;
	JellyBean flav5("Apple", "Green", 100);
	JellyBean flav6("Chocolate", "Brown", 1000);
	JellyBean flav7;
	JellyBean flav8;
	JellyBean flav9;
	JellyBean flav10;
	JellyBean flav11;
	JellyBean flav12;
	writeToFile(flav1);


write to file function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void writeToFile(JellyBean outputStream)
{
	ofstream fout;
	fout.open("file.txt", ios::app);

	if(fout.fail())
	{
		cout << "the file did not open" << endl;
		exit(1);
	}
	else
	{
	//fout.setf(ios::fixed);
	//fout.setf(ios::showpoint);
	fout.precision(2);
	
	fout << setw(15) << outputStream;
	}
	fout.close();
}

Last edited on
Haven't finished looking through your program yet, but all of the operators you have overloaded, other than the input and output ones, really should be part of your JellyBean class, rather than being global functions made friends of the class. Also, making your parameter names shorter, like first and second, will make this easier to read.

Edit: Found your problem. In your overloaded << operator, you need to use outputStream instead of cout.
Last edited on
made suggested change but same results.
1
2
3
4
5
6
7
ostream& operator << (ostream& outputStream, JellyBean JellyBeanToPrint)
{
	outputStream <<  JellyBeanToPrint.flavor << " in flavor, "
		 << JellyBeanToPrint.color << " in color, " << endl
		 << "and I have " << JellyBeanToPrint.quantity << " jellybean(s)." << endl;
	return outputStream;
}//End overloaded << 


I am actually confused by that change but i will stick with it to keep troubleshooting my problem. It seems to want to write, asking me if i want to save changes to the txt file (even though no changes have been made), any other suggestions?

Thank you,
Any ideas?
Topic archived. No new replies allowed.