Exercise 1.17. Write a program to print all input lines containing more than 80 characters.

Characters are entered from the console. I can not understand how to set an array, which will have a string consisting of the entered characters and in it so that the number of these lines is considered. Moreover, what would the output of this array look like?

    2 answers 2

    If you need a string - use char * , char [SOME_CONST] or std::string (the latter is preferable, since this is a specialized class of strings, so there is less chance of running into memory problems)

    If you need an array of strings (really needed) - either a pointer to a pointer, or std::vector<std::string> (desirable). For the passage through the vector will have to use iterators. It is not as scary as it sounds.

    Entering lines from the keyboard can be done in many ways. Starting from character, ending line by line. scanf("%s", pointer_to_char_arr) normal with scanf("%s", pointer_to_char_arr) , fgets() , std::getline or cin::getline . In short, the ways mass. It is necessary to pay special attention for not going beyond the boundaries of buffers and to work carefully with pointers (if you use them).

    • PS if it is not clear - ask, try to figure it out together. - gecube

    To solve this problem, arrays are not needed !!!

    Algorithm:

    Read line by line. If the length of the line is more than 80, print it. At the end of the input end the program.

    Everything.

    • Well, how can I set this dynamic array then? There is an algorithm, but there is no string array ... - KyJIu4
    • I repeat, the array of lines is not needed !!!! One line is enough (if you study the materiel, then 82 characters will suffice (taking into account the terminating zero line)). - avp
    • one
      I'm afraid you misunderstood the task. Initially, we have entered the line. And they are displayed at the end of the program. Those. It looks like this: <pre> Data entry: str1 str2 str3 .... Among the entered lines there are the following lines with the number of characters> 80: abcdef .... abcdef1 .... </ pre> About blocking the display of entered characters not a word. - gecube pm
    • do I have to create str1 str2 str3 for each line? Or can I specify an array to store these strings? can be an example pozhalst. - KyJIu4
    • Approximately like this: <pre> std :: vector <std :: string> str_array; while (...) // end the loop condition yourself {std :: string my; // defined the temporary variable of the string std :: cin.getline (my, 256); // put the lines of more than 256 characters will not be str_array.push_back (my); // put the next line in the vector} std :: vector <std :: string> :: iterator iter; // iterator for (iter = str_array.begin (); iter! = str_array.end (); ++ iter) std :: cout << * iter << std :: endl; // display the next line on the screen </ pre> - gecube