C++ program

Pages: 12
Heyy guys,
my code is below..i don't know what is happening but when i enter the first month in the output window enter month#2 and enter month#3 gets skipped and also the Now enter the amount of rain (in inches) for each month" and then there are two letters of month are displayed and then i type something and it closes down..i am new at C++.

Thanks



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

int main ()

{

char userInput1, userInput2, userInput3;
char userInput4, userInput5, userInput6;
double average_monthly_rainfall;


cout << "Enter month #1: " << endl;
cin >> userInput1;

cout << "Enter month #2: " << endl;
cin >> userInput2;

cout << "Enter month #3: " << endl;
cin >> userInput3;
cout << endl;

cout << "Now enter the amount of rain (in inches) for each month" << endl;
cout << endl;

cout << userInput1 << endl;
cin >> userInput4;

cout << userInput2 << endl;
cin >> userInput5;

cout << userInput3 << endl;
cin >> userInput6;

average_monthly_rainfall = (userInput4 + userInput5 + userInput6) / 3;

cout << "The average monthly rainfall for June, July, August was"
<< average_monthly_rainfall << "inches" << endl;


return 0;
}

Please put your code in code tags!
Then I'll have a look at it :)
userInput4, 5 and 6 should be doubles, not chars. I suggest choosing better names for your variables. And, generally, when you have something like variable1, variable2 ... variableN, you should be using a container, but you might not have learned how yet.
Also, I know why they skip. userInput1, 2 and 3 are chars. They only accept one character. So if I input August, it will store A in userInput1, u in userInput 2 and so forth.
I suggest you use strings.

-Xander
filipe your idea doesn't work and what is code tags xander333 ?? someone tell me what i need to add i mean i don't get it
This are code tags:

[code*] [/code*] (remove the *s)

it will show like this:

[code]
#include <iostream>
#include <string>
using namespace std;

int main ()

{

char userInput1, userInput2, userInput3;
char userInput4, userInput5, userInput6;
double average_monthly_rainfall;


cout << "Enter month #1: " << endl;
cin >> userInput1;

cout << "Enter month #2: " << endl;
cin >> userInput2;

cout << "Enter month #3: " << endl;
cin >> userInput3;
cout << endl;

cout << "Now enter the amount of rain (in inches) for each month" << endl;
cout << endl;

cout << userInput1 << endl;
cin >> userInput4;

cout << userInput2 << endl;
cin >> userInput5;

cout << userInput3 << endl;
cin >> userInput6;

average_monthly_rainfall = (userInput4 + userInput5 + userInput6) / 3;

cout << "The average monthly rainfall for June, July, August was"
<< average_monthly_rainfall << "inches" << endl;


return 0;
}
[/code]

Ok wait...from some odd reason it doesn't work now :S

~Xander
Last edited on
[code]#include <iostream>
#include <string>...


Actually, they show up like this:

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
#include <iostream>
#include <string>
using namespace std;

int main ()

{

char userInput1, userInput2, userInput3;
char userInput4, userInput5, userInput6;
double average_monthly_rainfall;


cout << "Enter month #1: " << endl;
cin >> userInput1;

cout << "Enter month #2: " << endl;
cin >> userInput2;

cout << "Enter month #3: " << endl;
cin >> userInput3;
cout << endl;

cout << "Now enter the amount of rain (in inches) for each month" << endl;
cout << endl;

cout << userInput1 << endl;
cin >> userInput4;

cout << userInput2 << endl;
cin >> userInput5;

cout << userInput3 << endl;
cin >> userInput6;

average_monthly_rainfall = (userInput4 + userInput5 + userInput6) / 3;

cout << "The average monthly rainfall for June, July, August was" 
<< average_monthly_rainfall << "inches" << endl;


return 0;
}


Oops.

-Albatross
Last edited on
ok..i need to do this please hlep me xander
Yes, really confused as to why it didn't work...
Worked always for me so far...

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
#include <iostream>
#include <string>
using namespace std;

int main ()

{

char userInput1, userInput2, userInput3;
char userInput4, userInput5, userInput6;
double average_monthly_rainfall;


cout << "Enter month #1: " << endl;
cin >> userInput1;

cout << "Enter month #2: " << endl;
cin >> userInput2;

cout << "Enter month #3: " << endl;
cin >> userInput3;
cout << endl;

cout << "Now enter the amount of rain (in inches) for each month" << endl;
cout << endl;

cout << userInput1 << endl;
cin >> userInput4;

cout << userInput2 << endl;
cin >> userInput5;

cout << userInput3 << endl;
cin >> userInput6;

average_monthly_rainfall = (userInput4 + userInput5 + userInput6) / 3;

cout << "The average monthly rainfall for June, July, August was" 
<< average_monthly_rainfall << "inches" << endl;


return 0;
}
hlp mee
pleaseeeeeeeeeeeeeeeeeeeeeeeeeee
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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{


string userInput1, userInput2, userInput3;
int userInput4, userInput5, userInput6;
double average_monthly_rainfall;


cout << "Enter month #1: " << endl;
getline(cin, userInput1);

cout << "Enter month #2: " << endl;
getline(cin, userInput2);

cout << "Enter month #3: " << endl;
getline(cin, userInput3);
cout << endl;

cout << "Now enter the amount of rain (in inches) for each month" << endl;
cout << endl;

cout << userInput1 << endl;
cin >> userInput4;

cout << userInput2 << endl;
cin >> userInput5;

cout << userInput3 << endl;
cin >> userInput6;

average_monthly_rainfall = (userInput4 + userInput5 + userInput6) / 3;

cout << "The average monthly rainfall for " << userInput1 << ", ";
cout << userInput2 << " and " << userInput3 << " was ";
cout << average_monthly_rainfall << " inches" << endl;


	return 0;
}


Give this a try.
Last edited on
it works fine upto when i enter the first value for amount of rain for my first month which is june but then it just says july, august and some other crap..thanks if u can try some more
Well you declared userInput as char. So when you're inputting do you enter single character like, 'A' or 'B'? If not you're going to have problems. If you don't want to use the code I posted above and you want to use yours do it like this :

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


int main ()

{

string userInput1, userInput2, userInput3;
int userInput4, userInput5, userInput6;
double average_monthly_rainfall;


cout << "Enter month #1: " << endl;
cin >> userInput1;

cout << "Enter month #2: " << endl;
cin >> userInput2;

cout << "Enter month #3: " << endl;
cin >> userInput3;
cout << endl;

cout << "Now enter the amount of rain (in inches) for each month" << endl;
cout << endl;

cout << userInput1 << endl;
cin >> userInput4;

cout << userInput2 << endl;
cin >> userInput5;

cout << userInput3 << endl;
cin >> userInput6;

average_monthly_rainfall = (userInput4 + userInput5 + userInput6) / 3;

cout << "The average monthly rainfall for June, July, August was "
<< average_monthly_rainfall << " inches" << endl;


return 0;
}
Last edited on
Could you post your test case (input, output expected, output that you get)
just send me the code..the purpose is to get the average rain in inches when the user enters all the things for ex june as a month and 5.4 inches as rain in that month and then at the end the average of 3 months rain in inches.
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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{


string userInput1, userInput2, userInput3;
float userInput4, userInput5, userInput6;
float average_monthly_rainfall;


cout << "Enter month #1: " << endl;
getline(cin, userInput1);

cout << "Enter month #2: " << endl;
getline(cin, userInput2);

cout << "Enter month #3: " << endl;
getline(cin, userInput3);
cout << endl;

cout << "Now enter the amount of rain (in inches) for each month" << endl;
cout << endl;

cout << userInput1 << endl;
cin >> userInput4;

cout << userInput2 << endl;
cin >> userInput5;

cout << userInput3 << endl;
cin >> userInput6;

average_monthly_rainfall = (userInput4 + userInput5 + userInput6) / 3.0;

cout << "The average monthly rainfall for " << userInput1 << ", ";
cout << userInput2 << " and " << userInput3 << " was ";
cout << average_monthly_rainfall << " inches" << endl;


	return 0;
}


INPUT / OUTPUT :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Enter month #1: 
June
Enter month #2: 
March
Enter month #3: 
April

Now enter the amount of rain (in inches) for each month

June
5.4
March
30
April
21
The average monthly rainfall for June, March and April was 18.8 inches


That does what you want it to do.
yes bro..thank u very much appreciate it..last thing can u please explain me the difference between int,double,float, and char.
char is for storing a single character. An example would be "A".
int is for storing an integer. An example would be "1234".
float is for storing a floating point number. An example would be "2.5".
double is for large floating point numbers.

Read :

http://www.cplusplus.com/doc/tutorial/variables/

if you have anymore questions.
how do i get all my results to have same number of decimal places for example 1.23,3454.56,754.89 and so on
Pages: 12