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
(David heffermanの発言の後で修正):
struct patient {
std::string nam, nom, prenom, adresse;
};
次に、以下が機能するはずです(論理的思考によってDavidHeffernanios::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);
...