Vector won't work

I am going through the book Programming principles and practice using C++ by Bjarne Stoustrup. I cannot get this program to build.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "stdafx.h"
#include <vector>
using namespace std;


int main()
{
	vector<double> temps;
	double temp;

	while (cin >> temp)
		temps.push_back(temp);

	double sum = 0;
	
	for (int i = 0; i<temps.size(); ++i){
		sum += temps[i];
	}

	cout << "Average temperature: " << sum / temps.size() << endl;

	sort(temps.begin(),temps.end());

	cout << "Median temperature: " << temps{temps.size()/2} << endl;


	return 0;
}


This is the error I get from Visual C++ 2008 Express edition. It chokes on the cin expression in the while loop. Any suggestions would be appreciated.

1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>Compiling...
1>test.cpp
1>c:\programming\test\test\test.cpp(11) : error C2065: 'cin' : undeclared identifier
1>c:\programming\test\test\test.cpp(11) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://c:\programming\test\test\Debug\BuildLog.htm"
1>test - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
You need to include iostream if you want to use cin and cout
I tried that and I must have done something even more wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "stdafx.h"
#include<vector>
#include<iostream>
using namespace std;


int main()
{
	vector<double> temps;
	double temp;

	while (cin >> temp)
		temps.push_back(temp);

	double sum = 0;
	
	for (int i = 0; i<temps.size(); ++i){
		sum += temps[i];
	}

	cout << "Average temperature: " << sum / temps.size() << endl;

	sort(temps.begin(),temps.end());

	cout << "Median temperature: " << temps{temps.size()/2} << endl;


	return 0;
}


1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>Compiling...
1>test.cpp
1>c:\programming\test\test\test.cpp(17) : warning C4018: '<' : signed/unsigned mismatch
1>c:\programming\test\test\test.cpp(23) : error C3861: 'sort': identifier not found
1>c:\programming\test\test\test.cpp(25) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)
1> with
1> [
1> _Ty=double
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(700): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(738): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(785): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(909): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(916): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(923): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(930): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(170): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(176): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(183): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(190): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(210): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(243): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(263): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 int)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(288): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned int)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(308): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(328): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned long)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(349): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]

1>Build log was saved at "file://c:\programming\test\test\Debug\BuildLog.htm"
1>test - 5 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
For sort you need <algorithm>
and this line is wrong:
cout << "Median temperature: " << temps{temps.size()/2} << endl (braces)
Last edited on
Thanks for your help!

That resolved most of the issues.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;


int main()
{
	vector<double> temps;
	double temp;

	while (cin>>temp)
		temps.push_back(temp);

	double sum = 0;
	
	for (int i=0; i<temps.size(); ++i){
		sum += temps[i];
	}

	cout << "Average temperature: " << sum/temps.size() << endl;

	sort(temps.begin(),temps.end());

	cout << "Median temperature: " << temps(temps.size()/2) << endl;


	return 0;
}


It seems to not like

for (int i=0; i<temps.size(); ++i){


1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>Compiling...
1>test.cpp
1>c:\programming\test\test\test.cpp(18) : warning C4018: '<' : signed/unsigned mismatch
1>c:\programming\test\test\test.cpp(26) : error C2064: term does not evaluate to a function taking 1 arguments
1>Build log was saved at "file://c:\programming\test\test\Debug\BuildLog.htm"
1>test - 1 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Brackets on line 26 are still wrong. http://www.cplusplus.com/reference/stl/vector/operator%5B%5D/
I guess you can figure out what the warning is about on your own
I've never had any problems with that, but I'm always on Linux machines using GCC. I suspect that std::vector::size() returns a size_t (probably a typedef for unsigned int) and the compiler wants you to use an unsigned index...

Try changing 'int' to 'size_t'.
Wow that was it man! Thanks so much! I am going to start using Linux to learn C++ since it is installed on my laptop. But I am at work now so I don't have access. I really appreciate your time and the creator of this website.

Oh just for anyone who may have this problem in the future. This compiles in Visual C++ 2008.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;


int main()
{
	vector<double> temps;
	double temp;

	while (cin>>temp)
		temps.push_back(temp);

	double sum = 0;
	
	for (size_t i=0; i<temps.size(); ++i){
		sum += temps[i];
	}

	cout << "Average temperature: " << sum / temps.size() << endl;

	sort(temps.begin(),temps.end());

	cout << "Median temperature: " << temps[temps.size()/2] << endl;


	return 0;
}

Topic archived. No new replies allowed.