Thread: Reading data from files

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Reading data from files

    I am having trouble getting the code below to work. My first problem is that the first checkSpecials function returns all but the first line.

    The second checkSpecials function returns 0 for all values. I dont know if I am using the correct way to assign "names" to the vaules.

    the input file looks something like this:

    5 2 2 670.60

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    
    void checkSpecials(ifstream& infile)
    {
    	cout << endl << "Display orders stored in file orders.dat: " << endl;
    	while (infile)
    	{
    		string temp;
    		getline(infile, temp);
    		cout << temp << endl;
    	}
    }
     
    void checkSpecials(ifstream& infile, ofstream& outfile)
    {
    	int numSpecials = 0;
    	float commision = 0;
    	int noServed =0;
    	float avgSpent = 0;
    	int count = 0;
    	int value, members, specials, wine, bill, total;
    
    	infile >> members >> specials >> wine >> bill;
    
    	while(infile >> value)
    	{
    		noServed ++;
    		total += total;
    		avgSpent = ((total / noServed) + (total % noServed));
    
    		if((members >= 5) && (specials >= 4) && (wine >= 2))
    		{
    		        
                                            numSpecials ++;
    		}
    		
    	}
    
    	outfile << "Number of families that ordered the special: " << numSpecials << endl
    	           << "Commision earned from the special meal: " << commision << endl
    	           << "Average spent per person for the evening: " << avgSpent << endl;
    	}
    
    
    int main()
    {
    	ifstream infile;
    	ofstream outfile;
    	string outName = "c:\\datafiles\\result.dat";
    	string inName = "c:\\datafiles\\orders.dat";
    
    	int members, special, wine;
    	float total;
    
    	infile.open(inName.c_str());
    	outfile.open(outName.c_str());
    	outfile.precision(2);
    
    	if  (!infile) 
    	{
    		cout << "Cannot open infile " << infile << " Aborting!" << endl;
    		exit(1);
    	}
    
    	if (!outfile) 
    	{
    		cout << "Cannot open oufile " << outfile << " Aborting!." << endl;
    		exit(1);
    	}
    
    		
    	checkSpecials(infile); 
    
    	checkSpecials(infile, outfile);
    
    	infile.close();
    	outfile.close();
    		
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well to read the file for the second time, you need to
    - reset the eof() error state
    - rewind to the beginning of the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    void checkSpecials(ifstream& infile)
    {
        cout << endl << "Display orders stored in file orders.dat: " << endl;
        while (infile)
        {
            string temp;
            getline(infile, temp);
            cout << temp << endl;
        }
    }
    This is flawed and suffers from the same issue as using end-of-file testing to control loops. Test the read operation itself:
    Code:
    void checkSpecials(ifstream& infile)
    {
        string temp;
        cout << endl << "Display orders stored in file orders.dat: " << endl;
        while (getline(infile, temp))
        {
            cout << temp << endl;
        }
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    Brilliant thank you. That solves my first problem.

    Would you also be able to help with the second problem? With the input file containing a few lines in the format: 5 2 5 680.00. Am I correct in using
    Code:
     infile >> members >> specials >> wine >> bill;
    to input the values?

    Code:
    void checkSpecials(ifstream& infile, ofstream& outfile)
    {
    	int numSpecials = 0;
    	float commision = 0;
    	int noServed =0;
    	float avgSpent = 0;
    	int count = 0;
    	int value, members, specials, wine, bill, total;
    
    	infile >> members >> specials >> wine >> bill;
    
    	while(infile >> value)
    	{
    		noServed ++;
    		total += total;
    		avgSpent = ((total / noServed) + (total % noServed));
    
    		if((members >= 5) && (specials >= 4) && (wine >= 2))
    		{
    		        
                                            numSpecials ++;
    		}
    		
    	}
    
    	outfile << "Number of families that ordered the special: " << numSpecials << endl
    	           << "Commision earned from the special meal: " << commision << endl
    	           << "Average spent per person for the evening: " << avgSpent << endl;
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data file efficiently
    By Korhedron in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2010, 03:09 PM
  2. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  3. Reading multiple data from text files
    By Crashie in forum C++ Programming
    Replies: 2
    Last Post: 12-26-2009, 11:37 AM
  4. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  5. Reading data from consecutively named files
    By a1pro in forum C Programming
    Replies: 10
    Last Post: 04-15-2005, 01:48 AM