I need to let the user input a file name/location ex: c:/school/cs-121/labData.txt
the lab data is formatted like this
149.99 3250
99.99 15587
49.99 18564
39.99 23450
I need to take that information and figure out total tickets sold and total money made but that part is easy. I just need to get the data file in the some how and separate the data into variables I can use. I got the declarations like this...
but using the input file data. I have been searching for hours and our text book doesn't have any good examples of how to do this. Please don't write the entire thing for me, just how to accept the input from the user and assign a value using the file and any necessary declarations or include statements.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main(void)
{
double fDouble;
int fInt;
string fString;
ifstream myfile;
string fileLocation;
//gets file location from the user
cout << "Please enter the location of the file: ";
getline(cin, fileLocation);
//opens the file
myfile.open(fileLocation);
//makes sure that the file opened properly
while(myfile.fail())
{
cout << "The file at location " << fileLocation << " failed to open" << endl;
cout << "Please enter the location of the file: ";
getline(cin, fileLocation);
myfile.open(fileLocation);
}
//extracts data from the file
myfile >> fDouble >> fInt >> fString;
//outputs data to the screen
cout << endl << fDouble << endl << fInt << endl << fString;
cin.ignore();
return 0;
}
OK I got it. I did search for a long time but I was still confused on how to read in the files data properly. A friend in class helped me out a tiny bit today and I got it to work. Here is what it ended up looking like.