Thread: reading from file

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    10

    reading from file

    Having a problem reading from an input file. Have two sets of data- account numbers and balances. I set them up in parallel arrays but the book im using does not explain anything about reading from a file. Just curious how I could read the arrays from a function.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    Something is wrong with my functions but I havent been able to find a good resource to help me out. I am getting errors saying my customer and balance array are unrefereneced. Any help would be appreciated

    Code:
     
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    void headings(ofstream &outFile);
    void readData(ifstream &inFile);
    void sort(ifstream &inFile);
    
    int main()
    {
    	const int size = 50;
    	
    	//declare variables
    	int customer[size];
    	double balance[size];
    	
    	//open files
    	ifstream inFile;
    	ofstream outFile;
    	inFile.open("prog7.txt");
    	outFile.open("prog7.out");
    
    	headings(outFile);
    	
    	// read from file
    	double temp = 0;
    	readData(inFile);
    	while (!inFile.eof())
    {
    	readData(inFile);
    	temp ++;
    }
    	sort(inFile);
    	outFile.close();
    	inFile.close();
    	return 0;
    }
    	void headings(ofstream &outFile)
    	{
    		
    		outFile <<setw(10)<< "Customer ID"
    				<<setw(10)<< "Balance"
    				<<endl;
    	}
    	void readData(ifstream &inFile)
    		{
    			double temp = 0;
    			inFile >> customer[temp] >> balance[temp];
    		}
    	void sort(ifstream &inFile)
    		{
            	for(int j=1; j< size; j++)
    			{
    				for(int i=0; i< size-1; i++)
    				{
    					if(balance[i] > balance[i+1]
    					{
    						temp=balance[i];
    						balance[i]=balance[i+1];
    						balance[i+1]=temp;
    					}
    				}
    			}
    		}
    	void printResults(ofstream &outFile, ifstream &inFile)
    		{
    			outFile << setprecision(2) << fixed << showpoint;
    			outFile <<setw(10)<< customer[size]
    					<<setw(10)<< balance[size]
    					<<endl << endl;
    		}

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You need to pass them to the functions. customer is declared in main so only main can see it.
    IE:
    Code:
    void readData(ifstream &inFile, int customer[], double balance[])
    {
    double temp = 0;
    inFile >> customer[temp] >> balance[temp];
    }
    which then would be called with:
    Code:
    readData(inFile, customer, balance);
    However that function has an error in that temp is a double but you are using it as the index to an array

  5. #5
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    Hi. I get a bunch of compile-time errors with your code. Be careful:
    Code:
        if(balance[i] > balance[i+1]
    You haven't closed the parenthesis of the if conditional.
    Correct:
    Code:
        if(balance[i] > balance[i+1])
        {
            /*...*/
        }
    Good Luck,
    Steve

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    ok thanks for the help so far. I cleaned up the code and managed to get no errors after debugging, but still am not getting any output. Not sure how to add reference variables to arrays while in a function. Any help on how to do this or what my problem might be would be greatly appreciated.

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cmath>
    #define size 50
    
    using namespace std;
    
    void headings(ofstream &outFile);
    void readData(ifstream &inFile, int customer[], float balance[]);
    void sort(ifstream &inFile, float balance[]);
    void printResults(ofstream &outFile, ifstream &inFile, int customer[], float balance[]);
    
    int main()
    {
    	//declare variables
    	int customer[size];
    	float balance[size];
    	
    	//open files
    	ifstream inFile;
    	ofstream outFile;
    	inFile.open("prog7.txt");
    	outFile.open("prog7.out");
    
    	// funtion to print headings
    	headings(outFile);
    	
    	// read from file
    	float temp = 0;
    	readData(inFile, customer, balance);
    	
    	// loop
    	while (!inFile.eof())
    	{
    		readData(inFile, customer, balance);
    		temp ++;								
    	}
    	
    	// function to sort in descending order
    	sort(inFile, balance);
    
    	// function to print results
    	printResults(outFile, inFile, customer,  balance); 
    
    
    	outFile.close();
    	inFile.close();
    	return 0;
    }
    	void headings(ofstream &outFile)
    	{
    		outFile <<setw(10)<< "Customer ID"
    				<<setw(10)<< "Balance"
    				<<endl;
    	}
    	void readData(ifstream &inFile, int customer[], float balance[]) 
    		{
    			static int i = 0;			  
    			static int j = 0;
    			inFile >> customer[j] >> balance[i];
    		}
    	void sort(ifstream &inFile, float balance[])
    		{
    			float temp = 0;			
            	for(int j=1; j< size; j++)
    			{
    				for(int i=0; i< size-1; i++)
    				{
    					if(balance[i] > balance[i+1])
    					{
    						temp=balance[i];
    						balance[i]=balance[i+1];
    						balance[i+1]=temp;
    					}
    				}
    			}
    		}
    	void printResults(ofstream &outFile, ifstream &inFile, int customer[], float balance[])
    		{
    			outFile << setprecision(2) << fixed << showpoint;
    			outFile <<setw(10)<< customer
    					<<setw(10)<< balance
    					<<endl << endl;
    		}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM