?
Solved

What is wrong with my C++ code?

Posted on 2009-04-16
5
Medium Priority
?
854 Views
Last Modified: 2013-12-14
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;
}

0
Comment
Question by:bdeyre
  • 2
  • 2
5 Comments
 
LVL 3

Assisted Solution

by:Azyre
Azyre earned 400 total points
ID: 24161670
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
 

Author Comment

by:bdeyre
ID: 24161862
It is homework, I wasn't aware of the policy.  Thanks for your help and I will give it a try.
0
 
LVL 3

Accepted Solution

by:
khdani earned 1600 total points
ID: 24162057
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
 
LVL 3

Assisted Solution

by:khdani
khdani earned 1600 total points
ID: 24162078
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
 

Author Comment

by:bdeyre
ID: 24162167
That fixed it, but I'll have to change it so they press enter after entering each month.  Thanks!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question