I'm fairly new to C++. Can someone help me with a problem I'm stuck on? Below is a data file that has 3 fields in each record. I have to read the fields into a structure one record at a time. Any suggestions would be greatly appreciated.

amazon.com 9250000 250 uudcus.edu 11075 62570 itmagazine.com 10609500 9075 usaf.org 84500
90500 myspace.net 7500 560

I have to create a screen report with a title, each record listed on a new line, and the following totals:

Total revenue
Total hits
Average revenue
Number of records in the file (this is a counter).

The program should contain these functions:

DisplayTitle
ReadRec
DisplayRec
DisplayTotals

This is what I have so far:

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

void DisplayTitle();

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

webRec ReadRec(ifstream&);

void DisplayRec(webRec);

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


   


    while(! inFile.eof())                 
    {
        record = ReadRec(inFile);
        DisplayRec(record);
    }

    inFile.close();

	system("pause");
    return 0;
 }

Help is needed.

Thanks

Newbee, I've asked that your thread be moved from Comm Intro's to C++. You will get a much better response from there.

Happy coding.:)

Where are ReadRec and DisplayRec? You've got the right idea, but .eof() isn't a good idea (see http://www.daniweb.com/forums/post155265-18.html). Post your attempt at those methods and we'll help you with them.

Also, you want if(!infile.is_open()) on line 24. Fail() is for checking if the stream has received "bad" input.

How to I move it to C++?

I haven't gotten to the ReadRec and DisplayRec yet. I'm trying though.

How to I move it to C++?

All set, a mod moved it.

I haven't gotten to the ReadRec and DisplayRec yet. I'm trying though.

The issue is more that the design of those functions will dictate how they will be used in main() so it's tough to help. You'll probably want to move your while loop inside of your function rather than the other way around.

I had a similar problem to do and this may help you. I am new to C++ so I know it can be a little overwhelming. I hope this helps.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cctype>

using namespace std;

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

struct Totals
{
    double totRev;
    double avgRev;
    int totHits;
    int numRec;
};


void DisplayTitle();
webHits ReadRec(ifstream&);
void DisplayRec(webHits);
void DisplayTotals(Totals);

int main()
{
    webHits record;                 //record is the name of the structure variable that 
                                    //contains the url, revenue, and hits members.

    Totals temps;                   //temps is the name of the structure variable that
                                    //contains the total members for the Total revenue,
                                    //Total hits, Average revenues & Number of records in file.
    temps.totRev = 0;
    temps.avgRev = 0;
    temps.totHits = 0;
    temps.numRec = 0;



    ifstream inFile;                //inFile is the name of the websiteHits file

    inFile.open("c:\\USERS\\LADYHVB\\Documents\\websiteHits.dat");
    if(inFile.fail())
    {
        cout << "File Opening Failed."<< endl;
        exit(1);
    }

    DisplayTitle();
    while(!inFile.eof())            //Read the fields into a structure one record at a time
    {
        record = ReadRec(inFile);   //ReadRec is the name of the function that reads the fields in the record

        temps.totRev = temps.totRev + record.revenue;       //Total revenue
        temps.totHits = temps.totHits + record.hits;        //Total hits
        temps.numRec++;                                     //Number of records in the file

        DisplayRec(record);
    }
    temps.avgRev = (temps.totRev) / (temps.numRec);     //Average revenue
    DisplayTotals(temps);           
    inFile.close();


    return 0;
}

//Create a screen report with a title, each record listed on 
//a new line, using these functions:
//  DisplayTitle
//  ReadRec
//  DisplayRec
//  DisplayTotals

void DisplayTitle()
{
    cout << "\t################################################"<< endl;
    cout << "\t\t\tWEBSITE STATISTICS"<<endl;
    cout << "\t################################################"<< endl;
    cout << endl;
    cout << "\tURL\t\t     REVENUES\t\t   HITS"<< endl;
    cout << "\t----\t\t   -----------\t\t -------"<<endl <<endl;
}

webHits ReadRec(ifstream& file)
{
    webHits temp;
    file >>temp.url >>temp.revenue >>temp.hits;
    return temp;
} 

void DisplayRec(webHits temp)
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout <<"\t"<<temp.url<<"\t"<<setw(15)<<temp.revenue<<"\t"<<setw(15)<<temp.hits<<endl;
}

void DisplayTotals(Totals temps)
{

    cout << "\n\n\tTotal Revenue:$ " << temps.totRev << endl;
    cout << "\n\tTotal Hits: " << temps.totHits << endl;
    cout << "\n\tAverage Revenue:$ " << temps.avgRev << endl;
    cout << "\n\tNumber of Records:" << temps.numRec << endl << endl;
}
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.