push_back, square( )ect.ect.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

push_back, square( )ect.ect.

Post by cplusplusnoob »

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. :)
I'm not modest, I'm brutally honest.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: push_back, square( )ect.ect.

Post by MarauderIIC »

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.
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: push_back, square( )ect.ect.

Post by cplusplusnoob »

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.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: push_back, square( )ect.ect.

Post by K-Bal »

You should write temps instead of temp in line 7 and try to clean up your code a bit ;)
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: push_back, square( )ect.ect.

Post by cplusplusnoob »

K-Bal wrote:You should write temps instead of temp in line 7 and try to clean up your code a bit ;)
witch one?
I'm not modest, I'm brutally honest.
User avatar
BOMBERMAN
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Wed Jan 14, 2009 5:00 pm

Re: push_back, square( )ect.ect.

Post by BOMBERMAN »

Try this:

Code: Select all

      temps.push_back(temp);
:)
My english sucks!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: push_back, square( )ect.ect.

Post by MarauderIIC »

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)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: push_back, square( )ect.ect.

Post by K-Bal »

cplusplusnoob wrote:
K-Bal wrote:You should write temps instead of temp in line 7 and try to clean up your code a bit ;)
witch one?
20 seconds of try and error ;)
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: push_back, square( )ect.ect.

Post by cplusplusnoob »

K-Bal wrote:
cplusplusnoob wrote:
K-Bal wrote:You should write temps instead of temp in line 7 and try to clean up your code a bit ;)
witch one?
20 seconds of try and error ;)
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. ;)
I'm not modest, I'm brutally honest.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: push_back, square( )ect.ect.

Post by K-Bal »

You could try this

Code: Select all

 while((cin>>temp) && (temps.size() < 20))
      temp.push_back(temp);
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: push_back, square( )ect.ect.

Post by cplusplusnoob »

Thanks man and nice music. ;)
I'm not modest, I'm brutally honest.
User avatar
wtetzner
Chaos Rift Regular
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.

Post by wtetzner »

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.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: push_back, square( )ect.ect.

Post by K-Bal »

cplusplusnoob wrote:Thanks man and nice music. ;)
Thanks ;)
wtetzner 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);
:)
I feel so ashamed :D:D
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: push_back, square( )ect.ect.

Post by cplusplusnoob »

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.
User avatar
kostiak2
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 74
Joined: Tue Mar 24, 2009 4:08 pm

Re: push_back, square( )ect.ect.

Post by kostiak2 »

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;
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:

Code: Select all

for (unsigned int i=0;i<temps.size();++i)sum+= temps[i];
Post Reply