Thread: some help on a disobedient function

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    13

    some help on a disobedient function

    ey all, i was wondering if you can all help me with this program. what it does it counts the number of letters in a file, in this case info.dat, and also counts the number of characters used, regardless of whether it is numeric, alpha, spaces, etc.

    purpose of this is so that i can subtract the 2 and get the number of words. there might be other ways of doing it -- but this is how i approached the problem, and it suffices for now. but the problem is that i can not get a proper amount of letters to display. when using the loop inside main, i do get the right amount of letters. so i'm assuming i'm not bringing in the fstream variable in correctly into the function. if you guys can help, i'd appreciate it.

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <cctype>
    using namespace std;
    
    int intfuncCharacters (ifstream ifsFileIn_par);
    int intfuncLetters (ifstream ifsFileIn_par);
    
    
    void main()
    
    {
    
     ifstream ifsFileIn;
     int intLetters = 0, intCharacters= 0;
     char chrCharacter;
    
     intCharacters = intfuncCharacters(ifsFileIn);
     intLetters = intfuncLetters(ifsFileIn);
    
    
     cout << intCharacters;
    
    
    
    }
    
    
    
    
    
    int intfuncLetters (ifstream ifsFileIn_par)
    
    {
     
     ifsFileIn_par.open("info.dat");
     char chrCharacter;
     int intDummyLetters;
     while(ifsFileIn_par.get(chrCharacter))
      {
       if (isalpha(chrCharacter))
    
       intDummyLetters ++;
      }
     
     ifsFileIn_par.close ();
     return (intDummyLetters);
    
    }
    
    
    
    
    
    
    int intfuncCharacters (ifstream ifsFileIn_par)
    
    {
     char chrCharacter;
     int intDummyLetters;
     while(ifsFileIn_par.get(chrCharacter))
      {
       intDummyLetters ++;
      }
    
     return (intDummyLetters);
    
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For one thing, my compiler doesn't let me pass ifstream to a function by value. I guess the streams are generally non-copiable, and you should pass it by reference (ifstream&).

    Secondly, intfuncCharacters doesn't open a file, so where would it read from? (You might also open the file just once in main with the constructor.)

    Thirdly, the counters in the functions are not initialized (they start at garbade value).

    Lastly, you'll need to clear the eof flag, before you can use the stream to read the file again, as well as move the file pointer back to the beginning of the file. (clear, seekg)

    By the way, is it guaranteed that each "word" is separated by exactly one "non-letter" in the file?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM