What is wrong with my C++ code?

I am just learning C++ and have had to write a program calculating average rainfall.  From everything I can tell, I did everything right, but I still get errors when I try to compile.

I am using Micrsoft Visual C++ 2008 Express and the build log is attached.  Here is the source code:

//A program to calculate average rainfall
//for a period of three months.
#include <iostream>
using namespace std;

int main ()
{
      char month1, month2, month3;        //To hold the name of the month
      double inches1, inches2, inches3;       //To hold the rainfall total for month
      double average;                   //To hold the average

      //Get the names of the three months used in the calculation
      cout << "This program will calculate the average rainfall, in inches, for three months.\n";
      cout << "Enter the names of the three months you want to calculate the average rainfall\n";
      cout << "for, seperated by a space. (i.e. March April May):";
      cin >> month1 >> month2 >> month3;

      //Get the amount of rainfall, in inches, for each month.
      cout << "What was the total rainfall, in inches, for the month of" << month1 << "?/n";
      cin >> inches1;
      cout << "What was the total rainfall, in inches, for the month of" << month2 << "?/n";
      cin >> inches2;
      cout << "What was the total rainfall, in inches, for the month of" <<month3 << "?/n";
      cin >> inches3;

      //Calculate the average rainfall for the three months.
      average = (inches1 + inches2 + inches3) / 3.0;

      //Display the average rainfall
      cout >> "The average rainfall for << month1 << "," << month2 << ;
      cout >> ", and" << month3 << "is" << average << "inches./n";
      return 0;
}

bdeyreAsked:
Who is Participating?
 
khdaniConnect With a Mentor Commented:
i guess that would be the fixed code:
#include <iostream>
using namespace std;
 
int main ()
{	
	char month1[20], month2[20], month3[20];        //To hold the name of the month
	double inches1, inches2, inches3;       //To hold the rainfall total for month
	double average;                   //To hold the average
 
	//Get the names of the three months used in the calculation
	cout << "This program will calculate the average rainfall, in inches, for three months.\n";
	cout << "Enter the names of the three months you want to calculate the average rainfall\n";
	cout << "for, seperated by a space. (i.e. March April May):";	
	gets(month1);
	gets(month2);
	gets(month3);
 
	//Get the amount of rainfall, in inches, for each month.
	cout << "What was the total rainfall, in inches, for the month of " << month1 << "?\n";
	cin >> inches1;
	cout << "What was the total rainfall, in inches, for the month of " << month2 << "?\n";
	cin >> inches2;
	cout << "What was the total rainfall, in inches, for the month of " <<month3 << "?\n";
	cin >> inches3;
 
	//Calculate the average rainfall for the three months.
	average = (inches1 + inches2 + inches3) / 3.0;
 
	//Display the average rainfall
	cout << "The average rainfall for " << month1 << "," << month2;
		cout << ", and " << month3 << " is " << average << " inches.\n";
	return 0;
}

Open in new window

0
 
AzyreConnect With a Mentor Commented:
Not a programmer, and this looks like homework, but I'd imagine you issue is here:

      cout << "for, separated by a space. (i.e. March April May):";
      cin >> month1 >> month2 >> month3;

As your only going to pull data for month1.  There's no qualifier to end the keyboard capture if a space is entered so it will think all 3 months are one singular input.

As we ca';t fix homework for you, either write a snip to create an array based on the spaces after a single input or create 3 separate inputs like you did for inches.
0
 
bdeyreAuthor Commented:
It is homework, I wasn't aware of the policy.  Thanks for your help and I will give it a try.
0
 
khdaniConnect With a Mentor Commented:
if you want to input more than one character, you should use char array or std::string.
for cout you should use the << operator
and for cin >> operator
0
 
bdeyreAuthor Commented:
That fixed it, but I'll have to change it so they press enter after entering each month.  Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.