Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7526577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:52:11+00:00 2026-05-30T03:52:11+00:00

Ok the only problem with this program is at the end where I try

  • 0

Ok the only problem with this program is at the end where I try to decide which months rain totals were lowest or highest. My output for either works great UNLESS “Jan”(January) is either the highest or lowest.Then either will just be left blank. All other variations work great. Any idea as to why this is happening? Is an index of my array off somewhere? Thanks in advance for any advice.

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

int main()
{
    //declare and initialize variable and arrays
    string year[] = {"Jan", "Feb", "Mar", "April", "May", "June",
            "July", "August", "Sept", "Oct", "Nov", "Dec"}; 

    const int totalMonths = 12;

    double month[totalMonths]; 

    double average; 
    double total = 0;

    cout << "This program will calculate the total annual rainfall,\n";
    cout << "calculate the monthly average rainfall, and display\n";
    cout << "which month had the lowest amount of rainfall and which\n";
    cout << "month had the highest amount of rainfall. Please enter the\n";
    cout << "rainfall data in inches." << endl;
    cout << "----------------------------------------------------------" << endl;

    // prompt user for input, keep a total sum of data entered
    for(int i = 0; i < 12; i++)
    {
        cout << "Enter total rainfall for " << year[i] << endl;
        cin >> month[i];
        total += month[i];
    while(month[i] < 0)
    {
        cout << "Rainfall cannot be negative!\n";
        cout << "Please re-enter positive numbers" << endl;
        cin >> month[i];
    }       

    }

    //average the total rainfall
    average = total / totalMonths;

    cout << setprecision(1) << fixed;
    cout << "Total Annual rainfall is: " << total << endl;
    cout << "The average rainfall per month is: " << average << endl;

    //determine which month had the lowest and highest amount of rainfall
    double highest = 0;
    string highMonth;
    highest = month[0];
    for(int count = 0; count < totalMonths; count++)
    {
        if(month[count] > highest)
        {
            highest = month[count];
            highMonth = year[count];

        }

    }

    double lowest = 0;
    string lowMonth;
    lowest = month[0];
    for(int count = 0; count < totalMonths; count++)
    {
        if(month[count] < lowest)
        {
            lowest = month[count];
            lowMonth = year[count];
        }
    }

    cout << "The month with the highest rainfall is: " << highMonth << endl;
    cout << "The month with the lowest rainfall is: " << lowMonth << endl;  

return 0;
}

Example of the output:

This program will calculate the total annual rainfall,
calculate the monthly average rainfall, and display
which month had the lowest amount of rainfall and which
month had the highest amount of rainfall. Please enter the
rainfall data in inches.
----------------------------------------------------------
Enter total rainfall for Jan
1
Enter total rainfall for Feb
2
Enter total rainfall for Mar
3
Enter total rainfall for April
4
Enter total rainfall for May
5
Enter total rainfall for June
6
Enter total rainfall for July
7
Enter total rainfall for August
89
Enter total rainfall for Sept
12
Enter total rainfall for Oct
13
Enter total rainfall for Nov
14
Enter total rainfall for Dec
15
Total Annual rainfall is: 171.0
The average rainfall per month is: 14.2
The month with the highest rainfall is: August
The month with the lowest rainfall is: <-------- //BLANK!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:52:12+00:00Added an answer on May 30, 2026 at 3:52 am

    I spotted two issues in this program:

    1. The reason why you’re here — you’re not allowing the first month to match:

      double highest = 0;
      string highMonth;
      highest = month[0];
      for(int count = 0; count < totalMonths; count++)
      {
          if(month[count] > highest)
          {
              highest = month[count];
              highMonth = year[count];
          }
      }
      

      In this case, the month[count] > highest test can never be true for January. Amend the test:

          if(month[count] >= highest)
      

      This allows the first month to match. Keep this in mind, you’ll probably make this mistake again — I know I have. (The fix is similar for the other case.)

    2. Another issue you haven’t run into yet, but I’m sure your professor will notice: you don’t handle negative numbers correctly:

      for (int i = 0; i < 12; i++)
      {
          cout << "Enter total rainfall for " << year[i] << endl;
          cin >> month[i];
          total += month[i];
          while (month[i] < 0)
          {
              cout << "Rainfall cannot be negative!\n";
              cout << "Please re-enter positive numbers" << endl;
              cin >> month[i];
          }
      }
      

      What happens if the user types in a negative number? The total += month[i] line will still influence the total (and thus the average), even though your output claims to disallow negative numbers. This might be difficult to spot during simple hand-testing unless you also hand-calculate the correct numbers, too. (Hint: when you can check input against hand-calculated results, it’s almost always worthwhile.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am only having this problem when running my program on a Mac; linux
[Edit: This problem applies only to 32-bit systems. If your computer, your OS and
This problem is only in IE. Consider the following HTML: <html> <body> <div style='position:absolute;left:1em;right:1em;top:1em;bottom:1em;overflow:auto;>
My example is similar to this - The only problem is I cannot rewrite
This problem is similar to when creating alternate colour table-rows, only we are dealing
This seems to be a problem related to Safari only. I've tried 4 on
How do I get past this variable initialization problem? If I only could figure
In this C++ code I try to erase element from the end of the
Can I copy only the error message from Eclipse Problems with instead of this
My only problem is making them line up three-across and have equal spacing. Apparently,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.