【发布时间】:2010-02-08 13:28:23
【问题描述】:
如何使用指向结构的指针来获取将存储在字符串变量中的输入?我认为简单地将 pz->szCompany 传递给 getline() 的行为与我使用 .比萨的普通实例上的运算符(而不是指针),但是当我运行这个程序时,它会完全跳过公司名称提示。
// Parts of the program omitted.
struct Pizza {
string szCompany;
float diameter;
float weight;
};
Pizza* pz = new Pizza;
cout << "Enter the weight: ";
cin >> pz->weight;
cout << "Enter the company name: ";
// use getline because the company name can have spaces in it.
getline(cin, pz->szCompany);
cout << "Enter the diameter: ";
cin >> pz->diameter;
cout << "\nCompany name: " << pz->szCompany;
cout << "\nWeight: " << pz->weight;
cout << "\nDiameter: " << pz->diameter;
// cannot forget this step.
delete pz;
return 0;
【问题讨论】: