Hey guys, long time reader, first time poster! So, my issue is that I need to turn in my last project for a grade. After hours spent on googling, reading chapter after chapter, and teacher PowerPoints I am mentally drained. I know the answer is in front of me but can't see it. So here it is! Here is my problem and code. PLEASE HELP LOL!
Write a program to read the data file, websiteHits.dat. There are 3 fields in each record:
Read the fields into a structure one record at a time.
The while loop in main will look like this to process the file:
while(!inFile.eof())
}
record = GetData(inFile);
..… GetData is the name of the function that reads the data. inFile is the name of the websiteHits file. record is the name of the structure variable that contains the url, revenue, and hits members.
Create a screen report with a title, each record listed on a new line, and the following totals:
Total revenue, Total hits, Number of records in the file (this is a counter).
Use a structure definition for the totals. You can increment the totals in main.
First: int revenue; //line 11 should be double revenue; right?
Second: what are you trying to do in your GetData function on lines 57-59; at this point in the function inFile will have hit an EOF charecter and can't read anymore. Did you want to make myURL a stringstream and then read from the stringstrem instead of inFile?
Well, at this point I need to read the data in the dat file print it to the screen. I cant get the program to read through the file and sort the data. I was using the while loop to sort through each line . Short answer is ...i actually was just trying anything at this point.
Record GetData(ifstream& inFile) {
Record web;
inFile >> web.url
>> web.revenue
>> web.hits; //note only the one semicolon here
// all three lines are a single statement
return web;
}
//It's simpler then your making it; the whole function is being looped,
// so you don't need another loop in the function too.
I don't know if this will fix your problem but you can replace line 28 while( !inFile.eof() ) with while( inFile.good() ) to support a more general case (like if the file couldn't be opened, or was corrupted somehow.)
//line 26
ifstream inFile("websiteHits.dat"); //opens the input stream on the specified file
//inFile.open("websiteHits.dat"); //You don't need to open the file twice!
1 2 3 4 5
//line 41
//Don't forget to close the file!
inFile.close();
system("pause");
return 0;