I have a Q for computer programmers. C++
Sep 17, 2005 at 7:38 PM Thread Starter Post #1 of 10

apnk

Headphoneus Supremus
Joined
May 20, 2004
Posts
3,596
Likes
11
Any computer programmers here? C++ language. I have a question. Dont laugh if its dumb, i am just a beginner
icon10.gif
.

Ok for some reason when i run this program the second CIN (its in bold) is skipped, it doesn't ask me to enter the amount of rainfall, it goes straight to the average.

Tell me whats wrong!!! Thanks!
__________________________________________________ ___________
#include <iostream>
using namespace std;

int main ()
{
int mth1, mth2, mth3; // months
float rain1, rain2, rain3; // rainfall
float average;

cout << "Enter the three months, in order to find an average: ";
cin >> mth1, mth2, mth3;


cout << "\nNow enter the rainfall for each month: \n";
cin >> rain1, rain2, rain3;
average = (rain1 + rain2 + rain3) / 3;



cout << "\n\nThe average rainfall is" << average << endl;



return 0;

}
 
Sep 17, 2005 at 8:35 PM Post #2 of 10
Try this:
#include <iostream>
using namespace std;

int main ()
{
int mth1, mth2, mth3; // months
float rain1, rain2, rain3; // rainfall
float average;

cout << "Enter the three months, in order to find an average: ";
cin >> mth1 >> mth2 >> mth3;


cout << "\nNow enter the rainfall for each month: ";
cin >> rain1 >> rain2 >> rain3;
average = (rain1 + rain2 + rain3) / 3;



cout << "\n\nThe average rainfall is " << average << endl;



return 0;

}

Use the >> or << to separate each element in a cout or cin stream.
 
Sep 18, 2005 at 2:12 AM Post #4 of 10
Quote:

Originally Posted by apnk
Thanks Born2bwire, I figured out the problem I was using integers for months, and months aren't numbers they are charactors do char not int.
smily_headphones1.gif



Certainly would also be another problem. But when I compiled and ran the batch (via Visual C++ 6.0) I had the skipping problem too. It wasn't until I added the >> in there that it worked.
 
Sep 18, 2005 at 2:18 AM Post #5 of 10
It has been a few few years since I have touched these kind of things with C++, but I think when using the ',' with cin and cout the the data is stored in a buffer that needs to be manually cleared. I am going to guess the input for mth was applied to rain.

Can anyone more up to date on C++ confirm this? (I am just trying to see how much I remember)
 
Sep 18, 2005 at 2:38 AM Post #6 of 10
Quote:

Originally Posted by Born2bwire
Certainly would also be another problem. But when I compiled and ran the batch (via Virtual C++ 6.0) I had the skipping problem too. It wasn't until I added the >> in there that it worked.


Virtual? Dont you mean Visual? Well anyway I have Visual C++ 6.0 and it ran fine with the commas. I'll try the << though.


BTW in my program I ask for the months because i was going to include the months names in the last cout. I have not done yet.
 
Sep 18, 2005 at 4:00 PM Post #7 of 10
Quote:

Originally Posted by apnk
Virtual? Dont you mean Visual? Well anyway I have Visual C++ 6.0 and it ran fine with the commas. I'll try the << though.


BTW in my program I ask for the months because i was going to include the months names in the last cout. I have not done yet.



Virtual???? What are you talking about? /me looks at post. I didn't say that...
rolleyes.gif
 
Sep 18, 2005 at 5:24 PM Post #8 of 10
Quote:

Originally Posted by Born2bwire
Virtual???? What are you talking about? /me looks at post. I didn't say that...
rolleyes.gif



Hmmm, must have been one of those sneaky little moderators. I understand they spend a lot of time editing posts silently!
tongue.gif
 
Sep 18, 2005 at 5:42 PM Post #10 of 10
Quote:

Originally Posted by apnk
Virtual? Dont you mean Visual? Well anyway I have Visual C++ 6.0 and it ran fine with the commas. I'll try the << though.


Are you serious? That actually compiled with the commas? Wow, I'm going to go and slam that into my compiler and see what happens. I'm using .NET 2003, and I'm interested as to what happens!
biggrin.gif



*EDIT*

Yea, so I ran this through .NET and it DID compile and link. However I did get 2 warnings for variable 'rain2' and 'rain3' being used without having been initialized. So you do need the operators, otherwise you will most likely have a really large negative value for rain2 and rain3 and your average will be bad!
smily_headphones1.gif
 

Users who are viewing this thread

Back
Top