STL String problems?

Started by
7 comments, last by Zahlman 19 years, 7 months ago

//This program opens a specified file and counts the number
//of whitespace seperated words
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
  ifstream File;
  char *FileLoc;
  string String;
  string WordString;
  vector<string> StrVector;
  
  cout << "Open which file?: ";
  cin >> FileLoc;
  
  File.open(FileLoc);
  
  while(getline(File, String))
  {
    for(int x = 0; x < String.size(); ++x)
    {
      if(String[x] != " ")
      {
        WordString += String[x];
      }
      else
      {
        StrVector.push_back(WordString);
      }    
    }
  }
  
  cout << "The number of words in the file is: "
       << StrVector.size();
  
  return 0;
}       
I'm trying to get the number of words in a file, but on compile I'm told ISO C++ forbids comparison between pointer and integer on line 27. I see no pointers, and the integer tells which array element to use, so I'm at a loss as to what my compiler is complaining about. Thanks for any help.
Advertisement
" " is not the same as ' '
I understand. " " == ' \0'. I'm only grabbing one character. Thanks!
Oops, another problem:
#include <iostream>#include <fstream>#include <string>#include <vector>using namespace std;int main(){  string Line;  string Word;  string Term;  int NumberOfOccurences;  char *FilePath;  ifstream File;  vector<string> StrVector;    cout << "Enter the file to test: ";  cin >> FilePath;  cout << endl << "Enter the string to test for: ";  cin >> Term;  cout << endl;    File.open(FilePath);    while(getline(File, Line))  {    for(int x = 0; x == Line.size(); ++x)    {      if(Line[x] != ' ')        Word += Line[x];      else        StrVector.push_back(Word);    }  }    for(int x = 0; x == StrVector.size(); ++x)  {    if(StrVector[x] == Term)      ++NumberOfOccurences;  }    cout << "The number of occurences of " << Term << " is:"       << endl << NumberOfOccurences;         return 0;}      

It compiles and links, but after I enter the file name, I get a windows error telling me that there was a problem etc. What's wrong with this?
Per your program's objective, though, your algorithm is pretty poor. std::string provides a bunch of find methods (find_first_of, find_first_not_of, find_last_of, find_last_not_of). It also provides a substr method. Used in conjunction, they can really simplify your program. Keep in mind that the find functions take a string and locate occurences of any of the characters in the string, so you can use it for word counts by passing a string like " \n\t\r".
im not sure because i've never used getline, but maybe it should be

x < Line.size();, not x == Line.size();

also, the same for the lower part of the code. now that i am almost sure should be

x < StrVector.size(), not x == StrVector.size();

lastly, i notice you use a char* when you ask for the filename. this isnt needed. use a string. when you want to open the file, you can convert the string to a const char* by just doing

stringname.c_str().
FTA, my 2D futuristic action MMORPG
Quote:Original post by Drakkcon
Oops, another problem:
*** Source Snippet Removed ***
It compiles and links, but after I enter the file name, I get a windows error telling me that there was a problem etc. What's wrong with this?
You're trying to read the file name into an unallocated buffer. Change the type of FileName to std::string, then use its c_str() member if you need a char * to pass to legacy APIs.
@ gravefilla: Thanks, that was the problem, stupid me.
@ oluseyi: I realize that the algorithm is poor. Heck, it's horrible, however the book I'm using as a refresher hasn't introduced the find method (even though I know how to use it), so I'm staying with current knowledge level of the chapter.
Re ' ' vs " " - " " isn't simply two characters (a space and a \0), it's a *pointer to a buffer* containing those characters. Which is why you get a pointer/int conversion error - char is an integral type and char * is a pointer type.

This topic is closed to new replies.

Advertisement