Help with an array size defined by file?

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:
1
2
3
4
5
4
65
56
89
12


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!

1
2
3
4
5
float *Numbers = new float[NumOfInts];

// other code

delete [] Numbers;
Where would i place this code at?

Which lines in my posted code?
When i try that code, i keep getting an error: "conflicting types for Numbers"?
Are you just copying and pasting what was given to you into a random place into your code?
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;
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.
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)
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.
Is something like this correct?

1
2
3
4
float *Numbers;
int NumOfInts;    /* store user input here */

Numbers = (float *) malloc(sizeof(float) * NumOfInts);
That should work. Just remember to free() it when you no longer need it.
Topic archived. No new replies allowed.