我如何输入带空格的字符串
How do i input strings with spaces?
我想输入一个字符串(中间有空格),我使用cin.getline()。但是我得到一个运行时错误(循环执行无限)。
class account
{
int acno;
int password;
char name[50];
char address[100];
char sex;
string phonenumber;
public:
void create_account(); //function to get data from user
void show_account() const; //function to show data on screen
void modify(); //function to add new data
void withdraw(int,int); //function to accept amount and subtract from balance amount
void donate(int,int); //function to accept amount and add to balance amount
void report() const; //function to show data in tabular format
int retacno() const; //function to return account number
int retpassword() const; //function to return password
}; // class definition
这是我为类记录输入数据的函数。
void account::create_account()
{
cout<<"nEnter The account No. :";
cin>>acno;
cout<<"nnEnter The Name of The account Holder : ";
cin.getline(name,49);
cin.ignore();
cout<<"nnEnter your Password (Max 8 characters) : ";
cin>>password;
cin.ignore();
cout<<"nnEnter your Address : ";
cin.getline(address,99);
cin.ignore();
cout<<"nEnter your Contact Number: ";
cin>>phonenumber;
cin.ignore();
cout<<"nSex (Enter M for male and F for female): ";
cin>>sex;
cout<<"nnnAccount Created..nn";
}
当我执行这个时,如果我在字符串条目之间引入空格,我会得到一个运行时错误如果我不使用cin.ignore()函数,它会跳过字符串输入
使用独立的std::getline
函数,而不是成员函数。前者在std::string
上操作,因此您不必使用原始指针。
相关文章:
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 移除前面有空格的前2个字符串
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 在字符串中插入空格
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 使用另一个字符串从字符串中删除空格
- 我还可以添加什么来按空格或空格正确拆分C++字符串?
- C++ "noskipws"没有按预期工作,如何正确允许字符串中的空格?
- 在 c++ 中逐行阅读字符串,不要以空格停止
- std::regex:匹配由数字和空格组成的字符串,并提取数字.如何?
- 如何存储包含多个空格的字符串 c++
- 在特定位置为字符串添加空格
- 有没有办法在 c++ 中获取带有空格的完整字符串?
- C++完全复制要字符串的字符列表,而不会忽略多个空格
- Winsock2:当我尝试发送带有空格的字符串时,该函数在遇到空格时似乎停止发送
- 从没有空格的字符串中提取整数
- 用于拆分空格字符串的程序不起作用
- 解析C++中的完整(从头到尾)字符串,包括所有空格
- 反转字符串中单词的位置,而不更改 O(1) 空格限制中特殊字符的顺序
- 当我的 if 语句在字符串中找到空格时,在无限循环中切换