简体   繁体   English

我该如何编写这段代码,以便它接受“空格”作为地址的一部分?

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

相关问题 如何构造C ++代码,以便仅编写一次通用方法? - How can I structure my C++ code so that I only write my common methods once? 如何编写 function 以在 SWIG 中接受分数 object? - How can I write a function to accept a Fraction object in SWIG? 如何正确处理 SIGBUS 以便我可以继续搜索地址? - How to correctly handle SIGBUS so I can continue to search an address? 我可以直接为指针分配地址吗? 如果是这样,该怎么做? - Can I directly assign an address to a pointer? If so, how to do that? 如何编写可以接受堆栈或队列的函数模板? - How can I write a function template that can accept either a stack or a queue? 如何编写将被继承的通用代码? - How can I write generic code that will be inherited? 解码摩尔斯电码时如何添加空间 - How can I add space while decoding Morse code 如何重载 &lt;&lt; 运算符以便将对象的数据写入文件? - How can I overload the << operator so I can write the data of an object to a file? 我如何在数组中找到空白空间并用部分代码填充它/我如何将 3 个 randed 数组合并为一个并随机洗牌? - How do i find empty space in array and fill it with part of code / how do i rand 3 randed arrays into one and shuffle it randomly? 如何编写接受无限参数的函数? - How do I write functions that accept unlimited arguments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM