Can someone please tell me why I am getting an error message saying 'temps' is uninitialised???

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cmath>
#include <iomanip>
#include <cmath>
#include <cstdlib>

using namespace std;


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

struct totals
{
	double totalRevenue;
	double totalHits;
	double averageRevenue;
	int totalrecords;

};

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


int main()
{
    record record;
	totals temps;
	ifstream inFile;

	double totalHits=0;
	double totalRev=0;

	

	//Opening the external file
	inFile.open("websiteHits.dat");
	if(inFile.fail())
	{ cout << "File failed to open!!\n"; system ("pause"); exit (1);}

	//Setting specific standards for displaying data
    cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

    DisplayTitle();
    
    while(!inFile.eof())
    {
        record = readRec(inFile);
		DisplayRec(record); 
		totalHits+=record.hits;
		totalRev+=record.revenue;
   }
   
    DisplayTotals(temps);

    inFile.close();//Closes the website hits file
    
    system("pause");
    
	return 0;
}

//Function to display title
void DisplayTitle()
{
     cout << right << setw(24) << "WEBSITE REPORT"   << endl;
     cout << right << setw(30) << "-----------------------"  << endl << endl;
     return;     
}

//Read record from file
record readRec(ifstream& inFile)
{ 	
	record record;
	
	inFile >> record.websiteUrl
		   >> record.revenue
		   >> record.hits; 
	return record;
}

//Display record
void DisplayRec(record record)
{
	cout<<"URL:" <<record.websiteUrl<< endl;
	cout<<"Revenue:" <<record.revenue<<endl;
	cout<<"Hits:" <<record.hits<<endl<<endl;
	system("Pause");
}

//Displays totals
void DisplayTotals(totals temps)
{
	cout << "Total Revenue:" <<temps.totalRevenue << endl  ;
	cout << "Total Hits:" <<temps.totalHits<< endl;
	cout <<"Average Revenue:"<<temps.averageRevenue<< endl;
	cout << "Total Records:"<<temps.totalrecords<<endl<<endl;
	system("Pause");
}

Recommended Answers

All 2 Replies

You never set a value for the "temps" that is in main() before it is passed to DisplayTotals()

Can someone please tell me why I am getting an error message saying 'temps' is uninitialised???

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cmath>
#include <iomanip>
#include <cmath>
#include <cstdlib>

using namespace std;


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

struct totals
{
	double totalRevenue;
	double totalHits;
	double averageRevenue;
	int totalrecords;

};

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


int main()
{
    record record;
	totals temps;
	ifstream inFile;

	double totalHits=0;
	double totalRev=0;

	

	//Opening the external file
	inFile.open("websiteHits.dat");
	if(inFile.fail())
	{ cout << "File failed to open!!\n"; system ("pause"); exit (1);}

	//Setting specific standards for displaying data
    cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

    DisplayTitle();
    
    while(!inFile.eof())
    {
        record = readRec(inFile);
		DisplayRec(record); 
		totalHits+=record.hits;
		totalRev+=record.revenue;
   }
   
    DisplayTotals(temps);

    inFile.close();//Closes the website hits file
    
    system("pause");
    
	return 0;
}

//Function to display title
void DisplayTitle()
{
     cout << right << setw(24) << "WEBSITE REPORT"   << endl;
     cout << right << setw(30) << "-----------------------"  << endl << endl;
     return;     
}

//Read record from file
record readRec(ifstream& inFile)
{ 	
	record record;
	
	inFile >> record.websiteUrl
		   >> record.revenue
		   >> record.hits; 
	return record;
}

//Display record
void DisplayRec(record record)
{
	cout<<"URL:" <<record.websiteUrl<< endl;
	cout<<"Revenue:" <<record.revenue<<endl;
	cout<<"Hits:" <<record.hits<<endl<<endl;
	system("Pause");
}

//Displays totals
void DisplayTotals(totals temps)
{
	cout << "Total Revenue:" <<temps.totalRevenue << endl  ;
	cout << "Total Hits:" <<temps.totalHits<< endl;
	cout <<"Average Revenue:"<<temps.averageRevenue<< endl;
	cout << "Total Records:"<<temps.totalrecords<<endl<<endl;
	system("Pause");
}

Within this code, the "uninitialised temps" error is the least to worry about.

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.