Well, it must be stored in an array....
Well, it must be stored in an array....
Yes.
Hmm... what is the longest length of a "word" that you may encounter in the file?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.Originally Posted by thomann061
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
a string array will suffice. I think 20 characters would cover the longest word.
I included the string in case I did use it. I'm not sure whether or not I need it.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.Originally Posted by thomann061
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
- You must use an array to store the information for each character.
- You must have these functions in your code: (you may have others but not necessary)
- 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.
- To exit a program early, you can use the function exit(0), but you must include the file cstdlib in your code.
- 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.
- 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.
- 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.
You must use either a selection sort, or an insertion sort.
- 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
Well, that settles it. However, there is no restriction on using std::string.Originally Posted by thomann061
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.Originally Posted by thomann061
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.Originally Posted by thomann061
Have you learnt how to define structs/classes?Originally Posted by thomann061
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.Originally Posted by thomann061
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes we have learned structs, and classes.