How to read a string from the console and store it in a string in C ++?

I need to read an entire line from the console and store it in an array std::string and char for example.

 "Hi this is balaji" 

Now I have to read the line above and save it in string . I tried this with the getline() function.

+6
source share
3 answers

Try:

 #include <string> #include <iostream> int main() { std::string line; std::getline(std::cin, line); // read a line from std::cin into line std::cout << "Your Line Was (" << line << ")\n"; std::getline(std::cin, line); // Waits for the user to hit enter before closing the program } 
+17
source

Maybe something is wrong with the way you use cin.getline ()?

  cin.getline (name,256); 

C ++ refence for getline ()

0
source

Can

 string a; cin >> a; cout << a << endl; 

Or something like that?

0
source

Source: https://habr.com/ru/post/886505/


All Articles