简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM