push_back, square( )ect.ect.
Moderator: Coders of Rage
- cplusplusnoob
- Chaos Rift Newbie
- Posts: 33
- Joined: Sun Mar 08, 2009 2:40 pm
- Location: Iowa city, Iowa
push_back, square( )ect.ect.
I have been doing the programs as best I can as my book tells me to and some things just did'nt work.It started with square witch would square a number but i can do that manually without too much trouble so I did.Now it's to the point where i can't do anything else.Currently learning about vectors I learned the push_back operator but when I try to compile the program it says
'push_back' : is not a member of 'System::Double'
1> c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Double'
My compiler is microft visual c++ express edition if that makes any difference and thank you all for your time and kindness.
'push_back' : is not a member of 'System::Double'
1> c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Double'
My compiler is microft visual c++ express edition if that makes any difference and thank you all for your time and kindness.
I'm not modest, I'm brutally honest.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: push_back, square( )ect.ect.
Relevant line(s) of code?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- cplusplusnoob
- Chaos Rift Newbie
- Posts: 33
- Joined: Sun Mar 08, 2009 2:40 pm
- Location: Iowa city, Iowa
Re: push_back, square( )ect.ect.
Code: Select all
using namespace std;
int main()
{
vector<double>temps;
double temp;
while(cin>>temp)
temp.push_back(temp);
double sum=0;
for (int i=0; i<temps.size();++i)sum+= temps[i];
cout<<"average temurature: "<<sum/temps.size()<<endl;
sort(temps.begin(),temps.end());
cout<<"medean temurature: "<<temps[temps.size()/2]<<endl;
I'm not modest, I'm brutally honest.
Re: push_back, square( )ect.ect.
You should write temps instead of temp in line 7 and try to clean up your code a bit
- cplusplusnoob
- Chaos Rift Newbie
- Posts: 33
- Joined: Sun Mar 08, 2009 2:40 pm
- Location: Iowa city, Iowa
Re: push_back, square( )ect.ect.
witch one?K-Bal wrote:You should write temps instead of temp in line 7 and try to clean up your code a bit
I'm not modest, I'm brutally honest.
Re: push_back, square( )ect.ect.
Try this:
Code: Select all
temps.push_back(temp);
My english sucks!
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: push_back, square( )ect.ect.
Yes, "temp" is a double. "temps" is a vector. You are trying to use push_back on your double, not on your vector (which is wrong).
ie you have
temp.push_back(temp)
needs to be
temps.push_back(temp)
ie you have
temp.push_back(temp)
needs to be
temps.push_back(temp)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: push_back, square( )ect.ect.
20 seconds of try and errorcplusplusnoob wrote:witch one?K-Bal wrote:You should write temps instead of temp in line 7 and try to clean up your code a bit
- cplusplusnoob
- Chaos Rift Newbie
- Posts: 33
- Joined: Sun Mar 08, 2009 2:40 pm
- Location: Iowa city, Iowa
Re: push_back, square( )ect.ect.
You are right.That was just plain laze and it shant happen again, however, this program does work but I don't know how to make it understand I want the medean and not to enter more data.Thanks to everyone your help is appreciated immencely.K-Bal wrote:20 seconds of try and errorcplusplusnoob wrote:witch one?K-Bal wrote:You should write temps instead of temp in line 7 and try to clean up your code a bit
I'm not modest, I'm brutally honest.
Re: push_back, square( )ect.ect.
You could try this
Code: Select all
while((cin>>temp) && (temps.size() < 20))
temp.push_back(temp);
- cplusplusnoob
- Chaos Rift Newbie
- Posts: 33
- Joined: Sun Mar 08, 2009 2:40 pm
- Location: Iowa city, Iowa
- wtetzner
- Chaos Rift Regular
- Posts: 159
- Joined: Wed Feb 18, 2009 6:43 pm
- Current Project: waterbear, GBA game + editor
- Favorite Gaming Platforms: Game Boy Advance
- Programming Language of Choice: OCaml
- Location: TX
- Contact:
Re: push_back, square( )ect.ect.
K-Bal wrote:You could try this
Code: Select all
while((cin>>temp) && (temps.size() < 20)) temp.push_back(temp);
Code: Select all
while((cin>>temp) && (temps.size() < 20))
temps.push_back(temp);
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
Re: push_back, square( )ect.ect.
Thankscplusplusnoob wrote:Thanks man and nice music.
I feel so ashamed :D:Dwtetzner wrote:K-Bal wrote:You could try this
Code: Select all
while((cin>>temp) && (temps.size() < 20)) temp.push_back(temp);
Code: Select all
while((cin>>temp) && (temps.size() < 20)) temps.push_back(temp);
- cplusplusnoob
- Chaos Rift Newbie
- Posts: 33
- Joined: Sun Mar 08, 2009 2:40 pm
- Location: Iowa city, Iowa
Re: push_back, square( )ect.ect.
I am now trying, as per the instructions of my book, to add some doubles int a vector and make it output witch is the smallert largest and all of the numbers put together. I started with just the smallest part to keep it simple.Your program had a warning but the program seemed to work fine so I didn't mind it but now near the end it will say that debug assertion failed. Here is the warning and then the code in witch the warning is along with the line before and after.
Code: Select all
1>.\meadean program.cpp(15) : warning C4018: '<' : signed/unsigned mismatch
Code: Select all
[double sum=0;
for (int i=0;i<temps.size();++i)sum+= temps[i];
cout<<"average temurature: "<<sum/temps.size()<<endl;/code]
I'm not modest, I'm brutally honest.
Re: push_back, square( )ect.ect.
you are getting the warning because size() returns an "unsigned int", which means it can't be minus (which makes sense, since you can't have a size of -54). You can simply change your 2nd line of code to:cplusplusnoob wrote: Here is the warning and then the code in witch the warning is along with the line before and after.
Code: Select all
1>.\meadean program.cpp(15) : warning C4018: '<' : signed/unsigned mismatch
Code: Select all
double sum=0; for (int i=0;i<temps.size();++i)sum+= temps[i]; cout<<"average temurature: "<<sum/temps.size()<<endl;
Code: Select all
for (unsigned int i=0;i<temps.size();++i)sum+= temps[i];