Dav*_*nan 30
你在找std::getline().例如:
#include <string>
std::string str;
std::getline(std::cin, str);
Run Code Online (Sandbox Code Playgroud)
当你说我还必须能够通过指针存储值时,我不知道你的意思.
更新:看看你更新的问题,我可以想象发生了什么.读取选项的代码,即数字1,2等不是读取换行符.然后你调用getline哪个消耗换行符.然后getline再次调用取出字符串.
Reads the next line of characters from the standard input stream.
Run Code Online (Sandbox Code Playgroud)
C++ - Variant(不涉及指针):
#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;
}
Run Code Online (Sandbox Code Playgroud)
#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);
}
Run Code Online (Sandbox Code Playgroud)
patient(在David hefferman的评论之后纠正):
struct patient {
std::string nam, nom, prenom, adresse;
};
Run Code Online (Sandbox Code Playgroud)
然后,以下应该有效(ios::ignore在DavidHeffernan通过逻辑思考解决了其他问题之后添加).请不要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);
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51974 次 |
| 最近记录: |