File is not being read and stored

Hello, ive been trying to figure out what im doing wrong here.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<iomanip>
#include<fstream>

using namespace std;

void readRainFall(float rainFall[])
{//Open the file
ifstream inputFile;
//Open the file
inputFile.open("rainfall.txt");
//Initialize month counter
int month =0; //first month
//Read the monthly rainfall in the file
//inputFile >> rainFall[month];
cout << rainFall[month];
while(month < 12)
 {
  inputFile >> rainFall[month];
  month++;
 }

}//end function readRainFall


inputFile >>rainFall[month] breaks in the while loop, i get some weird random numbers, like 1.4013e-45. If i put this statement outside of the while loop, it works perfectly fine. What am i doing wrong here?

Also, this is just part of the code, but im stuck on this one specifically... :\
Are you sure that the file actually opens properly? Perhaps it would be a good idea to check to insure the file opened correctly?

Also the cout on line 16 will probably print garbage because you haven't yet tried to read the file.

i was using line 16 to see if it reads the file outside of the while loop... to which it does. I called a friend for help to which we didnt change anything and it started to run properly again. Im starting to think if it is the compiler that isnt running properly or what not... cuz i hit another instance where the loop will not go but it should be right... :\
i was using line 16 to see if it reads the file outside of the while loop... to which it does.

You're printing an uninitialized variable at that point, so whatever it prints is undefined behavior. In the code you provided you're not reading the file until after that line so there is no way, with the code provided, you can tell if the program is actually reading the file or not. Perhaps you should try printing the variable after you actually try to read the file.

Perhaps something like:
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
#include<iostream>
#include<iomanip>
#include<fstream>

using namespace std;

bool readRainFall(float rainFall[])
{
    //Open the file
    ifstream inputFile("rainfall.txt");
    // Always check that the file opened.
    if(!inputFile)
    {
        cerr << "The file failed to open!\n";
        return false;
    }
    //Initialize month counter
    int month = 0; //first month

    //Read the monthly rainfall in the file

    while(month < 12 & inputFile >> rainFall[month])
    {
        cout << rainFall[month] << " ";

        month++;
    }
    return true;

}//end


int main()
{
    float rainFall[12];

    if(!readRainFall(rainFall))
        return 1;

    cout << "In main(): \n";
    int month = 1;
    for(auto& itr : rainFall)
    {
        cout << "Month: " << month << " Rain Fall: " << rainFall[month] << '\n';
        month++;
    }

    return 0;
}


@jlb
Change the loop on line 42:
1
2
3
4
for(int month = 0; month < 12; month++)
    {
        cout << "Month: " << month + 1 << " Rain Fall: " << rainFall[month] << '\n';
    }
Actually it probably should look more like:

1
2
3
4
5
    for(auto& itr : rainFall)
    {
        cout << "Month: " << month << " Rain Fall: " << itr << '\n';
        month++;
    }
Topic archived. No new replies allowed.