Help with an array size defined by file?
GeneralBamboo (11)
Oct 31, 2012 at 12:11am UTC
I am working on a program that will read in a file. The first integer in the file will tell the user how many numbers will be read in next.
The input file might look as follows:
so the first integer is saying there is 4 numbers in this file.
now i need to store the following 4 numbers into an array. My problem is that the size of these numbers in this file might change. For instants it might contain 20 or even 100 numbers.
How can i declare an array that will use this first integer as its size?
Here is what my code looks like so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <stdio.h>
int main()
{
int NumOfInts;
float Numbers[x];
int x;
FILE *InPut;
InPut = fopen("integers.in" , "r" );
fscanf(InPut, "%d" , &NumOfInts);
x = NumOfInts;
for (i=0; i<NumOfInts; i++)
{
fscanf(InPut, "%f" , &Numbers[i]);
}
getch();
return 0;
}
I know what i have wont work at all, im just trying to come up with a simple solution!
vlad from moscow (3108)
Oct 31, 2012 at 12:19am UTC
1 2 3 4 5
float *Numbers = new float [NumOfInts];
// other code
delete [] Numbers;
GeneralBamboo (11)
Oct 31, 2012 at 12:32am UTC
Where would i place this code at?
Which lines in my posted code?
GeneralBamboo (11)
Oct 31, 2012 at 1:55am UTC
When i try that code, i keep getting an error: "conflicting types for Numbers"?
Zhuge (2871)
Oct 31, 2012 at 2:59am UTC
Are you just copying and pasting what was given to you into a random place into your code?
GeneralBamboo (11)
Oct 31, 2012 at 3:30am UTC
This is what I tried:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <stdio.h>
int main()
{
int NumOfInts;
float Numbers[6];
FILE *InPut;
InPut = fopen("integers.in" , "r" );
fscanf(InPut, "%d" , &NumOfInts);
float *Numbers = new float [NumOfInts];
for (i=0; i<NumOfInts; i++)
{
fscanf(InPut, "%f" , &Numbers[i]);
}
getch();
return 0;
}
I was unsure where to put:
delete [] Numbers;
Zhuge (2871)
Oct 31, 2012 at 3:33am UTC
You have declared Numbers twice; once on line 7, and again on line 15. The compiler can't assign the same identifier (name) to two different things like that.
Put the the delete[] statement after you are done with the array, as it will destroy it.
GeneralBamboo (11)
Oct 31, 2012 at 3:44am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <stdio.h>
int main()
{
int NumOfInts;
FILE *InPut;
InPut = fopen("integers.in" , "r" );
fscanf(InPut, "%d" , &NumOfInts);
float *Numbers = new float [NumOfInts];
for (i=0; i<NumOfInts; i++)
{
fscanf(InPut, "%f" , &Numbers[i]);
}
delete [] Numbers;
getch();
return 0;
}
When I run this, I get an error that says:
`new' undeclared (first use in this function)
Zhuge (2871)
Oct 31, 2012 at 3:53am UTC
Oh, I see you are using C. new is a C++ keyword, so it wouldn't be in C. You'll need to use malloc() and free() instead.
GeneralBamboo (11)
Oct 31, 2012 at 4:36am UTC
Is something like this correct?
1 2 3 4
float *Numbers;
int NumOfInts; /* store user input here */
Numbers = (float *) malloc(sizeof (float ) * NumOfInts);
Zhuge (2871)
Oct 31, 2012 at 5:03am UTC
That should work. Just remember to free() it when you no longer need it.
Topic archived. No new replies allowed.