Thread: It is not reading the data from the file...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    22

    Unhappy It is not reading the data from the file...

    Can anyone tell me what is wrong with my program? It is not reading the data from the file...

    Code:
    //This program is....
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    	const int numrow = 20;
    	const int numcol = 2;
    
    void getts(int[][numcol], int);
    void printts(int[][numcol], int);
    
    int main()
    {
    	int testscores[numrow][numcol];
    	
    	getts(testscores, numcol);
    	printts(testscores, numcol);
    
    	return 0;
    }
    
    void getts(int testscores[][numcol], int size)
    {
    	ifstream inFile;
    
    	inFile.open("practicetestscore.dat");
    
    	for (int row=0; row < size; row++)
    	{
    		for (int col = 0; col < size; col++)
    		inFile >> testscores[numrow][numcol]>>testscores[numrow][numcol];
    	}
    
    	inFile.close();
    }
    
    void printts(int testscores[][numcol], int size)
    {
    	cout <<"The testscores are:\n";
    		for (int row = 0; row < size; row++)
    		{
    			for (int col = 0; col < size; col++)
    			cout << setw(2) <<testscores[numrow][numcol] 
    			<< setw(2) <<testscores[numrow][numcol] <<endl;
    		}
    		
    		return;
    }

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    inFile >> testscores[numrow][numcol]>>testscores[numrow][numcol];
    1) This always reads to the same spot in the array - numrow & numcol are const.
    2) This spot is invalid.

    You have the same issue with output.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    22

    Just changed it

    I understand, you have a point, thanks for your help...I just changed it to the following code but still not seeing the data in the file

    Code:
    void getts(int testscores[][numcol], int size)
    {
    	ifstream inFile;
    
    	inFile.open("practicetestscore.dat");
    
    	for (int row=0; row < size; row++)
    	{
    		for (int col = 0; col < size; col++)
    		inFile >> testscores[numrow][0]>>testscores[numrow][1];
    	}
    
    	inFile.close();
    }

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    I was able to get the 20 values of the first column to show now but just need the to get the values in the second column to be displayed, then I could continue adding to it.

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    inFile >> testscores[numrow][0]>>testscores[numrow][1];
    This is still invalid. In that first dimension of the array, you have numrow slots, or 0 to numrow-1. Since numrow is constant, you're loading all your data into the same two rows in the array. You likely want just "row" there. However, your for() loop for row is incorrect - it's looping by how many columns there are, not how many rows.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    22

    Sorting function trouble

    the sorting function I added was:

    Code:
    void selectsort(int testscores[][numcol], int size)
    {
    	int startscan, lowindex;
    		int lowts;
    		
    		for (startscan = 0; startscan < (size-1); startscan++)
    		{
    			lowindex = startscan;
    			lowts = testscores[startscan][0];
    
    			for (int index = startscan + 1; index < size; index++)
    			{
    				if (testscores[index][0] < lowts)
    				{
    					lowts=testscores[index][0];
    					lowindex=index;
    				}
    			}
    			testscores[lowindex][0]=testscores[startscan][0];
    			testscores[startscan][0]=lowts;
    
    			lowts = testscores[startscan][1];
    			testscores[startscan][1] = testscores[lowindex][1];
    			testscores[lowindex][1]=lowts;
    		}
    	printts(testscores, size);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. reading data from a file
    By brianptodd in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2003, 06:50 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM