C++ 在c+;中接受空白字符串时出现问题+;

C++ 在c+;中接受空白字符串时出现问题+;,c++,string,C++,String,我写这个代码是为了得到这个名字。一个人的电话和地址,然后我将这些输入到类对象变量中: #include<iostream> #include<cstdlib> #include<fstream> #include<string> using namespace std; class contact{ public: string name;//ALL CLASS VARIABLES ARE PUBLIC unsigned int

我写这个代码是为了得到这个名字。一个人的电话和地址,然后我将这些输入到类对象变量中:

#include<iostream>
#include<cstdlib>
#include<fstream>
#include<string>
using namespace std;

class contact{
public:
    string name;//ALL CLASS VARIABLES ARE PUBLIC
    unsigned int phonenumber;
    string address;

    contact(){//Constructor
        name= "Noname";
        phonenumber= 0;
        address= "Noaddress";
    }

    /*void input_contact_name(string s){//function to take contact name
        name=s;
    }
    void input_contact_number(int x){//function to take contact number
        phonenumber=x;
    }
    void input_contact_address(string add){//function to take contact address
        address=add;
    }*/
};

int main(){
    contact *d;
    d= new contact[200];
    string name,add;
    int choice;//Variable for switch statement
    unsigned int phno;
    static int i=0;//i is declared as a static int variable
    bool flag=false;
    cout<<"\tWelcome to the phone Directory\n";//Welcome Message
    cout<<"Select :\n1.Add New Contact\n2.Update Existing Contact\n3.Delete an Existing Entry\n4.Display All Contacts\n5.Search for a contact\n6.Exit PhoneBook\n\n\n";//Display all options
    cin>>choice;//Input Choice from user
    while(!flag){//While Loop Starts
        switch(choice){//Switch Loop Starts
        case 1:
            cout<<"\nEnter The Name\n";
            cin>>name;

            d[i].name=name;
            cout<<"\nEnter the Phone Number\n";
            cin>>phno;
            d[i].phonenumber=phno;
            cout<<"\nEnter the address\n";
            cin>>add;
            d[i].address=add;
            i++;

            flag=true;
        }
    }
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
班级联系{
公众:
字符串名称;//所有类变量都是公共的
无符号整数电话号码;
字符串地址;
contact(){//构造函数
name=“Noname”;
电话号码=0;
address=“Noaddress”;
}
/*无效输入联系人姓名(字符串s){//函数以获取联系人姓名
name=s;
}
无效输入联系人号码(int x){//函数获取联系人号码
phonenumber=x;
}
无效输入联系人地址(字符串添加){//函数获取联系人地址
地址=添加;
}*/
};
int main(){
联系人*d;
d=新联系人[200];
字符串名称,添加;
int choice;//switch语句的变量
无符号整数phno;
static int i=0;//i被声明为静态int变量
布尔标志=假;

cout使用而不是
操作符>
来提取包含空格的
std::string

使用而不是
操作符>
来提取包含空格的
std::string

请在格式化代码方面下功夫。发布较短的示例也是一个好主意。谢谢Mat,我将保留该示例我建议你接受ildjarn给出的答案。这已经有一段时间了,而且是正确的答案。请在格式化你的代码上下功夫。发布较短的示例也是一个好主意。谢谢Mat,我以后会记住这一点:我建议你接受ildjarn给出的答案。这已经有一段时间了,我t是正确的答案。我想知道运算符>>和带空格的字符串有什么问题?
运算符>>
读取一个元素。通常,一个元素被认为由下一个空格终止,就像std::string和所有原语一样。
std::getline
是你的朋友,正如答案所说。我想知道pr是什么问题在于运算符>>和带空格的字符串?
operator>
读取一个元素。通常,一个元素被认为是由下一个空格终止的,就像std::string和所有原语一样。
std::getline
是您的朋友,正如答案所说。