Jump to content
  • Advertisement
Sign in to follow this  
Drakkcon

STL String problems?

This topic is 5141 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

//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.

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
@ 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!