Part one takes in quarterly sales figures and writes it to a file.
Part two reads that file and displays the sales figures.
The program works just fine except it crashes on exit of part 2
I read something about zero null pointers but can't see where it
could apply in my case
/*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
usingnamespace std;//global namespace to avoid name clash
/*Structures*/
struct division
{
string divisionName;
double QSales[4];//array of doubles to hold sales figures
};
//***Function Prototypes
division getValues(const string &name);
void displayValues(division);
//***C++ starting point
int main()
{
division d1,d2,d3,d4;//4 instances of structure division
d1 = getValues("North");//name d1 in function call
d2 = getValues("East");//name d2 in function call
d3 = getValues("South");//name d3 in function call
d4 = getValues("West");//name d4 in function call
displayValues(d1);//function call to see what was typed in
displayValues(d2);// "
displayValues(d3);// "
displayValues(d4);// "
ofstream output;//start ofstream with the name output
output.open("salesReport.dat", ios::binary | ios::out);//open file or create if not exist
//use binary format and overwrite existing data
output.write(reinterpret_cast<char *>(&d1), sizeof(d1));//write d1 to file
output.write(reinterpret_cast<char *>(&d2), sizeof(d2));//write d2 to file
output.write(reinterpret_cast<char *>(&d3), sizeof(d3));//write d to file
output.write(reinterpret_cast<char *>(&d4), sizeof(d4));//write d4 to file
output.close();
system("PAUSE");//press any key
return 0;//exit
}
division getValues(const string &name)//function to fill sales array
{
division div;
div.divisionName = name;//temp variable for division names
for (unsigned i=0; i < 4; ++i)//loop through all divisions and quarters
{
cout << name << endl;//put out division name
cout << "Enter sales for quarter " << i+1 << ":\n "<<endl;//prompt for sales figures
cin >> div.QSales[i];//take in sales figures
}
return div;//return values
}
void displayValues(division d)//display function to test what was enterd
{
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;
}
You are trying to write and read and string as binary data, which won't work because a string is not a "plain-old data" type. For a program like this, you're better off just writing the data as text, though you could modify your saving code to save/load a char* to and from a string.
division getValues(constchar &name)//function to fill sales array
{
division div;
div.divisionName = name;//temp variable for division names
for (unsigned i=0; i < 4; ++i)//loop through all divisions and quarters
{
cout << i<<". division" << endl;//put out division name
cout << "Enter sales for quarter " << i+1 << ":\n "<<endl;//prompt for sales figures
cin >> div.QSales[i];//take in sales figures
}
return div;//return values
}
my changes broke the line
div.divisionName = name;//temp variable for division names
char divisionName[5];
Remember a character string such as "North" has a null terminator, so it requires one extra byte. divisionName needs to be at least 6 characters in length - though that sounds a bit short for a typical name. 20 to 30 characters might be more usual.