0

I wrote a C++ program using codeblocks and at the last minute I decided to use empress, the server at school that we use to do our labs, and turns out that it did not work! What does that mean? Is my program not right? Or could it be a compiler issue? I normally use linux ubuntu using codeblocks to do my programming. I tested the program using windows and it also worked. Why doesn't it run on the server?

Here is the code that I think causes the problem:

bool dictionary::insertWordsIntoDict(string fileName)
{

   ifstream inp;
   string word;
   vector<string> vec;
   inp.open(fileName.data());

   if(inp.good())
   {

     while(!inp.eof())
     {
      inp>>word;
      vec.push_back(word);

     }
    string temp;
    string temp2= "#.txt";

     for(int i=0 ; i<vec.size() ; i++)
     {
          temp = vec[i];
          temp2[0] = tolower(temp[0]);
          cout<<temp<<endl;
          AddWord(temp.data(), temp2);
     }

   }//end of if statement

  else
  {
     cout<<":(  File does not exist! "<<endl;
     return failure;
  }

}// end of function insert words
Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
user2171775
  • 317
  • 1
  • 2
  • 9
  • 3
    Hi, what is the exact error message? I am not reading all your code :) – Patashu Mar 15 '13 at 00:55
  • nothing, it just runs the drive function and then it gets stuck. and does not do what is supposed to. – user2171775 Mar 15 '13 at 00:58
  • well, i erased the whole thing, this is the function the i think im having problems with, – user2171775 Mar 15 '13 at 01:05
  • 4
    You have to state what the problem is for us to help you. Saying that it "does not do what it's supposed to" is not helpful when you haven't explained what it's supposed to do. Please say what the expected behaviour is and what the actual behaviour is – HXCaine Mar 15 '13 at 01:13
  • Could it be missing or incompatible runtime libraries? The server which you test on may not have the right runtime components to run your program properly – ksming Mar 15 '13 at 01:13
  • "at the last minute I decided to use the server at school that we use to do our labs" - never a good idea. If your program gets stuck in a function, you should add some debugging output into said function to see where it hangs and what the values of the relevant variables are at that point. – millimoose Mar 15 '13 at 01:15
  • When you want help, post a small self-contained example and explain what you want it to do, and then tell us what it does instead and how that isn't do what you want. – Omnifarious Mar 15 '13 at 01:27

2 Answers2

2

while(!inp.eof()) is not a good way to read from a file. In particular, if it cannot read for some reason other than EOF, the condition will never be false, and your loop will run forever.

The correct way to write this kind of loop is:

while(inp >> word)
 {
  vec.push_back(word);
 }

Here, inp >> word will evaluate to false if word could not be read from the input stream for any reason.

I can't be sure this is your problem without more details, but it can't hurt.

Tyler McHenry
  • 74,820
  • 18
  • 121
  • 166
1

Well there is at least one issue, you are using eof in your loop condition, you should modify like so:

while( inp >> word)
 {
  vec.push_back(word);

 }

This previous thread covers why Why is iostream::eof inside a loop condition considered wrong?.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740