Dont understand issue with getline(something else?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream> 
#include <string>
#include <iomanip>
using namespace std; 

int main()
{
	string monthone;
	string monthtwo;
	string monththree;
	double monthone1 = 0;
	double monthtwo2 = 0;
	double monththree3 = 0;
	double average = (monthone1 + monthtwo2 + monththree3) ;
	double total = average / 3;

	cout << " Please provide the first month " << endl;
	getline(cin,monthone);
	
	cout << "Please provide the second month " << endl;
	getline(cin,monthtwo);
	
	cout << " Please provide the third month " <<endl ;
	getline(cin, monththree); 
	
	cout << ""<< endl;



	cout << " How many inches did it rain in " << monthone<< "?" << endl;
	cin >> monthone1; 
	cout << " How many inches did it rain in " << monthtwo << "?" << endl;
	cin >> monthtwo2;
	cout << " How many inches did it rain in " << monththree << "?" << endl;
	cin >> monththree3;
	cout << " Considering all factors, the average rainfall for" << monthone << 
		"," << monthtwo << " and" << monththree << " is  " << total << " inches"  << endl << endl;

	




	


return 0;
}
Can't get total to display, everything else is fine though. :( thanks in advance
Last edited on
You did not display total. So what you do is what you get.
1
2
double average = (monthone1 + monthtwo2 + monththree3) ;
	double total = average / 3;


You need to do these after you input the inches of rain. Right now, it just sets the average to 0 and then divides 0 by 3 to make total 0. You can declare them where you have them now, just put the assignments after you cin the inches.

Also, there is an issue with using getline and cin together. getline will leave the end of line character generated from pressing enter in the input stream, then when cin is called for the first time it will just take that end of line character and never ask the user for input. I'm guessing that your program doesn't let the user input the inches for the first month? At least it seems like it shouldn't. You can fix this by just using cin everywhere instead of getline, or by putting cin.ignore() before you use cin. cin.ignore will remove the end of line character from the stream, thus making cin work. Sorry if this explanation was unclear.
I can't think of a month that needs a whitespace, so i would use cin instead of getline.

Also:

1
2
3
4
5
6
7
8
string monthone;
	string monthtwo;
	string monththree;
	double monthone1 = 0;
	double monthtwo2 = 0;
	double monththree3 = 0;
	double average = (monthone1 + monthtwo2 + monththree3) ;
	double total = average / 3;


Remember how c++ runs the program, from top down. The program will run and declare all variables, setting them to zero then performing the actions. You pretty much write:

1
2
average = (0 + 0 + 0);
total = 0 / 3;


THEN you read in variables, it doesn't matter if you change the values later, nothing will update unless you call the function AFTER you have the values. You would want to move those statements below all of your cin statements.
Thank you alll for the prompt reply. However, this is only my third or fourth time writing a program and I don't quite understand what you are trying to tell me. If you could please show an example I think I will get it.
Last edited on
And yes the program does want the user to put in the inches for the first month
Allright! I got the program to work. Thanks everyone. I did use cin and getline together though. Because everytime I tried to use cin instead of getline, the program wouldn't even build. To help me get the full picture, how would you use cin instead of getline over here?:S
Instead of getline(cin,monthone);, it would just be cin >> monthone;
Topic archived. No new replies allowed.