根据MSDN, Console::ReadLine:
Reads the next line of characters from the standard input stream.
C++ 变体(不涉及指针):
#include <iostream>
#include <string>
int main()
{
std::cout << "Enter string:" << flush;
std::string s;
std::getline(std::cin, s);
std::cout << "the string was: " << s << std::endl;
}
C-Variant(带有缓冲区和指针),也
适用于 C++ 编译器,但不应使用:
#include <stdio.h>
#define BUFLEN 256
int main()
{
char buffer[BUFLEN]; /* the string is stored through pointer to this buffer */
printf("Enter string:");
fflush(stdout);
fgets(buffer, BUFLEN, stdin); /* buffer is sent as a pointer to fgets */
printf( "the string was: %s", buffer);
}
根据您的代码示例,如果您有一个结构
patient (在大卫赫弗曼的评论后更正):
struct patient {
std::string nam, nom, prenom, adresse;
};
然后,以下应该可以工作(通过逻辑思维在附加问题为solved by DavidHeffernan 之后添加ios::ignore)。请不要在您的代码中完全使用scanf。
...
std::cin.ignore(256); // clear the input buffer
patient *ptrav = new patient;
std::cout << "No assurance maladie : " << std::flush;
std::getline(std::cin, ptrav->nam);
std::cout << "Nom : " << std::flush;
std::getline(std::cin, ptrav->nom);
std::cout << "Prenom : " << std::flush;
std::getline(std::cin, ptrav->prenom);
std::cout << "Adresse : " << std::flush;
std::getline(std::cin, ptrav->adresse);
...