Login Register






Average Rainfall filter_list
Author
Message
Average Rainfall #1
hey guys so for my c++ class we have these programming assignments assigned. My output keeps just being one line with no spaces. any reason why it's doing this?


Final output looks like this:
Enter month:Enter rainfall for:May Enter month: Enter rainfall for:June Enter month:Enter rainfall for:July The average rainfall for May,June, and July is 8.1 inches

^^ why is it so clustered? I'm using a online website my teacher assigned could it be that site?

Code:
#include<iostream> #include<string> #include<iomanip> using namespace std; int main(){ string month1,month2,month3; double rainfall1,rainfall2,rainfall3; cout<<"Enter month:"; cin>>month1; cout<<"Enter rainfall for:"<<month1<<endl; cin>>rainfall1; cout<<"Enter month:"<<endl; cin>>month2; cout<<"Enter rainfall for:"<<month2<<endl; cin>>rainfall2; cout<<"\n"<<"Enter month:"; cin>>month3; cout<<"Enter rainfall for:"<<month3<<endl; cin>>rainfall3; double average = (rainfall1 + rainfall2 + rainfall3)/3; cout<<setprecision(2)<<"The average rainfall for " <<month1 <<","<<month2<<", and "<<month3<<" is " << average<<" inches"<<endl; return 0; }

Reply

RE: Average Rainfall #2
You have to put spaces in the quotes like this:
Code:
#include<iostream> #include<string> #include<iomanip> using namespace std; int main(){ string month1, month2, month3; double rainfall1, rainfall2, rainfall3; cout<<"Enter month: "; cin>>month1; cout<<"Enter rainfall for: "<<month1<<endl; cin>>rainfall1; cout<<"Enter month: "<<endl; cin>>month2; cout<<"Enter rainfall for: "<<month2<<endl; cin>>rainfall2; cout<<"\n"<<"Enter month: "; cin>>month3; cout<<"Enter rainfall for: "<<month3<<endl; cin>>rainfall3; double average = (rainfall1 + rainfall2 + rainfall3)/3; cout<<setprecision(2)<<"The average rainfall for " <<month1 <<","<<month2<<", and "<<month3<<" is " << average<<" inches"<<endl; return 0; }

I learned this in my C++ class a couple of days ago. Personally, I think you should add comments every so often saying what the code does, that is what I do, and it helps my teacher to understand what my programs do.

Also, could you tell me what exactly it is for? I mean like, is it average rainfall per year?

Reply

RE: Average Rainfall #3
oh still not working. The formatting is wrong I'm pretty sure. I'm using an online lab thing codelab.com. so the periods, spaces and everything need to be exact, that's what i think is the problem. So frustrating

Reply

RE: Average Rainfall #4
(09-11-2014, 02:33 AM)Cosh Wrote: oh still not working. The formatting is wrong I'm pretty sure. I'm using an online lab thing codelab.com. so the periods, spaces and everything need to be exact, that's what i think is the problem. So frustrating

I'm not sure then. I use jGRASP in my class, but I went to codelab.com to see if I could figure it out on there, but I cannot access the website. I just told you what my teacher taught me, so it might not work any way, as my teacher does not know much programming herself.

Reply

RE: Average Rainfall #5
Code:
#include<iostream> #include<string> #include<iomanip> using namespace std; int main(){ string month1,month2,month3; double rainfall1,rainfall2,rainfall3; cout<<"Enter month:"; cin>>month1; cout<<endl<<"Enter rainfall for "<<month1<<":"<<endl; cin>>rainfall1; cout<<endl<<"Enter month:"<<endl; cin>>month2; cout<<endl<<"Enter rainfall for "<<month2<<":"<<endl; cin>>rainfall2; cout<<endl<<"Enter month:"; cin>>month3; cout<<endl<<"Enter rainfall for "<<month3<<":"<<endl; cin>>rainfall3; double average = (rainfall1 + rainfall2 + rainfall3)/3; cout<<endl<<setprecision(3)<<"The average rainfall for "<<month1<<", "<<month2<<", and "<<month3 <<" is "<<average<<" inches."<<endl; return 0; }

Fixed and Sovled, thanks for helping

Reply

RE: Average Rainfall #6
No problem, I am happy to help even though I am a newb XD.

Reply

RE: Average Rainfall #7
(09-11-2014, 01:24 AM)ShawnReynolds Wrote: You have to put spaces in the quotes like this:
Code:
#include<iostream> #include<string> #include<iomanip> using namespace std; int main(){ string month1, month2, month3; double rainfall1, rainfall2, rainfall3; cout<<"Enter month: "; cin>>month1; cout<<"Enter rainfall for: "<<month1<<endl; cin>>rainfall1; cout<<"Enter month: "<<endl; cin>>month2; cout<<"Enter rainfall for: "<<month2<<endl; cin>>rainfall2; cout<<"\n"<<"Enter month: "; cin>>month3; cout<<"Enter rainfall for: "<<month3<<endl; cin>>rainfall3; double average = (rainfall1 + rainfall2 + rainfall3)/3; cout<<setprecision(2)<<"The average rainfall for " <<month1 <<","<<month2<<", and "<<month3<<" is " << average<<" inches"<<endl; return 0; }

I learned this in my C++ class a couple of days ago. Personally, I think you should add comments every so often saying what the code does, that is what I do, and it helps my teacher to understand what my programs do.

Also, could you tell me what exactly it is for? I mean like, is it average rainfall per year?

The code and the output he's trying to show should indicate the obvious goal of the code... How would you calculate the yearly average if he only allows you to input data for 3 individual months?

One thing I'm going to bring up here. If your input has spaces in it, cin with the >> operator by default is overloaded to stop at whitespace as a delimiter. The next insertion will grab the junk that was left behind in the previous call. The other thing is that if invalid data is input for the amount of rainfall, cin will remain in an invalid state, and you never check or correct that.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Average Rainfall #8
(09-11-2014, 01:24 AM)ShawnReynolds Wrote: You have to put spaces in the quotes like this:
Code:
#include<iostream> #include<string> #include<iomanip> using namespace std; int main(){ string month1, month2, month3; double rainfall1, rainfall2, rainfall3; cout<<"Enter month: "; cin>>month1; cout<<"Enter rainfall for: "<<month1<<endl; cin>>rainfall1; cout<<"Enter month: "<<endl; cin>>month2; cout<<"Enter rainfall for: "<<month2<<endl; cin>>rainfall2; cout<<"\n"<<"Enter month: "; cin>>month3; cout<<"Enter rainfall for: "<<month3<<endl; cin>>rainfall3; double average = (rainfall1 + rainfall2 + rainfall3)/3; cout<<setprecision(2)<<"The average rainfall for " <<month1 <<","<<month2<<", and "<<month3<<" is " << average<<" inches"<<endl; return 0; }

I learned this in my C++ class a couple of days ago. Personally, I think you should add comments every so often saying what the code does, that is what I do, and it helps my teacher to understand what my programs do.

Also, could you tell me what exactly it is for? I mean like, is it average rainfall per year?

The code and the output he's trying to show should indicate the obvious goal of the code... How would you calculate the yearly average if he only allows you to input data for 3 individual months?

One thing I'm going to bring up here. If your input has spaces in it, cin with the >> operator by default is overloaded to stop at whitespace as a delimiter. The next insertion will grab the junk that was left behind in the previous call. The other thing is that if invalid data is input for the amount of rainfall, cin will remain in an invalid state, and you never check or correct that.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply