gpt4 book ai didi

c++ - 运行程序时它让我在名称后输入两行?请帮助

转载 作者:行者123 更新时间:2023-11-28 06:33:48 26 4
gpt4 key购买 nike

我的程序似乎想为名称变量输入两个输入,而不是只输入一个东西然后继续输入电话号码?

我确定它很简单,但有人可以帮我解决这个问题吗?它与 getline 有什么关系吗?

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//define Car struct
struct Speaker
{
string name;
string phoneNumber;
string emailAddress;
string theme;
double fee;
};

Speaker *getSpeaker();


int main()
{
Speaker thespeaker;
thespeaker = *getSpeaker();
cout << "The speaker entered is!" << endl;
cout << thespeaker.name << endl;
cout << "phone number: " << thespeaker.phoneNumber << endl;
cout << "email: " << thespeaker.emailAddress << endl;
cout << "theme: " << thespeaker.theme << endl;
cout << "fees: " << thespeaker.fee << endl;
}

Speaker *getSpeaker()
{
Speaker *theSpeaker;
theSpeaker = new Speaker;
cout << "Please enter Speakers information" << endl;
cout << "name: " ;
getline(cin, theSpeaker->name);
cin.ignore(100, '\n');
cin.clear();
cout << theSpeaker->name;
cout << "\nphone number: ";
cin >> theSpeaker->phoneNumber;
cout << "\nEmail Address: ";
cin >> theSpeaker->emailAddress;
cout << "\nTheme: ";
cin >> theSpeaker->theme;
cout << "\nFee: ";
cin >>theSpeaker->fee;

return theSpeaker;
}

最佳答案

不需要 cin.ignore();简单地写成:

Speaker *getSpeaker()
{
Speaker *theSpeaker;
theSpeaker = new Speaker;
cout << "Please enter Speakers information" << endl;
cout << "name: " ;
getline(cin, theSpeaker->name);
cout << theSpeaker->name;
cout << "\nphone number: ";
cin >> theSpeaker->phoneNumber;
cout << "\nEmail Address: ";
cin >> theSpeaker->emailAddress;
cout << "\nTheme: ";
cin >> theSpeaker->theme;
cout << "\nFee: ";
cin >>theSpeaker->fee;

return theSpeaker;
}

关于c++ - 运行程序时它让我在名称后输入两行?请帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27097583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com