Help solve the problem.

Using a text editor to create a file containing text, the length of which does not exceed 1000 characters, the length of lines of text should not exceed 70 characters. Write a program that displays the contents of the file on the screen, determine the number of characters in the shortest word, by pressing an arbitrary key in turn select each word of text containing the minimum number of characters.

I only managed to make the output of information from the file on the screen:

int main() { setlocale(LC_ALL, ".ACP"); ifstream file("text.DAT", "r"); cout << file.rdbuf(); //file.close(); system("pause"); return 0; } 

What's next? Help me please.

    1 answer 1

    @ Olga 08 , try to approach the task somewhat differently.

    Look, the main goal is to select the next shortest word by pressing a key (and obviously the output of this word on the screen). Before that, they want you to output the entire file and the length of the shortest word. Obviously, there may be several shortest words.

    Also important information from the problem statement is the statement about the length of the file (1000). This will help you, you can not build dynamic data structures, but it is reasonable to assume that no more than 500 shortest words can be typed.

    Now define for yourself what a word is. For simplicity, assume that there are several characters separated by one or more delimiters (space, tab, end-of-line character (s)).

    Thus, you need to read the file by words. It's simple. I think you already read cin into a string variable. So do it in a loop until the file ends. Getting the word length is obvious.

    Next, you determine whether the next word is the shortest of the previously read (that is, it has not been shorter yet) and if so, place it in the first element of the array of the shortest words and note that there is one word in this array (for example, n = 1 ). If the read word turns out to be the next shortest , then add it to the array (place in the element of the array n and increase n ). Along the way, calculate the length of the shortest (shortest) words.

    It is clear that the task is divided into 3 easily realizable parts. First - the output file on the screen - you did. Conclusion directly from the buffer is a good thing in principle (one should strive for efficiency even in C ++). For good it needs to be improved so that you can copy files of any length (ie, longer than the buffer) and implement the output in a loop.

    The second part is re-reading the file by words, calculating the length of the shortest word and placing these words in the string array.

    The third part is the printing of this array one element at a time when the key is pressed.

    That is, in fact, the whole decision. (now you can ask smaller questions (if problems arise during the implementation of any of the parts)).

    Successes.