What is the problem of my program?

a basic problem which extract each word of a sentence and print it out.
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
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
//using namespace std;
int main() {
std::string s;
std::string word;
std::getline(std::cin,s);
std::vector<string> v;

for(std::string::size_type i=0; i!=s.size(); i++) {
  if(isalpha(s[i])) word+=s[i];
  else {
    if(word!="\0") v.push_back(word);
    word="\0";
  }
}
for(std::vector<string>::size_type i=0; i!=v.size(); i++) {
  std::cout<<v[i];
  if((i+1)%5) std::cout<<"\t";
  else std::cout<<std::endl;
}
std::cout<<std::endl;
return 0;
}

I get error message like this when compiling:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
e3.14.cpp: In function ‘int main()’:
e3.14.cpp:10:13: error: ‘string’ was not declared in this scope
e3.14.cpp:10:19: error: template argument 1 is invalid
e3.14.cpp:10:19: error: template argument 2 is invalid
e3.14.cpp:10:22: error: invalid type in declaration before ‘;’ token
e3.14.cpp:15:22: error: request for member ‘push_back’ in ‘v’, which is of non-class type ‘int’
e3.14.cpp:19:17: error: ‘string’ cannot appear in a constant-expression
e3.14.cpp:19:23: error: template argument 1 is invalid
e3.14.cpp:19:23: error: template argument 2 is invalid
e3.14.cpp:19:36: error: expected initializer before ‘i’
e3.14.cpp:19:41: error: name lookup of ‘i’ changed for ISO ‘for’ scoping
e3.14.cpp:19:41: note: (if you use ‘-fpermissive’ G++ will accept your code)
e3.14.cpp:19:46: error: request for member ‘size’ in ‘v’, which is of non-class type ‘int’
e3.14.cpp:20:17: error: invalid types ‘int[std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size_type]’ for array subscript


if I change the comment of
 
//using namespace std; 


then it works. can any body tell me how to fix this if I don't want to use namespace. thanks!
closed account (zb0S216C)
During the declaration of the std::vector on line 19, string needs to be prefixed with std::.

Wazzak
you mean like this:?

1
2
3

std::vector<std::string>::size_type?


why line 19?

how about line 10?
closed account (zb0S216C)
Sorry, I missed line 10. Yeah, prefix string on line 10 with std:: as well.

Wazzak
thanks!
Topic archived. No new replies allowed.