Im having problem with this code I made for finding the average rain fall. When the program starts. it is suppose to go through each month and you are able to input the rainfall. When the months show up in the CMD it is just a bunch of numbers and letters. I believe the array is set up right. maybe im missing something.

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

int main () 
	
{	double avgRain = 0;
	double rainSum = 0;
	int count = 0;
	int monthlyTotals[12];
	string monthNames[] = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
	cout << "Please enter the amount of rainfall for each month, and press enter after each: " << endl;
	for (count = 0; count <= 12; count++)
	{
		cout << monthNames << " : ";
		cin >> monthlyTotals[count];
	}
		
	for (count =0; count <=12; count++)
		rainSum = rainSum + monthlyTotals[count];
		avgRain = rainSum / 12;
	for (count = 0; count <=12; count++)
	{	
		cout << "Output : " << endl;
		cout << monthNames << "\t" << monthlyTotals[count] << endl;
	}
	
	cout << rainSum << "is the total rainfall for the year\n" << avgRain << " is the average for the year.\n";
	return 0;
}

if anything else is noticed to be wrong or could be done better. plz reply.

Recommended Answers

All 5 Replies

You need to have an index on the monthNames[] array, otherwise what it will return is the pointer (address) of the array rather than the value.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main ()

{
    double avgRain = 0;
    double rainSum = 0;
    int count = 0;
    int monthlyTotals[12];
    string monthNames[] = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
    cout << "Please enter the amount of rainfall for each month, and press enter after each: " << endl;
    for (count = 0; count < 12; count++)
    {
        cout << setw(9) << left << monthNames[count] << " : ";
        cin >> monthlyTotals[count];
    }

    for (count = 0; count < 12; count++)
        rainSum += monthlyTotals[count];

    avgRain = rainSum / 12;


    cout << "Output : " << endl;
    for (count = 0; count < 12; count++)
    {
        cout << setw(9) << left << monthNames[count]
             << setw(3) << right << monthlyTotals[count] << endl;
    }

    cout << rainSum << " is the total rainfall for the year\n" << avgRain << " is the average for the year.\n";
    return 0;
}

Note the use of the setw() manipulator, which gives better control over the spacing than the tab character does. You will need to #include <iomanip> in order to use the manipulators, however. Note also the change in the index conditionals, such that the count variable goes from 0 to 11, rather than 0 to 12. The way it was originally, it was causing a segfault when it reached 12 and overran the arrays.

alright i have done that, but when i put in the [count] after monthNames. the << have a red under line saying no operator matches operands.

O, shoot cant belive i missed that..... also I also added #include <string>. Having a problem with the ending, its not adding up correctly now, im going to take a peak at it, will post back any ideas plz post.

Thanks!

Oops. I've updated my code changes since then. Sorry.

thats alright man I fixed like the same way lol. Thanks for the Help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.