Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

# C++ Rain

### #1 ihatetheworld Posted 27 March 2006 - 12:29 PM

ihatetheworld

New Member

• Member
• 6 posts
Do not use global variables
i need to calculate total rainfall
average rain fall
and show highest and lowest month

right now i am trying to display total maybe some one can lead me in the right direction i dont think that is the right way to display total.

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

int getActualRain(int j);
string print_month(int month);
//char January, February;

int main()

{
int i;
float monthly[13];
float ActualRain[13];
double total;

for (i = 1; i < 13; i++)
{
cout << "Enter Monthly Rainfall for " << print_month(i) << ": ";
cin >> monthly[i];
}

total = (monthly[i]* ActualRain[13]);
cin >> total;
//i dont think this will display total

return 0;
}

string print_month(int month)
{
string result;
switch (month)
{
case 1:
result = "January";
break;
case 2:
result = "February";
break;
case 3:
result = "March";
break;
case 4:
result = "April";
break;
case 5:
result = "May";
break;
case 6:
result = "June";
break;
case 7:
result = "July";
break;
case 8:
result = "August";
break;
case 9:
result = "September";
break;
case 10:
result = "October";
break;
case 11:
result = "November";
break;
case 12:
result = "December";
break;
default:
cout << "Invalid Value!\a";
break;
}
return result;
}
• 0

### #2 StephenL Posted 28 March 2006 - 10:15 AM

StephenL

Member

• Member
• 12 posts
hi,

int main()

{
int i;
float monthly[13];
float ActualRain[13];
double total;

for (i = 1; i < 13; i++)
{
cout << "Enter Monthly Rainfall for " << print_month(i) << ": ";
cin >> monthly[i];
}
total = (monthly[i]* ActualRain[13]);
cin >> total;
//i dont think this will display total
return 0;
}

I assume these lines of code are within a loop (hence the 'i' used) ?

total = (monthly[i]* ActualRain[13]);
cin >> total;
//i dont think this will display total

if so, then you'll need to add each individual month's rain amount to the total so:

int total = 0;

for(int currMonth = 0; currMonth < TotalMonths; currMonth++)
{
total = total + ActualRain[currMonth];
}

this way the actual rain for each month will be added to the running total

Plus, I know you're not supposed to use global constants but using 'magic' numbers in the loop is not a good practice. You could declare a constant within main{} to set the TotalMonths to 13? (should be 12 really)

Note:
I assumed that the ActualRain[] float array was the array that actually holds the actual rainfall for each month.

Edited by StephenL, 28 March 2006 - 10:17 AM.

• 0

### Similar Topics

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users