Amature needs help please!

This is a discussion on Amature needs help please! within the C++ Programming forums, part of the General Programming Boards category; How do you write a program that reads a text file and counts the number of words in that file? ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    8

    Question Amature needs help please!

    How do you write a program that reads a text file and counts the number of words in that file? I'm so lost at this...summer school sucks....

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498

  3. #3
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39
    file i/o thats what my friend c+noob is on
    I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)

  4. #4
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    jaybo ill give you a hand AIM: deathbydesire101 MSN: coolguy_5000@hotmail.com

    ive done this script before and ill post in a sec

  5. #5
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    here ya go this reads the file for example i got it opening cool.txt in C:\
    just put the directory were your file is please dont use this for the hell of it try to learn frum it. ask any questions you dont know

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
        int blank_count = 0;
        int char_count = 0;
        int sentence_count = 0;
        char ch;
    
        ifstream iFile("c:/cool.txt");
    
        if (! iFile)
        {
            cout << "Error opening input file" << endl;
            return -1;
        }
    
        while (iFile.get(ch))
        {
            switch (ch) {
                case ' ':
                    blank_count++;
                    break;
                case '\n':
                case '\t':
                    break;
                case '.':
                    sentence_count++;
                    break;
                default:
                    char_count++;
                    break;
            }
        }
    
        cout << "There are " << blank_count << " blanks" << endl;
        cout << "There are " << char_count << " characters" << endl;
        cout << "There are " << sentence_count << " sentences" << endl;
    
        return 0;
    }

  6. #6
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    i only got the sentance count to work for every period so that should give you a general idea

  7. #7
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    I'm not going to write your program for you, but this should give you an idea.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(){
        
        string s;
        int i = 0;
        while ( cin >> s ){
              
              i++;
        }
        
        cout << "You entered " << i << " words.";
    }
    Modify the code so that cin becomes the input stream of the file.
    Last edited by dra; 07-10-2005 at 03:10 AM.

  8. #8
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Summer School computer programming class? where is the drool smilie!

    Agh, my school doesnt even have a regular computer programming course. Well, actually it does have one described but not enough people sign up. It must be a Canadian thing..

    Oh yeah and for word counting, of course, pseudo-ish
    Code:
    make a char getChar;
    make an int charCount;
    
    open the file
    
    while (you get char) { //takes a char in until the end of file
      if charCount is equal to a space: add 1 to the charCount
    }
    
    print the charCount
    Though I doubt the tutorial on this site is enough info on file I/O, maybe try about.com too.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    >Summer School computer programming class? where is the drool smilie!

    Yeah same here. My school doesn't even offer a programming class during the regular shcool year! The only classes that are computer-related are CISCO Networking and A+

  10. #10
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    Quote Originally Posted by dra
    I'm not going to write your program for you, but this should give you an idea.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(){
        
        string s;
        int i = 0;
        while ( cin >> s ){
              
              i++;
        }
        
        cout << "You entered " << i << " words.";
    }
    even i have a bit of a time making that out the while loop would work much nicer.

  11. #11
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by C+noob
    even i have a bit of a time making that out the while loop would work much nicer.
    He did it like that so the topic creator could learn about file I/O and convert it and optimize it himself, since it is for school it would be better that way (in most peoples eyes). Converting that code may even turn out nicer looking than yours :P
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #12
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Quote Originally Posted by C+noob
    here ya go this reads the file for example i got it opening cool.txt in C:\
    just put the directory were your file is please dont use this for the hell of it try to learn frum it. ask any questions you dont know

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
        int blank_count = 0;
        int char_count = 0;
        int sentence_count = 0;
        char ch;
    
        ifstream iFile("c:/cool.txt");
    
        if (! iFile)
        {
            cout << "Error opening input file" << endl;
            return -1;
        }
    
        while (iFile.get(ch))
        {
            switch (ch) {
                case ' ':
                    blank_count++;
                    break;
                case '\n':
                case '\t':
                    break;
                case '.':
                    sentence_count++;
                    break;
                default:
                    char_count++;
                    break;
            }
        }
    
        cout << "There are " << blank_count << " blanks" << endl;
        cout << "There are " << char_count << " characters" << endl;
        cout << "There are " << sentence_count << " sentences" << endl;
    
        return 0;
    }
    Not to be an ass or anything but although your code does a few interesting things, it doesn't achieve the topic creator's task which is to count words. althought you could count spaces, then add 1....

    "One Two" <--- one space : 1 + 1 = 2 words. lol. But that's too much work in my opinion.

  13. #13
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    haha i thot he mmeant characters hahaha :|

  14. #14
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by dra
    >Summer School computer programming class? where is the drool smilie!

    Yeah same here. My school doesn't even offer a programming class during the regular shcool year! The only classes that are computer-related are CISCO Networking and A+
    Damn your lucky...My school only has 2 computer classes (c++/java and CISCO Networking) If they had more during summer school I dont care what they would be I'd take it. I love learning bout comps and stuff.
    Last edited by JoshR; 07-10-2005 at 04:27 PM.

  15. #15
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by Dae
    Code:
    make a char getChar;
    make an int charCount;
    
    open the file
    
    while (you get char) { //takes a char in until the end of file
      if charCount is equal to a space: add 1 to the charCount
    }
    
    print the charCount
    You'd have to change the if to...

    if charCount is equal to a space or newline or tab or EOF

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

Similar Threads

  1. Assembly amature question: prog errors
    By geek@02 in forum Tech Board
    Replies: 1
    Last Post: 10-03-2008, 02:35 PM

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