how to read in .txt file input from user then use data in the text file

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...

double boxPrice;
double box;
double sidelinePrice;
double sideline;
double premiumPrice;
double premium;
double generalPrice;
double general;

I need to fill them in like this

double boxPrice = 149.99 ;
double box = 3250;
double sidelinePrice = 99.99;
double sideline = 15587;
double premiumPrice = 49.99;
double premium = 18564;
double generalPrice = 39.99;
double general = 23450;

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.
I have been searching for hours

To be honest, it is hard to believe. I'm sorry. :)

There are really tons of examples jumping out when you ask for "file reading in c++" from google:

http://www.cplusplus.com/doc/tutorial/files/

Here is an example for C:

http://www.phanderson.com/files/file_read.html

One more variant to read data from console as always and redirect console to read from file:

c:> myprogram.exe < inputfile.txt
Last edited on
input:
123.321
123
name


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
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <string>

using namespace 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;
}


output:
123.321
123
name



http://www.cplusplus.com/doc/tutorial/files/
Last edited on
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.


#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>


using namespace std;

int main()
{
double boxPrice, sidelinePrice, premiumPrice, generalPrice;
double box, sideline, premium, general;
double boxTotal, sidelineTotal, premiumTotal, generalTotal, totalTickets, totalDollers, avgPerTicket;
string filename;

ifstream inData;

cout << "please enter the location of the file you wish to input: " << endl;
getline(cin, filename);

inData.open(filename.c_str());

inData >> boxPrice >> box >> sidelinePrice >> sideline >> premiumPrice >> premium >> generalPrice >> general;

cout << "\n\nbox ticket price: $" << boxPrice << '\t' << "box tickets sold: " << box << endl;
cout << "sideline ticket price: $" << sidelinePrice << '\t' << "sideline tickets sold: " << sideline << endl;
cout << "premium ticket price: $" << premiumPrice << '\t' << "premium tickets sold: " << premium << endl;
cout << "general ticket price: $" << generalPrice << '\t' << "general tickets sold: " << general << endl;

boxTotal = (box * boxPrice);
sidelineTotal = (sideline * sidelinePrice);
premiumTotal = (premium * premiumPrice);
generalTotal = (general * generalPrice);
totalTickets = (box + sideline + premium + general);
totalDollers = (boxTotal + sidelineTotal + premiumTotal + generalTotal);
avgPerTicket = (totalDollers / totalTickets);


cout << fixed << showpoint << setprecision(2);
cout << "\n\ntotal $ from box tickets:" << '\t' << "$" << boxTotal << endl;
cout << "total $ from sideline tickets:" << '\t' << "$" << sidelineTotal << endl;
cout << "total $ from premium tickets:" << '\t' << "$" << premiumTotal << endl;
cout << "total $ from general tickets:" << '\t' << "$" << generalTotal << endl;
cout << noshowpoint << setprecision(0);
cout << "\ntotal tickets sold:" << '\t' << '\t' << totalTickets << endl;
cout << showpoint << setprecision(2);
cout << "average $ per ticket sold:" << '\t' << "$" << avgPerTicket << endl;
cout << "total $ from all ticket sales:" << '\t' << "$" << totalDollers << endl;



return 0;
}

It works good but if anyone has suggestions on how they would make it better I'm all ears.
Topic archived. No new replies allowed.