This is a homework problem, and I know we aren't supposed to post about these kind of questions, but I have all the code written out except for I am not getting the output file as expected.
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
usingnamespace std;
struct division
{
char divisionName[7];
int quarter;
double quarterlySales;
};
int main()
{
fstream file("corp.txt", ios::out | ios::binary);
division north, east, west, south;
//Store names of divisions
strcpy (north.divisionName, "North");
strcpy (east.divisionName, "East");
strcpy (south.divisionName, "South");
strcpy (west.divisionName, "West");
cout << "Enter the quarterly sales for the North division.\n";
for (int k = 1; k <= 4; k++)
{
north.quarter = k;
cout << "Enter quarter " << k << " sales: ";
cin >> north.quarter;
while (north.quarter < 0)
{
cout << "The sales figures cannot be less than 0. Re-enter the sales figures for quarter " << k << ": ";
cin >> north.quarter;
}
file.write(reinterpret_cast<char *> (&north.quarter), sizeof(north));
}
cout << endl;
cout << "Enter the quarterly sales for the East division.\n";
for (int k = 1; k <= 4; k++)
{
east.quarter = k;
cout << "Enter quarter " << k << " sales: ";
cin >> east.quarter;
while (east.quarter < 0)
{
cout << "The sales figures cannot be less than 0. Re-enter the sales figures for quarter " << k << ": ";
cin >> east.quarter;
}
file.write(reinterpret_cast<char *> (&east.quarter), sizeof(east));
}
cout << endl;
cout << "Enter the quarterly sales for the South division.\n";
for (int k = 1; k <= 4; k++)
{
south.quarter = k;
cout << "Enter quarter " << k << " sales: ";
cin >> south.quarter;
while (south.quarter < 0)
{
cout << "The sales figures cannot be less than 0. Re-enter the sales figures for quarter " << k << ": ";
cin >> south.quarter;
}
file.write(reinterpret_cast<char *> (&south.quarter), sizeof(south));
}
cout << endl;
cout << "Enter the quarterly sales for the West division.\n";
for (int k = 1; k <= 4; k++)
{
west.quarter = k;
cout << "Enter quarter " << k << " sales: ";
cin >> west.quarter;
while (west.quarter < 0)
{
cout << "The sales figures cannot be less than 0. Re-enter the sales figures for quarter " << k << ": ";
cin >> west.quarter;
}
file.write(reinterpret_cast<char *> (&west.quarter), sizeof(west));
}
cout << endl;
return 0;
}
This code works and everything but the txt file I am getting is just a jumbled mess that I can't make sense of.
I expect this code to write to a file and output each division name followed by the 4 numbers I enter that correspond to the division.
Instead the output file I get reads:
8þa Õ¢îo°Ýðo 8þa Õ¢îo°Ýðo 8þa Õ¢îo°Ýðo 8þa Õ¢îo°Ýðo þa North þa North þa North þa North hðo West hðo West hðo West hðo West ðo ðoz'ïoEast ðo ðo ðoz'ïoEast ðo ðo ðoz'ïoEast ðo ðo ðoz'ïoEast ðo