Help with arrays

Hello, this is my first time using a forum and I am very rusty with C++ so forgive any stupid questions. I am required to make a program that receives data form a .txt file and places those values into an array. From the array, I have to calculate the average, the total amount of days, max rainfall in one day, and the total rainy days. The max amount of values in the .txt file will be 31. The file has a sentinel value of 999 so not all 31 days have to be used. Any negative numbers must be ignored. I'm not looking for any answers, I just need to be led in the right direction. I don't know how to insert the data from the .txt file into the array. Any help will be appreciated. Here is the program I have so far:

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

double userInput[31];
int totalDays;
int rainyDays;
double avgRain = 0.0;
double rainTotal;
double maxRain = 0.0;
int count=0;

int main ()
{
ifstream inFile;
inFile.open("raindata.txt");
inFile >> userInput[count];

for(int count=0; count <=30; count ++)
{
if (userInput[count] > 0 && userInput[count] != 999) {
totalDays = totalDays + 1;
rainyDays = rainyDays +1;
rainTotal = rainTotal + userInput[count];
if (userInput[count] >= maxRain) {
maxRain = userInput[count];
}
}
else if (userInput[count] == 0 && userInput[count] != 999)
{
totalDays = totalDays +1;
rainTotal = rainTotal + userInput[count]; if (userInput[count] >= maxRain)
{
maxRain = userInput[count];
}
}

avgRain = rainTotal/totalDays;

}
cout << "The total amount of days is: " << totalDays << endl << endl;
cout << "The amount of rainy days is: " << rainyDays << endl << endl;
cout << "The max amount of rainfall is: " << maxRain << endl <<endl;
cout << "The average amount of rainfall is: " << avgRain << endl <<endl;

return 0;
}
Last edited on
Put this inFile >> userInput[count]; inside the loop.
loop as long as inFile.good() and count <=30

See: http://www.cplusplus.com/reference/iostream/ios/good/

Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
Hello
I usually use IFSTREAM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    int N2P[(m*n)+1][5];    // make array N2P
    ifstream N2Pin("c:\\Games\\N2P.txt", ios::in); //open the file N2P in GAMES folder
    while (!N2Pin.eof()) { // while there is still an unread number in the file
        N2Pin >> N2P[i][j]; // replace data from file N2P into N2P matrix (row i, column j)
        i=i+1; //next row
        if (i>(m*n)) { //when you reach last row of matrix, go to first row and next column
            j=j+1;
            i=1;
        }
        k=k+1;
        if (k>((m*n)*4)) { //when the matrix is full, end the process
        break;
        }
    }
    N2Pin.close();*/  // close the file


in this method, u should have a one column data file; each row of this file should contain one value, without space before and after.
the program read the text file from up to down and u put each read character into your matrix.
I finally got it to work, but now I have a question with the next step in my program. How do I add tags? Once again, sorry for my stupidity.
How do I add tags?
Follow the link above.

When you post something you see right besides the edit box the word Format:. When you click on one of the buttons below, you get those tags as well

@bibalan
What?
Last edited on
Topic archived. No new replies allowed.