簡體   English   中英

我該如何編寫這段代碼,以便它接受“空格”作為地址的一部分?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM