Thread: I want my program to count all the words, and characters within a file

  1. #16
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    Well, it must be stored in an array....

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why do you need to use an array? Is this some sort of requirement?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    Yes.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hmm... what is the longest length of a "word" that you may encounter in the file?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    I guess it would be around 20 characters?

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, so fine, you need an array. Do you need a char array, or will a string array suffice?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thomann061
    I guess it would be around 20 characters?
    Don't guess: find out what are the requirements. If the requirements are that the longest "word" allowed is 20 characters, then you would need an array of at least 21 characters since you are presumably working with null terminated strings.

    That said, if you are supposed to only work with null terminated strings, then why do you use std::string in the code that you posted in post #1? If you use std::string instead, then you don't need to worry about how long the longest "word" will be.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    a string array will suffice. I think 20 characters would cover the longest word.

  9. #24
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    I included the string in case I did use it. I'm not sure whether or not I need it.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, let's put aside this issue of strings for a moment: what idea do you have "to count all the words, and characters within a file" in the first place?

    EDIT:
    Also, what is a "word"? How many "words" and characters must you cater for?

    You know, are you really sure that you must stick to using arrays for this? Are you really sure that you cannot use std::string? How incompetent a C++ programmer is your teacher, anyway? Sorry, but I really cannot resist ranting at people who choose to teach the basics of programming in C++, choosing to handicap their students, and then later switch to using other programming languages because C++ is perceived as being too hard.
    Last edited by laserlight; 04-25-2013 at 12:23 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    Well, I thought I could use an array to store all the characters. If I used the getline() function to get each line individually, and then stored each line into an array.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thomann061
    Well, I thought I could use an array to store all the characters. If I used the getline() function to get each line individually, and then stored each line into an array.
    Yes, you can do that. But how are you going to count? That is key to solving this problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    1. You must use an array to store the information for each character.
    2. You must have these functions in your code: (you may have others but not necessary)
      1. getFileName – asks the user for the name of the input file. You must pass the file stream as a reference parameter. If the input file does not exist, you should print to the screen an appropriate message, and exit the program.
        1. To exit a program early, you can use the function exit(0), but you must include the file cstdlib in your code.
        2. Read the section titled ‘string Type and Input/Output Files’ on pages 541 – 542 of the book for information about using a string variable in an ‘open’ statement.



    1. countWords – counts the number of words in the file. Words are anything separated by whitespace. The file identifier must be passed in to the function. Should also print a message with the number of words in the file.



    1. countChars – counts every occurrence of each character (see above) in the file. The information must be stored in an array, which must be passed in as a parameter. The file identifier must also be passed in as a reference parameter. Should also calculate the percentage for each character.



    1. sortChars – sorts the array, in descending order, by the percentage.

    You must use either a selection sort, or an insertion sort.


    1. printCount – Prints each character, the number of each character, and the percentage of each character. The percentage should be in the form “25%”. The information should come from the array, which must be passed in as a reference parameter, and the info must be sorted

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thomann061
    You must use an array to store the information for each character.
    Well, that settles it. However, there is no restriction on using std::string.

    Quote Originally Posted by thomann061
    To exit a program early, you can use the function exit(0), but you must include the file cstdlib in your code.
    Don't do this: exit does not play well with one common method that is used in C++ to manage resources. It wouldn't matter for memory on a modern desktop OS, but it could matter for other resources.

    Quote Originally Posted by thomann061
    countWords – counts the number of words in the file. Words are anything separated by whitespace. The file identifier must be passed in to the function. Should also print a message with the number of words in the file.
    Ah, but this sounds like rather than the count of the number of times a word appears, this is asking for just the total word count. In this case, you don't actually need to store the words since you can count them while reading from file, except that then a countWords function would be redundant. As such, I think that you are required to store the words, but an array of std::string objects would be fine.

    Quote Originally Posted by thomann061
    countChars – counts every occurrence of each character (see above) in the file. The information must be stored in an array, which must be passed in as a parameter. The file identifier must also be passed in as a reference parameter. Should also calculate the percentage for each character.

    sortChars – sorts the array, in descending order, by the percentage.
    Have you learnt how to define structs/classes?

    Quote Originally Posted by thomann061
    The information should come from the array, which must be passed in as a reference parameter, and the info must be sorted
    The part in bold is probably a wrong requirement, with respect to C++ terminology. You are probably just expected to have a pointer to the first element of the array as the argument.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Registered User
    Join Date
    Apr 2013
    Posts
    45
    Yes we have learned structs, and classes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-16-2012, 12:51 PM
  2. Replies: 2
    Last Post: 12-02-2011, 09:19 PM
  3. c program to count words
    By bigwillys111 in forum C Programming
    Replies: 3
    Last Post: 04-22-2011, 11:57 PM
  4. count words program problem
    By Tony654321 in forum C Programming
    Replies: 8
    Last Post: 10-19-2004, 11:23 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM