no output

This is a discussion on no output within the C++ Programming forums, part of the General Programming Boards category; I am having problems getting output on this program. I want to be able to display the contents of two ...

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

    no output

    I am having problems getting output on this program. I want to be able to display the contents of two parrallel arrays read from a file. It is reading my headings alright but can't read from the arrays. Any help would be welcomed.
    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;
    		}

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I would suggest going over the chapter on arrays in your book again. readData isn't filling either array, it's reading two values and placing one in the first element of each array. The problem really isn't in the reading two values part, it's in the assigning to the first element at all times part. Your printResults function seems confused as to what types you're working with.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    Yea that is my problem, the crappy book I have explains nothing about reading from a file, much less creating a function to read an array from file. It has been all guesswork at this point so far.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    Any good websites that might be able to show me how to do this?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    To read:
    Code:
    // Fill the array with values from a stream
    // Return the number of values read
    int readFile(istream& in, int array[], int size)
    {
      int i;
    
      for (i = 0; i < size; i++) {
        if (!(in>> array[i]))
          break;
      }
    
      return i;
    }
    To write:
    Code:
    void writeFile(ostream& out, int array[], int size)
    {
      for (int i = 0; i < size; i++)
        out<< array[i];
    }
    To use them:
    Code:
    #include <cstdlib>
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      ifstream in("somefile");
    
      if (!in) {
        cerr<<"Error opening input file"<<endl;
        exit(EXIT_FAILURE);
      }
    
      int a[50];
      int n;
    
      n = readFile(in, a, 50);
      writeFile(cout, a, n);
    }
    Last edited by Prelude; 12-05-2004 at 06:42 PM.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    Ok I get that, but what im confused on is how to read in 2 columns of data and put each column in a different array.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but what im confused on is how to read in 2 columns of data and put each column in a different array.
    That's actually the easy part. Assuming equal length columns:
    Code:
    int readFile(istream& in, int a[], int b[], int size)
    {
      int i;
    
      for (i = 0; i < size; i++) {
        if (!(in>> a[i] >> b[i]))
          break;
      }
    
      return i;
    }
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    awesome it should be smooth sailing from here. Thank you so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 03:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 06:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21