![](/img/trans.png)
[英]How can I structure my C++ code so that I only write my common methods once?
[英]how can i write this code so that it will accept “space” as a part of address?
在这里,我编写了一个将地址作为用户输入的代码,但是当用户在地址之间输入“空格”时,编译器将下一个单词作为下一个输入。我该如何编写它以便它将“空格”作为地址的一部分. 注意:我必须仅使用构造函数和复制构造函数来执行此操作
#include<iostream>
#include<string.h>
using namespace std;
class address{
string add;
public:
address(){
cout << "Enter your current address"<<endl;
cin >> add;
}
address(const address &ad1){
add=ad1.add ;
cout << "Permanent add: "<<add;
}
};
int main(){
char c;
string add2;
address ad1;
cout << "Is your permanent address same as current address? y for yes" <<endl;
cin >> c;
if(c=='y'||c=='Y')
{
address ad2=ad1;
}
else{
cout << "Enter your permanent address"<<endl;
cin >> add2;
}
}
您应该改用std::getline
。
即替换
address() {
cout << "Enter your current address"<<endl;
cin >> add;
}
和
address() {
cout << "Enter your current address" << endl;
getline(cin, add);
}
如果需要,当您想要获取新地址时,请执行相同的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.