I have to count the number of records being read in and print them to the screen. A record consists of: website name (temp.url), website revenue (temp.rev) and website hits (temp.hits). These are all read in and returned as temp. For the life of me though, I can't figure out for the life of me the logic behind how I'm suppose to count them. Below is my code...

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std; 

struct webSite
{
        int hits;
        string url;
        double revenue;
};

struct webTot
{
        int totRec, totHits;
        double totRev, avgRev;
};

webSite ReadRec(ifstream&);

void DisplayTitle ();

void DisplayRec(webSite);

void DisplayTotal(webTot);

int main()
{ 
		ifstream fin;

    cout << "Reading file input.\n\n";

   ifstream inFile; 
   inFile.open("c:\\websiteHits.dat");
    if (inFile.fail( ))
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);

	    	DisplayTitle ();
	
			webSite stats;
			webTot sums;
			sums.totRev = 0;
			sums.totHits = 0;
			sums.totRec = 0;
    		
    while(! inFile.eof())                 
    {
        stats = ReadRec(inFile);
        DisplayRec(stats);
		sums.totRev = sums.totRev + stats.revenue;
		sums.totHits = sums.totHits + stats.hits;
		sums.avgRev = sums.totRev / sums.totHits;
//		sums.totRec = sums.totRev++;
	}
	
	DisplayTotal (sums);
    inFile.close();

	system("pause");
    return 0;
 }

	webSite ReadRec(ifstream& inFile)
	{
		webSite temp;
		inFile >> temp.url >> temp.revenue >> temp.hits;
		return temp;
	}
    
void DisplayTitle ()
{
	cout<<"Website Report" << endl << endl;
}

void DisplayRec(webSite temp)
    {
        cout << temp.url << setw(10) << temp.revenue << setw(10) << temp.hits << endl << endl;
    }

void DisplayTotal(webTot temp)
    {
		cout << temp.totRev <<endl;
		cout << temp.totHits <<endl;
		cout << temp.avgRev << endl;
//		cout << temp.totRec << endl;
    }

The while statement on line 54 is not going to work correctly because it will cause the lzst record in the file to be read twice. One way to fix that is like this:

int numRecords = 0;
while( ReadRec(inFile, site) )
{
   ++numRecords;
   // blabla
}


ifstream& ReadRec(ifstream& inFile, webRec& temp)
{
   inFile >> temp.url >> temp.revenue >> temp.hits;
   return inFile;
}

omg thanks man...that's what happens when you over think things. dee dee dee...I'm going to go drink some beer now...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.