First of all: Thanks for taking time to help!
I have 2 programs that are accessing one text file.
The first one is supposed to take in the quarterly sales figures for each of the four divisions of a company.
The second one is to display the report.
My problem is the output of the second file:
Every item is:
-9.25596e+061
I am trying for a while now to figure what I'm doing wrong.
Could it be the file path? After running the first program, I tossed the text file in the solution folder of the second program. Thinking it should find it.
Or do I mix up reference and value? I tried to change the & to a * but that broke the code.
Here is the code that writes to the file.
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
|
/*Headers*/
#include <iostream>//needed 4 input/output
#include <iomanip>//needed to round
#include <string>//needed 4 strings
#include <fstream>//needed to write output into a file
using namespace std;//global namespace to avoid name clash
/*Structures*/
struct division
{
string divisionName;
double QSales[4];
};
//***Function Prototypes
division getValues(const string &name);
void displayValues(division);
//***C++ starting point
int main()
{
division d1,d2,d3,d4;
d1 = getValues("North");
d2 = getValues("East");
d3 = getValues("South");
d4 = getValues("West");
displayValues(d1);
displayValues(d2);
displayValues(d3);
displayValues(d4);
ofstream output;
output.open("salesReport.txt", ios::binary | ios::app);
output.write(reinterpret_cast<char *>(&d1), sizeof(d1));
output.write(reinterpret_cast<char *>(&d2), sizeof(d2));
output.write(reinterpret_cast<char *>(&d3), sizeof(d3));
output.write(reinterpret_cast<char *>(&d4), sizeof(d4));
output.close();
system("PAUSE");//press any key
return 0;//exit
}
division getValues(const string &name)
{
division div;
div.divisionName = name;
for (unsigned i=0; i < 4; ++i)
{
cout << name << endl;
cout << "Enter sales for quarter " << i+1 << ":\n "<<endl;
cin >> div.QSales[i];
}
return div;
}
void displayValues(division d)
{
cout << "Division Name: " << d.divisionName<<endl;
cout << "First quarter sales: " << d.QSales[0]<<endl;
cout << "Second quarter sales: " << d.QSales[1]<<endl;
cout << "Third quarter sales: " << d.QSales[2]<<endl;
cout << "Fourth quarter sales: " << d.QSales[3]<<endl;
cout << endl;
}
|
And here is the reading 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
|
/*Headers*/
#include <iostream>//needed 4 input/output
#include <iomanip>//needed to round
#include <string>//needed 4 strings
#include <fstream>//needed to write output into a file
using namespace std;//global namespace to avoid name clash
/*Structures*/
struct division
{
string divisionName;
double QSales[4];
};
//***Function Prototypes
void displayValues(division);
//***C++ starting point
int main()
{
division d1,d2,d3,d4;
ifstream input;("salesReport.txt",ios::binary | ios::in);
input.read(reinterpret_cast<char *>(&d1), sizeof(d1));
input.read(reinterpret_cast<char *>(&d2), sizeof(d2));
input.read(reinterpret_cast<char *>(&d3), sizeof(d3));
input.read(reinterpret_cast<char *>(&d4), sizeof(d4));
input.close();
displayValues(d1);
displayValues(d2);
displayValues(d3);
displayValues(d4);
system("PAUSE");//press any key
return 0;//exit
}
void displayValues(division d)
{
cout << "Division Name: " << d.divisionName<<endl;
cout << "First quarter sales: " << d.QSales[0]<<endl;
cout << "Second quarter sales: " << d.QSales[1]<<endl;
cout << "Third quarter sales: " << d.QSales[2]<<endl;
cout << "Fourth quarter sales: " << d.QSales[3]<<endl;
cout << endl;
}
|