[英]how can i write this code so that it will accept “space” as a part of address?
here i wrote a code for taking address as input from user, but when user enters "space" between address, compiler takes the next word as next input.how can i write it so that it will take "space" as a part of address.在这里,我编写了一个将地址作为用户输入的代码,但是当用户在地址之间输入“空格”时,编译器将下一个单词作为下一个输入。我该如何编写它以便它将“空格”作为地址的一部分. note:i have to do this using constructor and copy constructor only
注意:我必须仅使用构造函数和复制构造函数来执行此操作
#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;
}
}
You should use std::getline
instead.您应该改用
std::getline
。
ie replace即替换
address() {
cout << "Enter your current address"<<endl;
cin >> add;
}
with和
address() {
cout << "Enter your current address" << endl;
getline(cin, add);
}
and then do the same for when you want to get the new address if needed.如果需要,当您想要获取新地址时,请执行相同的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.