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:
Example Output (currently):
The earnings from the file are:
I need to figure out this portion:
After sorting the earnings are:
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:
using namespace std;
int main ()
ifstream myfile ("C:\\Users\\AFTEREFFECTS\\Desktop\\earnings.txt");
while ( myfile.good() )
cout << line << endl;
cout << "Unable to open file";
Any help will be greatly appreciated! Thank you!