I'm stuck trying to figure this out. I know I'm going to need a counter I just don't know where or how to implement it. I'm reading from a .dat file. The contents of the file look like this.

google.com 760000 250 yahoo.com 340000 170 daniweb.com 230000 125 etc.

I need to calculate the total revenue, the total hits, the avg revenue and, the number of records in the file. This is what I have so far.

//Description:    Website Report
//Compiler used:	Visual Studio C++ Express Edition

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


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

struct totals
{
	double totRev;
	double totHits;
	double avgRev;
	int numRecords;
};

record ReadRec(ifstream&);
void DisplayRec(record);
void DisplayTitle();
void DisplayTotals(totals);

int main()
{ 

   record temp;
   totals temps;
   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();
   while(! inFile.eof())                 
   {
        temp = ReadRec(inFile);
        DisplayRec(temp);	
   }
	
   DisplayTotals(temps);

   inFile.close();

   return 0;
 }

record ReadRec(ifstream& inFile)
	{
	     record temp;
              inFile >> temp.websiteUrl >> temp.revenue >> temp.hits;
	     return temp;
	}

void DisplayRec(record temp)

         {
	     cout << " Url: " << temp.websiteUrl << endl;
              cout << " Revenue: " << temp.revenue << endl;
              cout << " Hits:" << temp.hits << endl << endl; 
	}

void DisplayTitle()
        
         {
              cout << "\n          Website Report" << endl;
	     cout << "          --------------" << endl;
              system("pause");
         }

void DisplayTotals(totals temps)

         { 
	cout << "Total Revenue: " << temps.totRev << endl;
         cout << "Total Hits: " << temps.totHits << endl;
         cout << "Average Revenue:" << temps.avgRev << endl; 
         cout << "Number of Records:" << temps.numRecords << endl << endl;
	}

Add some accumulators between Lines 52 and 53.

i.e.

accumulatedTotal += currentRecord.valueToAccumulate;
/* ...etc...*/
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.