Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

equivalent of Console.ReadLine() in c++

screenshot My teacher just gave me an assignment in c++ and I am trying to get a string with scanf but it only get the last characters typed. Can anyone help me please? I am looking for the equivalent of console.readline() in c++.

edit : I must also be able to store the value through a pointer.

so the picture show the code currently runnign in the background and it should have stoped at No assurance maladie : and waited for an input but it skipped it.

getline(cin, ptrav->nam); works but it skip a line for some reason...

like image 774
ESD Avatar asked Oct 09 '12 18:10

ESD


People also ask

What is console ReadLine C?

This method is used to read the next line of characters from the standard input stream. It comes under the Console class(System Namespace). If the standard input device is the keyboard, the ReadLine method blocks until the user presses the Enter key.

Which function is used to read data from the console in C?

In C, the scanf() function is used to read formatted data from the console.

What is the purpose of console ReadLine () method?

One of the most common uses of the ReadLine method is to pause program execution before clearing the console and displaying new information to it, or to prompt the user to press the Enter key before terminating the application.

What is the difference between console read and console ReadLine?

The only difference between the Read() and ReadLine() is that Console. Read is used to read only single character from the standard output device, while Console. ReadLine is used to read a line or string from the standard output device.


1 Answers

You are looking for std::getline(). For example:

#include <string>
std::string str;
std::getline(std::cin, str);

I've little idea what you mean when you say I must also be able to store the value through a pointer.

Update: Looking at your updated question, I can imagine what is happening. The code that reads the choice, i.e. the number 1, 2, etc. is not reading the newline. Then you call getline which consumes the newline. And then you call getline again which fetches the string.

like image 181
David Heffernan Avatar answered Oct 10 '22 10:10

David Heffernan