reading in ints from user specified file into array

how do i declare the array when i don't know how big it is?

Page 1 of 1

4 Replies - 1912 Views - Last Post: 20 August 2009 - 05:16 PM Rate Topic: -----

#1 AJOHNZ  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 82
  • Joined: 10-May 09

reading in ints from user specified file into array

Post icon  Posted 20 August 2009 - 04:13 PM

here is my attempted code so far but i have no idea to declare/read in for an uspecified sized array

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

int main()
{
	ifstream infile;

	int array[];
	char fileName[21];
	int number;
	int total = 0;
	int nums = 0;

	cout << "Enter a file name: ";
	cin >> fileName;

	infile.open(fileName);






	infile.close();
	
	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: reading in ints from user specified file into array

#2 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 426
  • Joined: 28-September 08

Re: reading in ints from user specified file into array

Posted 20 August 2009 - 04:17 PM

It's relatively simple. Either you can ask the user how many elements gather the number and then create the array. Or you can...
int array[50];


This isn't buffer worm proof!
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: reading in ints from user specified file into array

Posted 20 August 2009 - 04:18 PM

You have to use dynamic memory OR you can limit how much of the file gets loaded at once.

you can use the new[]/delete[] operators to allocate arrays dynamically:

int * myArray = new int[size];
Was This Post Helpful? 0
  • +
  • -

#4 AJOHNZ  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 82
  • Joined: 10-May 09

Re: reading in ints from user specified file into array

Posted 20 August 2009 - 04:53 PM

trixter the first thing you said is not legal. i cant store the input from the user as the size of an array. nickd i have not gotten to dynamic memory yet in the book so i dont think thats it but thank you. here is the problem:

"Write a program that asks the user for a file name. assume the file contains a series of numbers, each written on a separate line. the program should read the contents of the file into an array, and then display the following data...blablabla"

so how could i do this?
Was This Post Helpful? 0
  • +
  • -

#5 AJOHNZ  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 82
  • Joined: 10-May 09

Re: reading in ints from user specified file into array

Posted 20 August 2009 - 05:16 PM

so here is my code knowing the number of numbers in the file...

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

int main()
{
	ifstream infile;

	int array[12];
	char fileName[21];
	int number;
	int total = 0;
	int nums = 0;
	int size;

	cout << "Enter a file name: ";
	cin >> fileName;

	infile.open(fileName);


	for (int i = 0; i < 12; i++)
	{
		infile >> array[i];
		total += array[i];
	}

	int lowest = array[0];
	int highest = array[0];
	for (int i = 0; i < 12; i++)
	{
		if (array[i] > highest)
			highest = array[i];
		if (array[i] < lowest)
			lowest = array[i];
	}

	cout << "Total: " << total << endl;
	cout << "Average: " << total/12 << endl;
	cout << "Highest: " << highest << endl;
	cout << "Lowest: " << lowest << endl;

	infile.close();
	
	return 0;
}




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1