Filling an array with integers from a file

Hey, this is my first time posting on this site. I got assigned something for my lab where we are given a bunch of numbers and were told to load them into an array. My issue is they specifically requested I do so in a for loop and I can't seem to get the numbers to load in at all. Instead, the uninitialized array goes through my code and provides me with garbage numbers. Note that the teacher wanted us only to use the first 30 numbers even though he provided 33 in the file
1
2
3
4
5
6
7
8
9
10
11
void getInputStreamArray(ifstream& inStream, int array[], int array_size)
{   // the input file name is taken here
    inStream.open("athens_low_temps.txt");
    for(int i = 0; i < array_size; i++)
    {
        cout << "\n" << array[i]; // this was here so I can see what numbers 
        inStream >> array[i];     // are loaded in
    }

    inStream.close();
}


Is where I seem to be having issues. My main function has other loops in it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main(){
    int array_size = 30;
    int array[30];
    ifstream inStream;
    ofstream outStream;
    int low = 0;
    int high = 0;
    double average;
    getInputStreamArray(inStream, array, array_size);
    low_temp( array, low, array_size);
    high_temp(array, high, array_size);
    average_temp( array, average, array_size);
    cout << "Monthly Low:" << low;
    cout << "\nMonthly High: " << high;
    cout << "\nMonthly Average: " << average;

    return EXIT_SUCCESS;
}


The file athens_low_temps.txt goes as following
29
23
15
12
22
-12
34
35
0
22
27
1
22
24
37
16
19
10
16
36
19
19
13
27
27
45
33
31
34
36
33
24
19
Last edited on
Can you use the code tags? Provide us your full file so we can help.
Sure
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>

using namespace std;

void getInputStreamArray(ifstream& inStream, int array[], int array_size);
void low_temp(int array[], int& low, int size);
void high_temp(int array[], int& high, int size);
void average_temp(int array[], double& average, int size);
int main(){
    int array_size = 30;
    int array[30];
    ifstream inStream;
    ofstream outStream;
    int low = 0;
    int high = 0;
    double average;
    getInputStreamArray(inStream, array, array_size);
    low_temp( array, low, array_size);
    high_temp(array, high, array_size);
    average_temp( array, average, array_size);
    cout << "Monthly Low:" << low;
    cout << "\nMonthly High: " << high;
    cout << "\nMonthly Average: " << average;

    return EXIT_SUCCESS;
}

void getInputStreamArray(ifstream& inStream, int array[], int array_size)
{   // the input file name is taken here
    inStream.open("athens_low_temps.txt");
    if(inStream.fail()) // checks if you input an invalid file 
    { 
        cout << "Opening of your file has failed\n";
        cout << "Please make sure the file is in your current directory\n";
        getInputStreamArray(inStream, array, array_size);
    }
   
    for(int i = 0; i < array_size; i++)// loads the array
    {
        cout << "\n" << array[i];
        inStream >> array[i];
        
    }

    inStream.close();

}
void low_temp(int array[], int& low, int size){
    low = 100000; // low temp initilized high to make sure it is replaced instantly
    int temp;   
    for(int i = 0; i < (size + 1); ++i)
    {
        temp = array[i];  // loads the temp of the day into the array
        if(temp < low)  // checks if the temp is lower
        {
            low = temp;    
        }
        
    }
    
}
void high_temp(int array[], int& high, int size){
    high = -100000; //high temp initilized low to make sure it is replaced instantly
    int temp;
    for(int i = 0; i < (size + 1); ++i)
    {
        temp = array[i]; // loads the temp of the day into the array
        if(temp > high)  // checks if the temp is higher
        {
            high = temp;    
        }
    }
    
}
void average_temp(int array[], double& average, int size){
    //average, total of tempts to be average and temp of each day as set by the array
    average = 0; 
    int total = 0;
    int temp = 0;
    for(int i = 0; i < (size + 1); ++i)  
    {
        temp = array[i];
        total += temp;
    }

    average = (total/size);
}
Last edited on
Glancing at it, it looks like you're reading them in, but the order of your statements is wrong which is producing the garbage values.

Try swapping your cout and inStream >> statements.
Topic archived. No new replies allowed.