运行程序时,它让我在名称后输入两行?请帮忙

when running program it has me enter two lines after name? help please

本文关键字:两行 输入 程序 运行      更新时间:2023-10-16

我的程序似乎想为 name 变量输入两个输入,而不仅仅是输入一件事并转到电话号码?

我确定它很简单,但有人可以帮我解决这个问题吗? 它与 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;
}