Reputation: 73
I would like to read a file and put the number of times the word appears in a std::map. So I made the following code:
#include <fstream>
#include <string>
#include <map>
int main() {
std::map<std::string, unsigned> M;
std::ifstream declaration("declaration.txt");
while(declaration >> std::string s){
M[s]=0;
}
while(declaration >> std::string s){
M[s]=M[s]+1;
}
declaration.close();
return 0;
}
And I'm getting the following error messages:
text_analyse.cpp: In function ‘int main()’:
text_analyse.cpp:8:35: error: expected primary-expression before ‘s’
while(declaration >> std::string s){
^
text_analyse.cpp:8:34: error: expected ‘)’ before ‘s’
while(declaration >> std::string s){
~ ^~
)
text_analyse.cpp:8:35: error: ‘s’ was not declared in this scope
while(declaration >> std::string s){
^
text_analyse.cpp:11:35: error: expected primary-expression before ‘s’
while(declaration >> std::string s){
^
text_analyse.cpp:11:34: error: expected ‘)’ before ‘s’
while(declaration >> std::string s){
~ ^~
)
text_analyse.cpp:11:35: error: ‘s’ was not declared in this scope
while(declaration >> std::string s){
^
Upvotes: 0
Views: 644
Reputation: 31183
std::string s
is not a valid expression, it is a statement.
Declare the string outside the loop instead:
std::string s;
while(declaration >> s){
M[s]=0;
}
Upvotes: 1