1.11M Members

Rainfall

 
0
 

I am working on a rainfall program that will ask the user to enter the total rainfall for each month, then will calculate the total rainfall for the year, the average monthly rainfall and is supposed to display the month names for the highest and lowest month. I have everything else working but my months are displaying numbers not the actual month. I have looked at previous rainfall programs in daniweb, but they are of no help, I am a beginner, pls help

#include <iostream>

using namespace std;



int main()
{
    const int NUM_MONTHS = 12;           //number of values
   int values[NUM_MONTHS];              //each value
   int month;                           //loop counter
   int lowest, highest, rainfall, count;
   double average;
   double total = 0;   
   
   //Array with the names of the months
   string months[NUM_MONTHS] = {"January", "February", "March", "April", "May", "June",                         
                         "July", "August", "September", "October", "November", "December"};
   //Input rainfall for each month
   for(int month = 1; month <= NUM_MONTHS; month++)
   {
       cout << "Enter the total rainfall for " << months[month-1] <<  ": ";
       cin >> rainfall;   
   
   //Input validation -  enter a value over or equal to 0
   if ( rainfall < 0 )
   {
             cout << "The number you have entered is invalid." << endl;
             cout << "Please reenter: ";
             cin >> rainfall;
   }
    //Calculate total rainfall for the year
             total += rainfall;
}
   
   //Calculate the average monthly rainfall
   average = total/12;
   
   //Find the months with the highest rainfall and lowest rainfall
  highest = values[0];
  lowest = values[0];

        for(int month = 0; month < NUM_MONTHS; month++)
        {
            if(values[month] > highest)
            highest = values[month];
            if(values[month] < lowest)
            lowest = values[month];
        }
   //Display total rainfall for year, monthly average, highest and lowest months
   cout << "\n\nThe total rainfall for the year is: " << total << " inches"
        << "\nThe average monthly rainfall is: " << average << " inches";
   cout << "\nThe month with the highest rainfall is: " << highest
        << "\nThe month with the lowest rainfall is: " << lowest << endl;
   
   
   
          system("pause");
          return 0;
          }
 
0
 

Hello tricket 7,
Already you have found the month with highest and lowest rainfall. So instead of giving highest in line 53 give months[highest]. Note that January will be in months[0]. So if highest is 1 then it will print February rather than January. You will have to make some changes there.

 
0
 

I am working on a rainfall program that will ask the user to enter the total rainfall for each month, then will calculate the total rainfall for the year, the average monthly rainfall and is supposed to display the month names for the highest and lowest month. I have everything else working but my months are displaying numbers not the actual month.

Where are you outputting the Month Names?

 
-1
 

Hello tricket 7,
Already you have found the month with highest and lowest rainfall. So instead of giving highest in line 53 give months[highest]. Note that January will be in months[0]. So if highest is 1 then it will print February rather than January. You will have to make some changes there.

Would I then use an if statement? For example

if (months[0] == highest)
{
  highest = "January";
}

and so on......
would something like that fix the problem

 
0
 

Where are you outputting the Month Names?

That is where I am lost, if I enter months[highest] or highest[months] I get an error, I suppose I am not really sure how to output the month names.....

 
0
 

I think there is a confusion between month(integer variable) and months(string).
As i said earlier if you give months[highest] you wont get error. If you get error then it would be because of this . You have given highest=values[0]; in line 40 but you haven't filled the values[ ] with integer values at all.

highest = "January";

highest is an integer variable. You can't assign a string to it. Try copying all the month names in the months[ ]
starting from month[1] using strcpy().

 
-1
 

I would recommend that you use an std::string instead of a char*. std::strings are safer than char*s and don't require functions like strcpy. So for example:

std::string highest_str;
//you don't need the "std::" if you have "using namespace std;" in your code

//...some code here...

highest_str = "January";

//...some code here...
 
-1
 

How about this:

highest = 0;
lowest = 0;
 
for(int month = 0; month < NUM_MONTHS; month++)
{
   if ( values[month] > values[highest] )
      highest = month;
   if ( values[month] < values[lowest] )
      lowest = month;
}
//Display total rainfall for year, monthly average, highest and lowest months
cout << endl << endl
     << "The total rainfall for the year is: " << total << " inches" << endl
     << "The average monthly rainfall is: " << average << " inches" << endl
     << "The month with the highest rainfall is: " << months[highest] << endl
     << "The month with the lowest rainfall is: " << months[lowest] << endl;

?

 
-3
 

That is where I am lost,

Your code isn't that large, you can't be that lost

if I enter months[highest] or highest[months] I get an error,

An error. Like it crashes the computer? Like it burns the roast? Like it nukes the city? Never never say "an error". We are not psychic and hate to guess.

I suppose I am not really sure how to output the month names.....

The same way you output anything, with cout << ...

 
0
 

Thank you for your help, I was getting things mixed up as stated previously with string and integer. It does display a month for the highest rainfall and a month on the lowest rainfall, not the correct months, but I think I can take it from here and figure it out. I appreciate all of your help

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article