运行程序时,它让我在名称后输入两行?请帮忙
when running program it has me enter two lines after name? help please
我的程序似乎想为 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;
}
相关文章:
- C++ 从文本文件中读取每两行,并且在矢量中是安全的
- Arduino Nano:A4988 使用串行输入时通过定时器进行步进控制不稳定
- Cin 需要两个输入
- 使用makefile中的命令行输入
- std::具有两个输入参数的矢量构造函数
- Qt读取串行输入,然后将其拆分为单独的变量
- 有没有办法将两个输入图像堆叠到卤化物发生器中的单个 4 维缓冲区中?
- 这两行代码有什么区别
- 为什么在输入的第一行输入测试用例数量后得到垃圾值?
- 矩阵中的内积两行C++与特征
- 如何停止带有串行输入的循环
- C++使用命令行输入 txt 文件,但打开文件失败
- 使用 c++ 中的 getline 函数连续读取两行
- 如果两个输入流相等,我如何构造主()到输出是
- 将用户输入验证为整数:用户必须两次输入输入
- 一条线和两行声明之间的差异
- C 错误:从命令行输入更改为变量输入
- C 如何进行计数循环,该循环确定两个输入数字中的哪个是最低的,并且从那里计数
- C++:如何在两行中为一个字符串和一个整数分配相同的输入
- 运行程序时,它让我在名称后输入两行?请帮忙