Simple C++ question

Discussion in 'OT Technology' started by Rick, Nov 17, 2003.

  1. Rick

    Rick New Member

    Joined:
    Feb 20, 2002
    Messages:
    1,735
    Likes Received:
    0
    Location:
    Henderson, NV
    For some reason when I run it, it counts the last word in the file 2 times. Where am I going wrong?

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    string formatword(string);
    
    int main()
    {
    
      ifstream infile;  // input filestream
      ofstream outfile; // output filestream
    
      infile.open("test"); // opens input file
      
      int wordcount = 0; // counts the words as they are read
      string word; // variable to read each word
    
      infile >> word;
      word = formatword(word);
      cout << word << endl;
      wordcount++;
      
      while(!infile.eof())
        {
          infile >> word;
          word = formatword(word);
          cout << word << endl;      
          wordcount++;
        }
    
      infile.close();
    
      cout << "There are " << wordcount << " words in the file." << endl;
    
    return 0;
    }
    
    string formatword(string word)
    {
      int len;
      len = word.length();
      if(len > 0)
        {
          word[0] = toupper(word[0]);
          for(int i = 1; i < len; i++)
    	word[i] = tolower(word[i]);
        } 
      return word;
    }
     
    Last edited: Nov 17, 2003
  2. fatlip

    fatlip Transcendence Into The Peripheral

    Joined:
    Jun 16, 2001
    Messages:
    522
    Likes Received:
    0
    Location:
    UK
    I'm no C++ master and I don't know if this will solve your problem, but you should get rid of the first

    Code:
    infile >> word;
    formatword(word);
    wordcount++;
    
    bit and let the while loop do all the file reading. At the moment your program will probably throw an error or not work properly if the data file is empty.
     
  3. Rick

    Rick New Member

    Joined:
    Feb 20, 2002
    Messages:
    1,735
    Likes Received:
    0
    Location:
    Henderson, NV
    Doesn't work and I don't need to worry on this program if the data file is empty or not.
     
  4. Reign

    Reign Banned

    Joined:
    Nov 17, 2003
    Messages:
    21,304
    Likes Received:
    0
    Location:
    Westminster, CO
    I think if you take out wordcount++; before the while loop, that should fix it. I THINK that's what's doing it. Because if you're incrementing wordcount before you enter the loop, it's setting wordcount to 1 instead of 0, which would "count the last word" twice.
     
  5. Rick

    Rick New Member

    Joined:
    Feb 20, 2002
    Messages:
    1,735
    Likes Received:
    0
    Location:
    Henderson, NV
    The problem is that it is counting and reading the last word twice. Removing the wordcount++ before the while loop doesn't solve it reading the last word twice.
     
  6. Rick

    Rick New Member

    Joined:
    Feb 20, 2002
    Messages:
    1,735
    Likes Received:
    0
    Location:
    Henderson, NV
    Ah nevermind, fixed the problem. Simple mistake.
     
  7. Reign

    Reign Banned

    Joined:
    Nov 17, 2003
    Messages:
    21,304
    Likes Received:
    0
    Location:
    Westminster, CO
    What was the problem?
     
  8. StevesVR4

    StevesVR4 Get Arrested

    Joined:
    Jul 1, 2003
    Messages:
    7,328
    Likes Received:
    0
    In this situation, a while loop is not the best choice in my opinion. You want the program to execute the code in the loop one or more times. That would call for a do...while loop, they are designed for this situation. C++ has a coupe of different types of loop statements and you should know which loop to use in which situation.
     
  9. Rick

    Rick New Member

    Joined:
    Feb 20, 2002
    Messages:
    1,735
    Likes Received:
    0
    Location:
    Henderson, NV
    Code:
    infile >> word;
      word = formatword(word);
      cout << word << endl;
      wordcount++;
      
      while(!infile.eof())
        {
          infile >> word;
          word = formatword(word);
          cout << word << endl;      
          wordcount++;
        }
    Changed that to this:

    Code:
    infile >> word;
      
      while(!infile.eof())
        {
          word = formatword(word);
          cout << word << endl;      
          wordcount++;
          infile >> word;    
        }
     

Share This Page