Errors On Code From Book (vector question), Question On Its Output

I am reading Stroustrup's Edition 3 C++ book.  I am trying to get this code to compile, because I do not understand how the output can be what it is...  I was wondering if anyone can help me out getting this to compile.  I am sure I do not have the proper include files included, and I am working under Microsoft Visual C++ 5.0.

According to the book if the input was
aa bb bb aa aa bb aa aa

The output should be
aa: 5
bb: 3

I have no idea how this could be...I would figure it would display each element of vector's name and what the value of val would be..  Any help would be appreciated..

Below Is Code And Errors..

#include <iostream.h>
#include <string>
#include <vector>

using namespace std;

struct Pair {
 string name;
 double val;
};

vec<Pair>pairs;

double& value(const string& s);


int main() // count the occurence of each word on input

{
 string buf;

 while (cin>>buf) value(buf)++;
 for (vector<Pair>::const_iterator p = pairs.begin(),p!=pairs.end(), ++p)
    cout << p->name << " : " << p->val << '\n';

 return 0;

}


double& value(const string& s)
/*
 Maintain a set of pairs:
 search for s, return its value if found; otherwise
 make a new pair and return the default value 0
*/

{
 for (int i=0; i < pairs.size(); i++)
  if (s==pairs[i].name) return pairs[i].val;

  Pair p={s,0};
  pairs.push_back(p);  // add pair at end
  return pairs[pairs.size()-1].val;

}


Errors...

--------------------Configuration: vector - Win32 Debug--------------------
Compiling...
vector.cpp
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(16) : error C2501: 'vec' : missing decl-specifiers
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(16) : error C2239: unexpected token '<' following declaration of 'vec'
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(16) : error C2059: syntax error : '<'
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(26) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>>' (or there
is no acceptable conversion)
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(28) : error C2065: 'pairs' : undeclared identifier
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(28) : error C2228: left of '.begin' must have class/struct/union type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(28) : error C2086: 'p' : redefinition
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(28) : error C2143: syntax error : missing ';' before '!='
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(28) : error C2143: syntax error : missing ';' before '!='
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(28) : error C2143: syntax error : missing ')' before '!='
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(28) : error C2143: syntax error : missing ';' before '!='
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(44) : error C2228: left of '.size' must have class/struct/union type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(45) : error C2109: subscript requires array or pointer type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(45) : error C2228: left of '.name' must have class/struct/union type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(45) : error C2109: subscript requires array or pointer type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(45) : error C2228: left of '.val' must have class/struct/union type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(47) : error C2552: 'p' : non-aggregates cannot be initialized with initializer list
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(48) : error C2228: left of '.push_back' must have class/struct/union type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(50) : error C2228: left of '.size' must have class/struct/union type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(50) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

vector.exe - 20 error(s), 0 warning(s)
larockdAsked:
Who is Participating?
 
chensuConnect With a Mentor Commented:
The following is the correct one.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct Pair {
 string name;
 double val;
};

vector<Pair>pairs;

double& value(const string& s);


int main() // count the occurence of each word on input

{
 string buf;

 while (cin>>buf) value(buf)++;
 for (vector<Pair>::const_iterator p = pairs.begin();p!=pairs.end(); ++p)
    cout << p->name << " : " << p->val << '\n';

 return 0;

}


double& value(const string& s)
/*
 Maintain a set of pairs:
 search for s, return its value if found; otherwise
 make a new pair and return the default value 0
*/

{
 for (int i=0; i < pairs.size(); i++)
  if (s==pairs[i].name) return pairs[i].val;

  Pair p;
  p.name = s;
  p.val = 0;
  pairs.push_back(p);  // add pair at end
  return pairs[pairs.size()-1].val;

}

0
 
larockdAuthor Commented:
Chensu,
Thanks For your help...I pasted your code into the compiler and below is the errors from the compile..   Is there something I have to change in Visual C++ to get this to compile.


--------------------Configuration: vector - Win32 Debug--------------------
Compiling...
vector.cpp
F:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2784: 'bool __cdecl std::operator <(const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &,const template-parameter-1 *)' : could not deduce t
emplate argument for 'const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2784: 'bool __cdecl std::operator <(const template-parameter-1 *,const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &)' : could not deduce t
emplate argument for 'const template-parameter-1 *' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2784: 'bool __cdecl std::operator <(const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &,const class std::basic_string<`template-parameter-1
',`template-parameter-2',`template-parameter-3'> &)' : could not deduce template argument for 'const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2784: 'bool __cdecl std::operator <(const class std::reverse_iterator<`template-parameter-1',`template-parameter-2',`template-parameter-3',`template-parameter-4',`template-parameter-5'> &,c
onst class std::reverse_iterator<`template-parameter-1',`template-parameter-2',`template-parameter-3',`template-parameter-4',`template-parameter-5'> &)' : could not deduce template argument for 'const class std::reverse_iterator<`template-parameter-
1',`template-parameter-2',`template-parameter-3',`template-parameter-4',`template-parameter-5'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2784: 'bool __cdecl std::operator <(const struct std::pair<`template-parameter-1',`template-parameter-2'> &,const struct std::pair<`template-parameter-1',`template-parameter-2'> &)' : could
 not deduce template argument for 'const struct std::pair<`template-parameter-1',`template-parameter-2'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2676: binary '<' : 'const struct Pair' does not define this operator or a conversion to a type acceptable to the predefined operator
F:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2784: 'bool __cdecl std::operator <(const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &,const template-parameter-1 *)' : could not deduce t
emplate argument for 'const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2784: 'bool __cdecl std::operator <(const template-parameter-1 *,const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &)' : could not deduce t
emplate argument for 'const template-parameter-1 *' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2784: 'bool __cdecl std::operator <(const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &,const class std::basic_string<`template-parameter-1
',`template-parameter-2',`template-parameter-3'> &)' : could not deduce template argument for 'const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2784: 'bool __cdecl std::operator <(const class std::reverse_iterator<`template-parameter-1',`template-parameter-2',`template-parameter-3',`template-parameter-4',`template-parameter-5'> &,c
onst class std::reverse_iterator<`template-parameter-1',`template-parameter-2',`template-parameter-3',`template-parameter-4',`template-parameter-5'> &)' : could not deduce template argument for 'const class std::reverse_iterator<`template-parameter-
1',`template-parameter-2',`template-parameter-3',`template-parameter-4',`template-parameter-5'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2784: 'bool __cdecl std::operator <(const struct std::pair<`template-parameter-1',`template-parameter-2'> &,const struct std::pair<`template-parameter-1',`template-parameter-2'> &)' : could
 not deduce template argument for 'const struct std::pair<`template-parameter-1',`template-parameter-2'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2676: binary '<' : 'const struct Pair' does not define this operator or a conversion to a type acceptable to the predefined operator
Error executing cl.exe.

vector.exe - 12 error(s), 0 warning(s)

0
 
q2guoCommented:
try this larockd, I had the same problem just a few days ago.

#include <iostream.h>
#include <string>
#include <vector>

struct Pair {
     string name;
     double val;
};

std::vector<Pair>pairs;

.....
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
larockdAuthor Commented:
I gave it a try, but It did not help.  Same errors..  I beleive even though I had it declared as
vector<Pair>pairs; it was actually std::vector<Pair>pairs; because I had using namespace std; defined at the top.

Thanks Again
0
 
chensuCommented:
Follow these steps,

1. Create a new Win32 Console Application.
2. Copy and paste the above code I gave you, name it test.cpp.
3. Add the test.cpp to the project.
4. Build it.

If the compiling errors still come out, the only question is whether you have installed Visual Studio Service Pack 1, 2 or 3?
0
 
larockdAuthor Commented:
Mr. Chensu,
I did as you directed and still have the same compiling errors.  As you have indicated I do not have any of the service packs.  I am on my way right now to start downloading them to install them..

Should this fix my problem?

0
 
larockdAuthor Commented:
I just checked out microsofts site, and like you indicated I found a KB article that referenced this error and the fix is in service pack one.  I am now downloading it.


0
 
larockdAuthor Commented:
The code compiles with the installation of the service pack..  I do have a question about the code..

This line here

 while (cin>>buf) value(buf)++;

is an infinite input loop to me, how do you break out of it for normal program operation?

Thanks For your help and good call on the problem with it being the service pack.


0
 
chensuCommented:
Press Ctrl-Z, which produces End-of-File (EOF).
0
 
ngcollinCommented:
cin>> will return a reference to the stream. So

while(cin>>buffer)

Checks if there is more input in the stream.

As you are using Visual C++ 5 like me, I assume you are not on a unix platform, therefore pressing Ctrl-Z won't give you an EOF and thus will not be able to stop the loop.

To test this program in dos, you could run this program by piping a text stream to the program, eg:

echo testing aa bb bb aa aa bb aa aa | yourprog.exe

This way, the test for the end of stream will work and your program can terminate.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.