为什么在函数中使用多个 getlines 来输入字符串会出现意外行为?
Why the use of multiple getlines in a function to input strings acts unexpectedly?
我正在开发一个打印公司信息(例如公司名称、CEO 姓名等)的程序。我正在使用 struct 对所有相关变量进行分组。我的结构看起来像:
struct Company
{
std::string NameOfCompany;
std::string NameOfCeo;
int Revenue;
int NumOfEmployees;
std::string BranchesIn;
};
我定义了一个void printInfo(Company x)来打印一个公司的信息。我的 main () 从用户那里获取数据并将其传递给 printInfo()。剩下的事情由 printInfo() 完成。我的问题是下面的 main ():
int main ()
{
using namespace std;
Company x;
cout << "Name of company: ";
getline(cin, x.NameOfCompany);
cout << "Name of CEO: ";
getline(cin, x.NameOfCeo);
cout << "Total Revenue: ";
cin >> x.Revenue;
cout << "Number of employees: ";
cin >> x.NumOfEmployees;
cout << "Branches In: ";
getline(cin, x.BranchesIn);
printInfo (x);
return 0;
}
上面的代码可以很好地编译和链接(不包括 void printInfo (),没有错)。执行时,程序从获取 x.NameOfCompany 的输入开始。没关系。但是其他的getline() 都让我百思不得其解。程序从不要求输入一个(或两个,当我更改 getlines 的顺序时,例如 NameOfCeo 之前的 BranchesIn)变量,这些变量设置为通过 getline 方法接受输入。有什么问题。 getline 有什么限制吗?
如果代码有任何语法或印刷错误,请忽略。我发誓链接和编译都很好。
以下代码将为您提供所需的信息。我注释掉打印方法,因为它没有在问题中提供。
int main ()
{
using namespace std;
Company x;
cout << "Name of company: ";
getline(cin, x.NameOfCompany);
cout << "Name of CEO: ";
getline(cin, x.NameOfCeo);
cout << "Total Revenue: ";
cin >>x.Revenue;
cout << "Number of employees: ";
cin >>x.NumOfEmployees;
// Ignore everything what is left in the input stream after
// the data for NumOfEmployees.
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Branches In: ";
getline(cin, x.BranchesIn);
//printInfo (x);
return 0;
}
您似乎缺少 cin.ignore()。在 cin 之后添加这个 >> x.NumOfEmployees:
cin.ignore(1, '\n'); //this will allow branchesin to be requested.
这是因为在使用 getline 之前先使用 cin 然后使用 getline 时,您需要刷新缓冲区。
至于重复的问题。它更类似于 this one。
我正在开发一个打印公司信息(例如公司名称、CEO 姓名等)的程序。我正在使用 struct 对所有相关变量进行分组。我的结构看起来像:
struct Company
{
std::string NameOfCompany;
std::string NameOfCeo;
int Revenue;
int NumOfEmployees;
std::string BranchesIn;
};
我定义了一个void printInfo(Company x)来打印一个公司的信息。我的 main () 从用户那里获取数据并将其传递给 printInfo()。剩下的事情由 printInfo() 完成。我的问题是下面的 main ():
int main ()
{
using namespace std;
Company x;
cout << "Name of company: ";
getline(cin, x.NameOfCompany);
cout << "Name of CEO: ";
getline(cin, x.NameOfCeo);
cout << "Total Revenue: ";
cin >> x.Revenue;
cout << "Number of employees: ";
cin >> x.NumOfEmployees;
cout << "Branches In: ";
getline(cin, x.BranchesIn);
printInfo (x);
return 0;
}
上面的代码可以很好地编译和链接(不包括 void printInfo (),没有错)。执行时,程序从获取 x.NameOfCompany 的输入开始。没关系。但是其他的getline() 都让我百思不得其解。程序从不要求输入一个(或两个,当我更改 getlines 的顺序时,例如 NameOfCeo 之前的 BranchesIn)变量,这些变量设置为通过 getline 方法接受输入。有什么问题。 getline 有什么限制吗?
如果代码有任何语法或印刷错误,请忽略。我发誓链接和编译都很好。
以下代码将为您提供所需的信息。我注释掉打印方法,因为它没有在问题中提供。
int main ()
{
using namespace std;
Company x;
cout << "Name of company: ";
getline(cin, x.NameOfCompany);
cout << "Name of CEO: ";
getline(cin, x.NameOfCeo);
cout << "Total Revenue: ";
cin >>x.Revenue;
cout << "Number of employees: ";
cin >>x.NumOfEmployees;
// Ignore everything what is left in the input stream after
// the data for NumOfEmployees.
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Branches In: ";
getline(cin, x.BranchesIn);
//printInfo (x);
return 0;
}
您似乎缺少 cin.ignore()。在 cin 之后添加这个 >> x.NumOfEmployees:
cin.ignore(1, '\n'); //this will allow branchesin to be requested.
这是因为在使用 getline 之前先使用 cin 然后使用 getline 时,您需要刷新缓冲区。
至于重复的问题。它更类似于 this one。