I have an assignment due tomorrow at midnight so I have 24 hours to finish this.
Assignment: First, you should use Notepad (or any other text editor) to create a file ahead of time. The file should have 6 double values on separate lines. These values represent the earnings of a person for each of the first 6 months in a year; thus each double value will have exactly two places to the right of the decimal. Name this file, "earnings.txt", and save it to your desktop.
Your program should open and then read the 6 double values from this file into an array named 'earnings'. Use a 'for' loop to do this. Then use another 'for' loop to display the earnings as they were in the file.
After this, sort the array , and then display the earnings in sorted order.
Finally, compute and display the average of the earnings.
I have had no problem with doing the first part of creating the file, getting the file to be read by my program and listing all the number in a list format.
Example text file:
1012.30
1400.71
1250.78
5000.40
3050.70
2134.88
Example Output (currently):
The earnings from the file are:
1012.30
1400.71
1250.78
5000.40
3050.70
2134.88
I need to figure out this portion:
After sorting the earnings are:
1012.30
1250.78
1400.71
2134.88
3050.70
5000.40
The average earnings for the half year is $2308.29
My
MAIN PROBLEM is I cannot figure out how to read each line and store it into an array. I have tried various methods but since I am using a string to read the lines, I always get errors if I try to make it an double.
Heres my code so far:
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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string line;
ifstream myfile ("C:\\Users\\AFTEREFFECTS\\Desktop\\earnings.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else
{
cout << "Unable to open file";
}
return 0;
}
|
Any help will be greatly appreciated! Thank you!