6 Replies - 368 Views - Last Post: 19 November 2012 - 05:58 PM Rate Topic: -----

#1 anwind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-November 12

problem: Cannot read file .txt to array

Posted 19 November 2012 - 01:08 PM

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

void maximum(int Inumber, int& OMax)
{
		if (OMax < Inumber)
			OMax = Inumber ;
}
void minimum(int Inumber, int& OMin)
{	
		if (Inumber < OMin)
			OMin = Inumber;
}
void ave(int Inumber, double& Oaverage)
{
		Oaverage = Oaverage + Inumber;

}
int main()
{
	ifstream inputFile;
	inputFile.open("HiFile.txt", ios_base::in);

	if (inputFile.is_open())
	{
		double average = 0;
		int number;
		int count=1;
		inputFile >> number;
		int Max = number, Min = number;
		while (inputFile >> number)
		{
			count++;
		}
		int Iarray[10]={0};
		for (int arrayIndex = 0; arrayIndex < count; arrayIndex++)    
		{
			inputFile >> Iarray[arrayIndex];                         //I think the problem is here
		}
		for (int arrayIndex = 0; arrayIndex < count; arrayIndex++)
		{		
			maximum(Iarray[arrayIndex], Max);
			minimum(Iarray[arrayIndex], Min);
			ave(Iarray[arrayIndex], average);
		}
		average = average / count;
		cout << "Finished processing the file" << endl;
		cout << "Minimum value: "<< Min << endl;
		cout << "Maximum value: "<< Max << endl;
		cout << setprecision(2);
		cout << "Average value: "<< average << endl;
		for (int arrayIndex = 0; arrayIndex < count; arrayIndex++)
			cout << Iarray[arrayIndex]<< endl;
	}
	else
	cout << "Failed to open the file." << endl;
// close both streams

inputFile.close();
	return 0;
}




I cannot read the file to array. The program is to read a file with at most 10 integer, than put it in an array and find max, min, average of it. I struggle at how to put the data from the file to an array.

Is This A Good Question/Topic? 0
  • +

Replies To: problem: Cannot read file .txt to array

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,868
  • Joined: 25-December 09

Re: problem: Cannot read file .txt to array

Posted 19 November 2012 - 01:24 PM

Your problem is being caused by the following snippet:
		while (inputFile >> number)
		{
			count++;
		}


What is happening is that you are reading your file until you get an error in the read operation. You will need to clear() your error and rewind() your stream back to the beginning before you can read more items.

Why are you counting the number of items in the file? In your problem description you said:

Quote

The program is to read a file with at most 10 integer,

So instead of counting the number of entries and then re-reading your file why don't you read your file and keep track of the number of items read at the same time (one loop)? Just be sure you stop your reading when you have reached your maximum array size.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 anwind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-November 12

Re: problem: Cannot read file .txt to array

Posted 19 November 2012 - 03:02 PM

So what loop should I use? for or while? I think I should you while but I don't know how to put it in array
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,868
  • Joined: 25-December 09

Re: problem: Cannot read file .txt to array

Posted 19 November 2012 - 03:49 PM

What's wrong with your while loop where you're counting? Use your variable you are counting with as the array index. Just make sure you break out of your loop when you reach the limit of your array.

Jim

This post has been edited by jimblumberg: 19 November 2012 - 04:18 PM

Was This Post Helpful? 0
  • +
  • -

#5 anwind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-November 12

Re: problem: Cannot read file .txt to array

Posted 19 November 2012 - 05:13 PM

I still don't have any ideal. I try to use this
		for (int arrayIndex=0; arrayIndex <= 10; arrayIndex)
		{			
			inputFile >> Iarray[arrayIndex];
			maximum(Iarray[arrayIndex], Max);
			minimum(Iarray[arrayIndex], Min);
			ave(Iarray[arrayIndex], average);
			count++;
		}



the program runs but won't show me anything (just a black, blank screen)
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,868
  • Joined: 25-December 09

Re: problem: Cannot read file .txt to array

Posted 19 November 2012 - 05:23 PM

Show your complete code.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 anwind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 19-November 12

Re: problem: Cannot read file .txt to array

Posted 19 November 2012 - 05:58 PM

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

void maximum(int Inumber, int& OMax)
{
		if (OMax < Inumber)
			OMax = Inumber ;
}
void minimum(int Inumber, int& OMin)
{	
		if (Inumber < OMin)
			OMin = Inumber;
}
void ave(int Inumber, double& Oaverage)
{
		Oaverage = Oaverage + Inumber;
}
int main()
{
	ifstream inputFile;
	inputFile.open("HiFile.txt", ios_base::in);

	if (inputFile.is_open())
	{
		double average = 0;
		int number;
		int count=0;
		inputFile >> number;
		int Max = number, Min = number;
		int Iarray[10]={0};
		int arrayIndex = 0;

		for (int arrayIndex=0; arrayIndex <= 10; arrayIndex)
		{			
			inputFile >> Iarray[arrayIndex];
			maximum(Iarray[arrayIndex], Max);
			minimum(Iarray[arrayIndex], Min);
			ave(Iarray[arrayIndex], average);
			count++;
		}
	inputFile.close();
		average = average / count;
		cout << "Finished processing the file" << endl;
		cout << "Minimum value: "<< Min << endl;
		cout << "Maximum value: "<< Max << endl;
		cout << setprecision(2);
		cout << "Average value: "<< average << endl;
		int Index = 0;
		for (int Index=0; Index <= count; ++Index)
		{
			cout <<setw(count)<< Iarray[Index];
		}
	}
	else
	cout << "Failed to open the file." << endl;


	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1