Reading data from file into array

Discussion in 'Software' started by someguy85, Sep 3, 2009.

  1. someguy85

    someguy85 Private E-2

    I have an assignment do tomorrow, but I'm having trouble getting data read into my array and my patience is short today considering I worked 14 hours. I've been going insane trying to figure out what I'm doing wrong and have come to the conclusion that I need a second pair of eyes (perhaps superior to my own). Anyway, any help is much appreciated and the code is below.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    const int numStudents = 3;
    const int numTests = 4;
    void getTestsFromFile();
    void totalTests(double[][numTests], int);
    void averageAllTests(double[][numTests], int);
    void averageEachStudent(double[][numTests], int); // do I need int variable with 2d array?
    void getHighest(double[][numTests], int);
    void getLowest(double[][numTests], int);
    void report(double[][numTests], int);
    
    void main()
    {
    	ifstream datafile;
    	double scoresArray[numStudents][numTests];
    	
    
    	datafile.open("G:\\visual\\homework\\homework\\test.txt");
    	if(!datafile)
    		cout << "Error opening data file.\n";
    	else
    		getTestsFromFile();
    		report(scoresArray, 4);
    
    
    }
    
    void getTestsFromFile()
    {
    	for (int stud = 0; stud < numStudents; stud++)
    		{
    			for (int test = 0; test < numTests; test++)
    			{
    				datafile >> scoresArray[stud][test];
    			}
    		}
    }
    
    
    
    void report()
    {
    	for (int index = 0; index < numStudents; index++)
    	{
    		cout << "Student " << index + 1 << ":\n";
    		for (int count = 0; count < numTests; count++)
    		{
    			cout << "\tTest " << count + 1 << ": ";
    		}
    	}
    	
    
    
    }
    

    Here's the output from the build:

    error C2065: 'datafile' : undeclared identifier
    error C2065: 'scoresArray' : undeclared identifier

    My programming is a little rusty right now and I'm trying to get back on the horse. Thanks again for any help I receive.
     
  2. someguy85

    someguy85 Private E-2

    Disregard previous post

    DISREGARD PREVIOUS POST


    Figured out partly, but still need help.

    I have an assignment due tomorrow but I'm having trouble getting data read into my array. Everything is read as zero. The code is below and I've attached the text file the data is read from. Thanks for any help.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    const int numStudents = 3;
    const int numTests = 4;
    void getTestsFromFile();
    void totalTests(double[][numTests], int);
    void averageAllTests(double[][numTests], int);
    void averageEachStudent(double[][numTests], int); // do I need int variable with 2d array?
    void getHighest(double[][numTests], int);
    void getLowest(double[][numTests], int);
    void report(double[][numTests]);
    double scoresArray[numStudents][numTests];
    void main()
    {
    	ifstream datafile;
    	
    	
    
    	datafile.open("G:\\visual\\homework\\homework\\test.txt");
    	if(!datafile)
    		cout << "Error opening data file.\n";
    	else
    		getTestsFromFile();
    		report(scoresArray);
    
    
    }
    
    void getTestsFromFile()
    {
    	ifstream datafile;
    	for (int stud = 0; stud < numStudents; stud++)
    		{
    			for (int test = 0; test < numTests; test++)
    			{
    				datafile >> scoresArray[stud][test];
    			}
    		}
    }
    
    
    
    void report(double scoresArray[][numTests])
    {
    	for (int index = 0; index < numStudents; index++)
    	{
    		cout << "\n\nStudent " << index + 1 << ":\n";
    		for (int count = 0; count < numTests; count++)
    		{
    			cout << "\tTest " << count + 1 << ": " << scoresArray[index][count] << endl;
    		}
    	}
    	
    
    
    }
    



    My programming is a little rusty right now and I'm trying to get back on the horse. Thanks again for any help I receive.
     

    Attached Files:

    Last edited: Sep 3, 2009
  3. Wyatt_Earp

    Wyatt_Earp MajorGeek

    Re: Disregard previous post

    In GetTestsFromFile() datafile doesn't have any initialization. That datafile and the datafile in Main() are different, so you need to either open the datafile inside of GetTestsFromFile() or you need to pass the datafile from main into GetTestsFromFile().

    For example, if you change the signature of GetTestsFromFile() to:

    void GetTestsFromFile(&ifstream datafile);

    My C++ is a little rusty. I can't remember if the '&' goes infront of ifstream or datafile, but you get the idea. Hope that helps.
     
  4. Wyatt_Earp

    Wyatt_Earp MajorGeek

    Re: Disregard previous post

    Also, you're going to have a similar scope issue on the report() function. You have scoresArray declared as a global variable, yet you are passing it in as a function parameter. And they are named the same, so I'm surprised you aren't getting a compiler error about a scope conflict, or something.
     
  5. someguy85

    someguy85 Private E-2

    Thanks for your help. I fixed it first thing this morning. Everything's working except the getHighest and getLowest function. I don't know how to do this with a 2D array. I would appreciate if someone could show me a good way. The new code I wrote is below and I'll attach the new text file that I was apparently supposed to use. Thanks again for any help.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    const int numStudents = 3;
    const int numTests = 4;
    //Function prototypes
    void getTestsFromFile(double[][numTests], int numRows);
    double totalAllTests(double[][numTests], int);
    double averageAllTests(double[][numTests], int, double);
    void averageEachTest(double[][numTests], int);
    void getHighest(double[][numTests], int);
    void getLowest(double[][numTests], int);
    void report(double[][numTests], int numRows);
    
    
    void main()
    {
    	double scoresArray[numStudents][numTests];
    	
    	getTestsFromFile(scoresArray, numStudents);
    	report(scoresArray, numStudents);
    			
    
    
    }
    
    
    void getTestsFromFile(double myArray[][numTests], int numRows)
    {
    	ifstream datafile;
    	datafile.open("test.txt");
    	if(!datafile)
    		cout << "Error opening data file.\n";
    	else
    	{
    	for (int stud = 0; stud < numRows; stud++)
    		{
    			for (int test = 0; test < numTests; test++)
    			{
    				datafile >> myArray[stud][test];
    			}
    		}
    	}
    	
    }
    
    
    double totalAllTests(double myArray[][numTests], int numRows)
    {
    	double totAllTests = 0.0;
    	for(int count = 0; count < numRows; count++)
    	{
    		for(int index = 0; index < numTests; index++)
    		{
    			totAllTests += myArray[count][index];
    		}
    	}
    
    	return totAllTests;
    }
    
    double averageAllTests(double myArray[][numTests], int numRows, double totalAll)
    {
    	double allAverage = (totalAll / (numRows * numTests));
    	return allAverage;
    } 
    
    
    void averageEachTest(double myArray[][numTests], int numRows)
    {
    	for(int index = 0; index < numTests; index++)
    	{
    		double tot = 0.0;
    		for(int count = 0; count < numRows; count++)
    		{
    			tot += myArray[count][index];
    		}
    		double avg = tot / numRows;
    		cout << fixed << showpoint << setprecision(2);
    		cout << "Average for Test # " << index+1 << ": " << avg << endl;
    	}
    	cout << "Press Enter to Continue. ";
    	cin.get();
    	cout << "\n\n";
    }
    
    void getHighest(double myArray[][numTests], int numRows)
    {
    	double highest;
    	double newHighest;
    	
    	for(int count = 0; count < numRows; count++)
    	{
    		int index = 0;
    		highest = myArray[count][index];
    	
    		for(int index = 0; index < numTests; index++)
    		{
    			if(myArray[count][index+1] > myArray[count][index])
    			{
    				newHighest = myArray[count][index+1];
    			}
    		}
    		cout << fixed << showpoint << setprecision(2);
    		cout << "Highest test of Student # " << count+1 << ": " << newHighest << endl;
    	}
    	cout << "Press Enter to Continue. ";
    	cin.get();
    	cout << "\n\n";
    }
    
    void getLowest(double myArray[][numTests], int numRows)
    {
    	double lowest;
    	double newLowest;
    	
    	for(int count = 0; count < numRows; count++)
    	{
    		int index = 0;
    		lowest = myArray[count][index];
    		
    		for(int index = 0; index < numTests; index++)
    		{
    			if(myArray[count][index+1] < myArray[count][index])
    			{
    				newLowest = myArray[count][index+1];
    			}
    		}
    		cout << fixed << showpoint << setprecision(2);
    		cout << "Lowest test of Student # " << count+1 << ": " << newLowest << endl;
    	}
    	
    }
    
    				
    
    
    
    void report(double sArray[][numTests], int numRows)
    {
    
    	double totAll = totalAllTests(sArray, numRows);
    	cout << fixed << showpoint << setprecision(2);
    	cout << "\nThe total score of all tests is: " << totAll << endl;
    	cout << "Press Enter to Continue.";
    	cin.get();
    
    	double averageofAll = averageAllTests(sArray, numRows, totAll);
    	cout << "\n\nThe average of all tests is: " << averageofAll << endl;
    	cout << "Press Enter to Continue.";
    	cin.get();
    	cout << "\n\n";
    
    	
    	averageEachTest(sArray, numRows);
    	
    
    	getHighest(sArray, numRows);
    
    	getLowest(sArray, numRows);
    
    
    }
    
     

    Attached Files:

    Last edited: Sep 4, 2009
  6. Wyatt_Earp

    Wyatt_Earp MajorGeek

    Code:
    if(myArray[count][index+1] > myArray[count][index])
    {
    	newHighest = myArray[count][index+1];
    }
    I think your problem is here. Take a look at what you are comparing. Instead, try comparing the current value with the previous high value. You want something like:

    Code:
    if(myArray[count][index] > highest)
    {
              // Insert code to save the highest value here
    }
    
    P.S. It's the same issue with the GetLowest function.
     
  7. someguy85

    someguy85 Private E-2

    Oh. Yeah, that makes sense. I don't know why I did that. I've done highest and lowest functions before with one dimensional arrays, I don't know why I made it so difficult on myself just because it's 2D. Now not to waste a cycle, when defining the for loop I should declare index as 1, right? Thanks so much for your help. Some times you can rack your brain all day and you just won't see the simplest of things like that. It's working just fine now.
     
  8. Wyatt_Earp

    Wyatt_Earp MajorGeek

    Yeah, you can start at one since you are setting the [0] element as the first high (and low) number, thus, you don't need to compare element [0] to element [0].
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds