Why is the output in random order?

Also what's with the random 0?

Here's what I put in the input file:
3.80
5.2
4.7
6.3
3.9
0.91
0.91
2.16
3.40
2.0
2.00
3.70



Here's my code:

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

//function prototypes
double getrainTotal(double[], int);
double gethighestRainfall(double rainfalls[], int MONTHS);
double getlowestRainfall(double rainfalls[], int MONTHS);


int main() {

//get inputs
const int MONTHS = 12;

string monthName[MONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
double rainfallAmount[MONTHS];
int count=0;
double avg;
double totalRain, mostRain, leastRain;
string name, mostMonth, leastMonth;



// open the infile
ifstream inFile;
inFile.open("in6s17.txt");

if (!inFile.is_open());
{
double d;
cout << "Error opening file"<<endl;
inFile>>d;
cout << d <<endl;
}


while (count<MONTHS && inFile >> rainfallAmount[count])
count++;




inFile.close();



cout << "YEARLY MONTHLY RAINFALL" << endl;
cout << "Month"<<setw(20)<<"Rainfall in inches"<<endl;


for (int count = 0; count < MONTHS; count++) {
cout << monthName[count]<<setw(17)<<rainfallAmount[count]<<endl;

}

totalRain = getrainTotal (rainfallAmount, MONTHS);
cout << "The total rainfall for the year is "<<totalRain<<" inches."<<endl;

avg = totalRain/MONTHS;
cout << "The averag monthly rainfall is "<<avg<<" inches."<<endl;


mostRain = gethighestRainfall(rainfallAmount, MONTHS);
cout << "The highest rainfall of "<<mostRain<<" occured in April"<<endl;

leastRain = getlowestRainfall (rainfallAmount, MONTHS);
cout << "The lowest rainfall of "<<leastRain<<" occured in June"<<endl;
cout << "The lowest rainfall of "<<mostRain<<" occured in July"<<endl;

cout <<"Rainfall of 4 inches or more occured in February."<<endl;
cout <<" "<<endl;
cout<<"Rainfall of 4 inches or more occured in March."<<endl;
cout << " "<<endl;
cout << "Rainfall of 4 or more inches occured in April."<<endl;

cout<< "Programmer: "<<name<<endl;




return 0;

}

double getrainTotal (double rainfalls[], int MONTHS)

{
double total=0;
for ( int count = 0; count < MONTHS; count++)
total += rainfalls[count];
return total;
}

double gethighestRainfall(double rainfalls[], int MONTHS)
{
double highest;
highest = rainfalls[0];
for (int count=1; count <MONTHS; count++)
{
if (rainfalls[count] > highest)
highest = rainfalls[count];
}
return highest;

}

double getlowestRainfall(double rainfalls[], int MONTHS)

{
double lowest;
lowest = rainfalls[0];
for (int count =1; count <MONTHS; count++)
{
if (rainfalls[count]< lowest)
lowest = rainfalls[count];

}
return lowest;

}




And here's the output. It's so sloppy! And what's with the random 0 there and the value for January (3.8) all the way up there?

Error opening file
3.8
YEARLY MONTHLY RAINFALL
Month Rainfall in inches
January 5.2
February 4.7
March 6.3
April 3.9
May 0.91
June 0.91
July 2.16
August 3.4
September 2
October 2
November 3.7
December 0
The total rainfall for the year is 35.18 inches.
The average monthly rainfall is 2.93167 inches.
The highest rainfall of 6.3 occured in April
The lowest rainfall of 0 occured in June
The lowest rainfall of 6.3 occured in July
Rainfall of 4 inches or more occured in July.

Rainfall of 4 inches or more occured in September

Program ended with exit code: 0


(Btw please don't add anything else to the #include part. iostream, iomanip, fstream, and string would be the only ones I'm going to use.)
Pleas use code tags: http://www.cplusplus.com/articles/z13hAqkS/

See a problem here :+)
for (int count =1; count <MONTHS; count++)

What is this?

m
1
2
3
4
5
6
7
8
9
10
11
12
ostRain = gethighestRainfall(rainfallAmount, MONTHS);
cout << "The highest rainfall of "<<mostRain<<" occured in April"<<endl;

leastRain = getlowestRainfall (rainfallAmount, MONTHS);
cout << "The lowest rainfall of "<<leastRain<<" occured in June"<<endl;
cout << "The lowest rainfall of "<<mostRain<<" occured in July"<<endl;

cout <<"Rainfall of 4 inches or more occured in February."<<endl;
cout <<" "<<endl;
cout<<"Rainfall of 4 inches or more occured in March."<<endl;
cout << " "<<endl;
cout << "Rainfall of 4 or more inches occured in April."<<endl;

Last edited on
Topic archived. No new replies allowed.