My assignment says that I have to Put sales2.dat in the project folder that contains the program file. I'm having a hard time interperting that statement. I scoured my book and forums, but still am stumped.
#include <iostream>
#include <fstream>
#include <iomanip>
usingnamespace std;
int main()
{
constint NUM_DIVS = 3; // Number of divisions
constint NUM_QTRS = 4; // Number of quarters
double sales[NUM_DIVS][NUM_QTRS]; // 2D array with 3 rows & 4 columns
double totalSales = 0; // Accumulates total sales
int div, qtr; // Loop counters
ifstream datafile; // Used to read data from a file
datafile.open("sales2.dat");
if (!datafile)
cout << "Error opening data file.\n";
else
{
cout << fixed << showpoint << setprecision(2);
cout << "Quarterly Sales by Division\n\n";
// Nested loops are used to fill the array with quarterly
// sales figures for each division and to display the data
for (div = 0; div < NUM_DIVS; div++)
{ for (qtr = 0; qtr < NUM_QTRS; qtr++)
{
cout << "Division " << (div + 1)
<< ", Quarter " << (qtr + 1) << ": $";
datafile >> sales[div][qtr];
cout << sales[div][qtr] << endl;
}
cout << endl; // Print blank line.
}
datafile.close();
// Nested loops are used to add all the elements
for (div = 0; div < NUM_DIVS; div++)
{ for (qtr = 0; qtr < NUM_QTRS; qtr++)
totalSales += sales[div][qtr];
}
// Display the total
cout << "The total sales for the company are: $";
cout << totalSales << endl;
}
return 0;
}
I actually changed it a txt file ,but I'm still having trouble reading it. I did a similar project and i was able to write it to a file. The txt file does exist I'm just not sure whats going on. I have the info set in notepad and it's saved, I then closed the file and try to read it with my program. If I'm missing a step please let me know. Thanks guys. Oh I don't believe I need it in binary mode.
OH !!! if the data stored there is in binary form, than you cant read it directly .....
beczuse,
in binary filing the data is stored as it is , for example if there is number 1234567 in bin binary file it will be saved as one integer ,( if data type is int ) ,
while if you store it in txt file it is stored as a single character here 1 , 2 and all will be treated as different characters ...