Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

C++ Rain


  • Please log in to reply

#1
ihatetheworld

ihatetheworld

    New Member

  • Member
  • Pip
  • 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

Advertisements


#2
StephenL

StephenL

    Member

  • Member
  • PipPip
  • 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

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP