rain

L

littlegirl

girls should not take c++ but can some one hel

Do not use global variable
i need to calculate total rainfal
average rain fal
and show highest and lowest mont

i cant seem to get the total or the highest or lowest to display an
help thank you



#include <iostream
#include <string
using namespace std

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

int main(


int i
float AverageRain[13]
float ActualRain[13]

for (i = 1; i < 13; i++

cout << "Enter Monthly Rainfall for " << print_month(i
<< ": "
cin >> AverageRain

double TotalMonths
monthly
int total = 0

for(int currMonth = 0; currMonth < TotalMonths; currMonth++

total = total + ActualRain[currMonth]



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
 
J

Jaspreet

littlegirl said:
girls should not take c++ but can some one help

Oh thats why there were hardly any girls by the time I finished my last
semester. However I would love to have them around. :)
Do not use global variables

Yes they are not cosidered good. One of the methods might accidently
modify their values.
i need to calculate total rainfall
average rain fall
and show highest and lowest month

i cant seem to get the total or the highest or lowest to display any
help thank you.






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

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


int main()

{
int i;
float AverageRain[13];

Average variable needs to be just 1 variable. Why have an array of 13
elements ?

float AverageRain;
float ActualRain[13];
There are 12 months. So declare ActualRain to be of size 12.
float ActualRain[12];
for (i = 1; i < 13; i++)

always start index from 0.
for (i = 0; i < 12; i++)
{
cout << "Enter Monthly Rainfall for " << print_month(i)

cout << "Enter Monthly Rainfall for " << print_month(i+1)

<< ": ";
cin >> AverageRain;
cin>>ActualRain;

}
double TotalMonths,
monthly;
int total = 0;

TotalMonths needs to be initialised with value 12.
for(int currMonth = 0; currMonth < TotalMonths; currMonth++)
{
total = total + ActualRain[currMonth];

}
Display total variable.

cout<<total<<"\n";
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;

}
 
S

Sunil Varma

littlegirl said:
girls should not take c++ but can some one help

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

i cant seem to get the total or the highest or lowest to display any
help thank you.






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

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


int main()

{
int i;
float AverageRain[13];
float ActualRain[13];


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

----------------------------------
Make it
cin>>AverageRain;
----------------------------------
}
double TotalMonths,
monthly;
int total = 0;

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

}


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;
 
G

Gernot Frisch

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;


You can shorten this by creating an array of constant strings and
return an indexed copy of the contents:

string print_month(int month) // base index=1 !!
{
const char* results[]={
"January", "Feb", "Mar", "Ap", "M", "Jn",
"Jl", "A", "S", "O", N", "D"
};
return string(results[month-1]);
}

Also, this function should be called "get_month_name" instead of
"print_month", since it doesn't actually print something ;)

Bye,
-Gernot
 
P

Phlip

littlegirl said:
girls should not take c++ but can some one help

Totally. Girls are smarter than guys, and should lead them to do all the
stupid jobs, such as cleaning up stables or writing C++ code.
 
O

osmium

Gernot Frisch said:
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;


You can shorten this by creating an array of constant strings and return
an indexed copy of the contents:

string print_month(int month) // base index=1 !!
{
const char* results[]={
"January", "Feb", "Mar", "Ap", "M", "Jn",
"Jl", "A", "S", "O", N", "D"
};
return string(results[month-1]);
}

Also, this function should be called "get_month_name" instead of
"print_month", since it doesn't actually print something ;)

I didn't look at her code, but presuming it works ....She had an invalid
value as a possibility which you seem to ignore. Why do you feel you are
authorized to change the implicit specification?
 
G

Gernot Frisch

I didn't look at her code, but presuming it works ....She had an
invalid value as a possibility which you seem to ignore. Why do you
feel you are authorized to change the implicit specification?

I was just pointing out that an array would be shorter/easier to code.
I assumed she's clever enough to figure out how to perform a range
test before that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top