It is necessary to read a string from the console into the char array.
The problem is that scanf and cin read the string only up to the first space, and I need to write the entire string to the array.
How do I solve this problem?
It is necessary to read a string from the console into the char array.
The problem is that scanf and cin read the string only up to the first space, and I need to write the entire string to the array.
How do I solve this problem?
Use the member function of the getline input stream class.
Here are their ads
basic_istream<charT,traits>& getline(char_type* s, streamsize n); basic_istream<charT,traits>& getline(char_type* s, streamsize n, char_type delim); Or you can use the member function of the get class in the input stream. Her ad looks like the previous function.
basic_istream<charT,traits>& get(char_type* s, streamsize n); basic_istream<charT,traits>& get(char_type* s, streamsize n, char_type delim); The difference between these functions is that the first function reads the chimvol delim (if it is not specified, the newline character) from the input stream, but does not add it to the string. While the second function leaves this character in the input stream.
I think you better use the first function.
Here is an example of working with the getline function.
#include <iostream> int main() { const size_t N = 50; char s[N]; std::cout << "Enter a sentence: "; std::cin.getline( s, N ); std::cout << '\n' << s << std::endl; return 0; } The dialogue with this function may look, for example, as follows:
Enter a sentence: Hello World! Hello World! If you entered with the operator operator >> before calling this function, then a newline character may remain in the input buffer. It can be removed by calling another member function of the ignore class. The simplest call to this function looks like
std::cin.ignore(); Either to remove a newline character, then
#include <limits> //... std::cin.ignore( std::numeric_limits<streamsize>::max() ); Source: https://ru.stackoverflow.com/questions/584042/
All Articles
cin, notcout. - maestro