Page 1 of 3

push_back, square( )ect.ect.

Posted: Thu Mar 19, 2009 10:30 pm
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. :)

Re: push_back, square( )ect.ect.

Posted: Thu Mar 19, 2009 10:41 pm
by MarauderIIC
Relevant line(s) of code?

Re: push_back, square( )ect.ect.

Posted: Fri Mar 20, 2009 9:38 am
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;

Re: push_back, square( )ect.ect.

Posted: Fri Mar 20, 2009 9:39 am
by K-Bal
You should write temps instead of temp in line 7 and try to clean up your code a bit ;)

Re: push_back, square( )ect.ect.

Posted: Fri Mar 20, 2009 12:11 pm
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?

Re: push_back, square( )ect.ect.

Posted: Fri Mar 20, 2009 12:37 pm
by BOMBERMAN
Try this:

Code: Select all

      temps.push_back(temp);
:)

Re: push_back, square( )ect.ect.

Posted: Fri Mar 20, 2009 3:44 pm
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)

Re: push_back, square( )ect.ect.

Posted: Fri Mar 20, 2009 4:30 pm
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 ;)

Re: push_back, square( )ect.ect.

Posted: Fri Mar 20, 2009 5:34 pm
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. ;)

Re: push_back, square( )ect.ect.

Posted: Fri Mar 20, 2009 5:54 pm
by K-Bal
You could try this

Code: Select all

 while((cin>>temp) && (temps.size() < 20))
      temp.push_back(temp);

Re: push_back, square( )ect.ect.

Posted: Fri Mar 20, 2009 7:31 pm
by cplusplusnoob
Thanks man and nice music. ;)

Re: push_back, square( )ect.ect.

Posted: Sat Mar 21, 2009 12:20 am
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);
:)

Re: push_back, square( )ect.ect.

Posted: Sat Mar 21, 2009 5:14 am
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

Re: push_back, square( )ect.ect.

Posted: Tue Mar 24, 2009 10:13 pm
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]

Re: push_back, square( )ect.ect.

Posted: Wed Mar 25, 2009 3:39 am
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];