Need help C++ Project!

Pages: 12
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:

website url (string)
revenue (double)
hits (integer)

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.

The program should contain these functions:

DisplayTitle
GetData
DisplayTotals


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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;


struct Record
{
       string websiteUrl;
       double revenue;
       int hits;
};

Record GetData(ifstream& inFile);
void DisplayTotals( Record web );


int main()
{
    Record web;
    int totalHits = 0;
    int totalRev = 0;
    
    
    ifstream inFile("websiteHits.dat");
    inFile.open("websiteHits.dat");
    while(!inFile.eof())
    {
         Record web = GetData(inFile);
         //totalHits = GetData(inFile);
        //totalRev = GetData(inFile);
        totalHits += web.hits;
        totalRev += web.revenue;
        
        cout << "This is the Total Hits: " << totalHits << endl;
        cout << "This is the Total Rev: " << totalRev;
        
    }
    
    
    system("pause");
    return 0;
}

Record GetData(ifstream& inFile)
{
       Record web;
      	
	string myURL;
	string nextvictim = " ";
	while(!inFile.eof())
	{
		inFile >> myURL;
		myURL.append(nextvictim);
	}
	inFile    >> web.websiteUrl = "zero";
              >> web.revenue = 0;
              >> web.hits = 0;
              return web;
} 
Last edited on
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?
Last edited on
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.
Last edited on
How are these entrees (in the .dat file) seperated (delimited?) Are they each on their own line, one space between each, ...etc?
The dat file is setup like

URL space revenue space hits

All on one line

Separated by one space each.
1
2
3
4
5
6
7
8
9
10
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. 
Last edited on
Here is the updated code
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
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;


struct Record
{
       string websiteUrl;
       double revenue;
       int hits;
};

Record GetData(ifstream& inFile);
void DisplayTotals( Record web );


int main()
{
    Record web;
    int totalHits = 0;
    double totalRev = 0;
    
    
    ifstream inFile("websiteHits.dat");
    inFile.open("websiteHits.dat");
    while(!inFile.eof())
    {
         Record web = GetData(inFile);
         //totalHits = GetData(inFile);
        //totalRev = GetData(inFile);
        totalHits += web.hits;
        totalRev += web.revenue;
        
        cout << "This is the Total Hits: " << totalHits << endl;
        cout << "This is the Total Rev: " << totalRev;
        
    }
    
    
    system("pause");
    return 0;
}

Record GetData(ifstream& inFile)
{ 	
	Record web;
	
	inFile >> web.websiteUrl
		   >> web.revenue
		   >> web.hits; 
	return web;
} 




The file now compiles!!! A big step =). But the file increments and never stops, getting the "matrix look" with numbers just going and going?
Last edited on
Post the file you're reading information from.
amazon.com 820000 350 ccbcmd.edu 22040 40020 pcmagazine.com 760900 500 umbc.edu 89075 36000 mdspc.org 4500 230
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.)
Last edited on
Well now it just compiles and than says "press any key to continue". After I read the file, the totals have to be displayed on the screen.

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).
1
2
3
//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;
Last edited on
Ok I see, It's very simple lines. So, how do I trace out the dtotals to the screen? Each listed on a new line?
I don't understand the question. What are the dtotals?
Ok I was able to code a good bit on my own. But my totals are not calculating correctly.
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cmath>
#include <iomanip>
#include <cmath>
using namespace std;


struct Record
{
       string websiteUrl;
       double revenue;
       int hits;
};

void DisplayTitle();
Record GetData(ifstream& inFile);
//void DisplayTotals( Record web );
void DisplayTotals( double totalRev_Calculation, int totalHits_Calculation, int totalRecords);


int main()
{
    Record web;
    
    string websiteUrl;
    double totalRev = 0;
    int totalHits = 0;
    
    double totalRev_Calculation;
    int totalHits_Calculation;
    int totalRecords = 0;
    
    
    DisplayTitle();
    
    ifstream inFile("websiteHits.dat");
    while(inFile.good())
    {
        
        Record web = GetData(inFile);
        websiteUrl = web.websiteUrl;
        totalRev = web.revenue;
        totalHits = web.hits;
        
        totalRecords ++;
        
        totalRev_Calculation = totalRev * totalRecords; //This is where I calculate the total Rev or    all 5 records

        totalHits_Calculation = totalHits * totalRecords; //This is where i calculate the Total hits of all 5 records
        
        
        cout << "Website URL: " << websiteUrl << endl ;
        cout << "Website Revenue: " << totalRev << endl ;
        cout << "Website Hits: " << totalHits << endl << endl ;
    }
   
    DisplayTotals( totalRev_Calculation, totalHits_Calculation, totalRecords); 
    inFile.close();
    
    system("pause");
    return 0;
}

void DisplayTitle()
{
     using namespace std;
     cout << right << setw(24) << "WEB TRACKER SERVICE"   << endl;
     cout << right << setw(26) << "START TRACKING TODAY!"   << endl;
     cout << right << setw(30) << "-----------------------"  << endl << endl;
     return;     
}

Record GetData(ifstream& inFile)
{ 	
	Record web;
	
	inFile >> web.websiteUrl
		   >> web.revenue
		   >> web.hits; 
		   
	return web;
}

void DisplayTotals( double totalRev_Calculation, int totalHits_Calculation, int totalRecords)
{
     cout << "This is the Total Revenue: " << totalRev_Calculation << endl  ;
     cout << "This is the Total Hits: " <<  totalHits_Calculation << endl;
     cout << "Total Records   = "<< totalRecords << endl << endl;
}

Can some help me with this calculation please?
closed account (D80DSL3A)
Try this to replace your lines 40-60. Hopefully I'm right about WHAT you are trying to calculate:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while(inFile.good())
    {
        
        Record web = GetData(inFile);
        totalRev += web.revenue;
        totalHits += web.hits;        
        totalRecords ++;       
        
        cout << "Website URL: " << web.websiteUrl << endl ;
        cout << "Website Revenue: " << web.revenue << endl ;
        cout << "Website Hits: " << web.hits<< endl << endl ;
    }
   
    DisplayTotals( totalRev, totalHits, totalRecords );
That works great for hits but revenue prints out to
1.69652e+006


Is there something else im missing?
closed account (D80DSL3A)
What's wrong with that number? That's what the individual revenue amounts add up to.
Oh, I thought it was supposed to print a whole number?
Pages: 12