Trying to count numbers in file

This is a discussion on Trying to count numbers in file within the C++ Programming forums, part of the General Programming Boards category; So I am using file stream to see in a file. Then read the contents of the file. I am ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Trying to count numbers in file

    So I am using file stream to see in a file. Then read the contents of the file. I am supposed to be counting the number of words, sentences, digits etc. I am working on the digit count right now and this is not working! It should work- unless I am seeing something small that I dont have. Please someone help me! Heres the code:

    Code:
    int numdigits(ifstream& name)
    {
      string words;
      char ch;
      int count = 0;
    
     name >> words;
     while(!name.eof())
       {
         name >> ch;
        if (ch == 1)
           {
          count ++;
           }
        name >> words;
       }
      return count;
    }
    Iwant it to count the number of digits in the file. Thanks!

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    if (ch == 1)
    Should be:
    Code:
    if (ch == '1')
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    tried

    hey i tried that- still doesnt work- i just forgot to put them back in.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Answer

    All it is returning is a 0. But if i change the int count to int count = 1 it will return a 1. So is it only reading my count variable and returning that without going through the while loop?

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    hmm, why just '1'? there are 10 digits
    Code:
    ch >= '0' && ch <= '9'
    maybe?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    digits

    I am just trying to get it working with the 1 digit and then i will add the rest in. I purposely put a couple 1's in my file to see if it will read it and its not. but thanks!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Notice how you are reading into the words variable, and later reading into the ch variable. Every time you read into words, it will read in an entire string until it finds whitespace (skipping leading whitespace). Your code is ignoring what is read into words.

    If you are only checking for digits, you should remove the name >> ch line, and then change words to ch in the other two places that read from name. You only need to read in single characters, not whole strings.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    tried

    Daved-

    Normally you help me but this time it still didnt work. Still returning a 0. Am i calling the function right?

    Code:
     while(!name.eof())
        {
         getwords(name);
         numdigits(name);
        }
    There is nothing that I have not tried- that is why I had to come here and see if someone could help.

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Won't this check for EOF twice? I'm not so sure that function is your problem. Post all your code.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Reread Daved post, ammochck21. It's all there
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    all of the code

    Code:
     
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    
    string getwords(ifstream&);
    int numwords(ifstream&);
    int numsent(ifstream&);
    int numdigits(ifstream&);
    int numsearch(ifstream&);
    int numletters(ifstream&);
    
    int main()
    {
      string filename;
      string search;
      string words;
      string x;
      ifstream name;
    
      cout << endl;
      cout << "Please enter the name of the input file" << endl;
      cin >> filename;
      cout << endl;
      name.open(filename.c_str());
      cout << "Please enter your search string" << endl;
      cin >> search;
      cout << endl;
    
      cout << "The reformatted content of " << filename << " is:" << endl;
      cout << endl;
    
       while(!name.eof())
        {
         getwords(name);
         numdigits(name);
        }
    
      cout << endl;
      cout << endl;
      cout << "# of words: " << endl;                             //getwords(name) << endl;
      cout << "# of sentences: " << endl;                         //<< numsent(name);
      cout << "# of letters: " << endl;                           //<< numletters(name);
      cout << "# of digits: " << numdigits(name) << endl;
      cout << "# of occurrences of " << search << ":" << endl;    // << numsearch(name);
      cout << endl;
    }
    
    string getwords(ifstream& name)
    {
      int sum;
      string words;
      int wlen;
      int sumline;
      int count;
    
      name >> words;
      while(!name.eof())
       {
         //wlen = words.length();
         //sum = sum + wlen;
         //while (sum <= 40)
         // {
    
           cout << words << " ";
         // }
        name >> words;
        }
    
     return words;
    }
    
    
    int numdigits(ifstream& name)
    {
      string words;
      char ch;
      int count = 0;
    
     name >> ch;
     while(!name.eof())
       {
         if (ch =='1')
           {
          count ++;
          cout << count;
           }
        name >> ch;
       }
      return count;
    }
    All the cout << endl is just because I like spacing. Please some tell me you see the problem!

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    delete

    disregard the cout << count in the loop- i was just trying to test it.

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    What about rewind before you starting count from the beginning? Or just count everything at one scan
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You read through the file once to get the words, but then you try to read through it again to count the digits. You can do it that way if you like (it might not be the best solution), but you have to re-open the file or seek to the beginning, then clear the flags before starting over.

    Your numdigits function looks ok by itself. Try commenting out the call to getwords to test whether numdigits is working properly. Then work on re-opening the file or changing your code to test them all at once.

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    thanks

    Thank you! I will try that- time for class. I am sure I will back with other questions. Thanks again everyone!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21