writing data to a file

This code runs but it is supposed to write the information to a file. I called it "corpsalesfile.txt" and created a blank project in the same folder as my program but it is not opening or writing it to the file. Anyone have any idea how to fix this?

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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
 #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;




	struct CorpData
	{
    string divisionName;
	    double firstQtr;
	    double secondQtr;
		double thirdQtr;
		double fourthQtr;
		double totalAnnual;
	    double averageQtr;
		
};
 
	int main()
	{
	 
	    CorpData east;
	    CorpData west;
	    
	     
	    fstream corpsales("corpsales.txt", ios::out);
	    if (!corpsales)
	    {
	        cout << "Error opening file.\n";
	        return 0;
	    }
	    else
	 
	    cout << "Enter the following information about the East Division\n";
	    cout << "Enter the the data for First Quarter :\n";
	    cin >> east.firstQtr;
	  while (east.firstQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> east.firstQtr;
	  }
	    cout << "Enter the the data for Second Quarter :\n";
	    cin >> east.secondQtr;
	     while (east.secondQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> east.secondQtr;
	  }
	    cout << "Enter the the data for Third Quarter :\n";
	    cin >> east.thirdQtr;
	    while (east.thirdQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> east.thirdQtr;
	  }
	    cout << "Enter the the data for Fourth Quarter :\n";
	    cin >> east.fourthQtr;
	     while (east.fourthQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> east.fourthQtr;
	  }
	     
	 
	 
	    cout << "Enter the following information about the West Division\n";
	    cout << endl;
	    cout << "Enter the the data for First Quarter :\n";
	    cin >> west.firstQtr;
	    while (west.firstQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> west.firstQtr;
	  }
	    cout << "Enter the the data for Second Quarter :\n";
	    cin >> west.secondQtr;
	     while (west.secondQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> west.secondQtr;
	  }
	    cout << "Enter the the data for Third Quarter :\n";
	    cin >> west.thirdQtr;
	     while (west.thirdQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> west.thirdQtr;
	  }
	    cout << "Enter the the data for Fourth Quarter :\n";
	    cin >> west.fourthQtr;
	     while (west.thirdQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> west.fourthQtr;
	  }
	
	     
	 


	// setAnnualSales
	
		double AnnualSalesEast;
		double AnnualSalesWest;
		AnnualSalesEast = (east.firstQtr + east.secondQtr + east.thirdQtr + east.fourthQtr);
		AnnualSalesWest = (west.firstQtr + west.secondQtr + west.thirdQtr + west.fourthQtr);
	

//setTotalSales
	
		double TotalSales;
			TotalSales = (east.firstQtr = east.secondQtr + east.thirdQtr + east.fourthQtr + west.firstQtr + west.secondQtr + west.thirdQtr + west.fourthQtr);
	

	
	
	// setDivSales
			double div1, div2, div3, div4;
		div1 = east.firstQtr + west.firstQtr;
		div2 = east.secondQtr + west.secondQtr;
		div3 = east.thirdQtr + west.thirdQtr;
		div4 = east.fourthQtr + west.fourthQtr;
	

	  corpsales.open("corpsalesfile.txt", ios::out);
		corpsales << cout << setw(10) << "Div."
			 << setw(10) << "Qtr1"
			 << setw(10) << "Qtr2"
			 << setw(10) << "Qtr3"
			 << setw(10) << "Qtr4"
			 << setw(10) << "TOTAL";
		 cout << setw(10) << "East"
			 << setw(10) << east.firstQtr
			 << setw(10) << east.secondQtr
			 << setw(10) << east.thirdQtr
			 << setw(10) << east.fourthQtr
			 << setw(10) << AnnualSalesEast;
	     cout << setw(10) << "West"
				<< setw(10) << west.firstQtr
				<< setw(10) << west.secondQtr
				<< setw(10) << west.thirdQtr
				<< setw(10) << west.fourthQtr
				<< setw(10) << AnnualSalesWest;
		 cout << setw(10) << "TOTAL"
			 << setw(10) << div1
			 << setw(10) << div2
			 << setw(10) << div3
			 << setw(10) << div4
			 << setw(10) << TotalSales;


		 corpsales.close();


		    return 0;
}
	 


closed account (LN7oGNh0)
I didnt read all of your code, but I think you need to do this:

1
2
3
4
ofstream file("file path");

file << "hello";  //this will get written to the file
cout << "hello"; //this will be output in the console 


Hoped that helped.
Last edited on
leave out the opening of the file at line 129.
The file is already opened when constructing the fstream and calling the 'open'-function again in this case will fail and 'corrupt' the stream. Any further operation on this fstream will fail as well, so no data is written.

Another suggestion: leave out the cout at lines 130, 136, 142 and 148. You will get weird data in your file as you are writing cout (stream to standard output = console) to the file, which doesn't make sense.
Topic archived. No new replies allowed.