C++ guys. Need some help.

I need my program to find the exact amount of data to be stored into an array without asking the user. I am having trouble with this because my program will not run unless it knows how much data is going into the array. The program opens up a simple .txt file with numbers in it (ex. 9.8 4.5 2.4 8.7…). The amount of numbers will be random and I cannot waste any memory space (therefore I cannot declare the array size to be [10000]). So how to I get my program to open the notepad file, then count the amount of numbers, and then put those numbers into an array to be worked with later on in the program?

Here is my code with all the other, non-array, information taken out to make it much smaller to look at and work with. My problem with this program is hard to explain so I hope I have explained it well enough.

edit: I need to use an array! No vectors or alternative methods please.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

double scores[ARRAYSIZE];

int main()
{

	ifstream infile;

infile.open(“scores.txt”);

int i = 0;

while (infile)
{
infile >> scores[i];
i++;
}
infile.close();

return 0;

}

sounds like homework. And sounds like you haven’t given it a shot yet based on what you posted. Give it a shot and if you get stuff, ask about that

Notice my time of posting. 5am. I gave it a shot.

In the mid 90s, I did this for a living using C++ on Macs, but it’s been a long time since I’ve coded anything. Is a dynamic array considered an alternative? If it’s allowable, you should be able to find code examples on-line or in your books. You can probably go through the text file and count the numbers and increase the array size as needed. As for the actual code - been to long for me to help there.

One easier but more daunting way you could do it is loop through the file 2 times the first using a constant variable to count the number of numbers in the file then use the constant as the array size but that is assuming its a file of only numbers

did you learn pointers yet and creating memory on the heap?
If so then you should know that a pointer will solve this problem.

http://www.fredosaurus.com/notes-cpp/newdelete/50dynamalloc.html

Dam I miss programming, but not homework.

Didn’t you post your last problem for CS class? You really need to consider another major if you can’t figure this out.

^ not my major and I posted one problem last semester I think.

Also I guess Im allowed to waste space. So an array[100] will work just fine. Thanks everyone except Boxxa.

Welcome :wiggle:

That’s a great way to write an exploitable program

This is just a simple C++ project.

Here it is completed:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cassert>

using namespace std;

void askuser(char filename[]);
void getdata(double scores[], char filename[], int & size);
void calculatehigh(double scores[], int size, double & high);
void calculatelow(double scores[], int size, double & low);
void calculatesum(double scores[], double low, double high, int size, double & sum);
void display(double scores[],int size, double high, double low, double sum);

int main()
{

char filename[20];
double scores[10], high, low, sum;
int size;
askuser(filename);
getdata(scores, filename, size);
calculatehigh(scores, size, high);
calculatelow(scores, size, low);
calculatesum(scores, low, high, size, sum);
display(scores, size, high, low, sum);

return 0;

}
//This function will ask the user for the input file.
void askuser(char filename[])
{

cout &lt;&lt; "What is the name of the input file? " &lt;&lt; endl;
cin &gt;&gt; filename;

}
//This function will open the input file and put the data into an array.
void getdata(double scores[], char filename[],int & size)
{

ifstream infile;
infile.open(filename); 
assert(infile);  
int i = 0;

while (infile)
{
	infile &gt;&gt; scores[i];
	i++;
}
size = i - 1;
infile.close();

}
//This function will calculate the high score.
void calculatehigh(double scores[], int size, double & high)
{
int i;
high = scores[0];

for (i = 0; i &lt; size; i++)
	if (scores[i] &gt; high)
		high = scores[i];	

}
//This function will calculate the low score.
void calculatelow(double scores[],int size, double & low)
{
int i;
low = scores[0];

for (i = 0; i &lt; size; i++)
	if (scores[i] &lt; low)
		low = scores[i];

}
//This function will calculate the sum of the score minues the high and low.
void calculatesum(double scores[], double low, double high, int size, double & sum)
{
int i;
sum = 0;
for (i = 0; i < size; i++)
sum = sum + scores[i];
sum = sum - high - low;
}
//This function will display all data.
void display(double scores[],int size, double high, double low, double sum)
{
int i;
cout <<"The contestants scores were: " << endl;
for (i = 0; i < size; i++)
cout << scores[i] << endl;
cout << endl <<"The dropped scores were: " << endl << high << " and " << low << endl << endl;
cout <<"Points recieved: " << endl << sum << endl;
}