我目前正在编写“C++ Primer Plus”一书并进行一些编程练习。看来,我在使用 Xcode(4.3.3) 时遇到问题,因为以下代码无法正常工作:
#include <iostream>
#include <string>
struct car
{
std::string maker;
int year;
};
int main()
{
using namespace std;
cout << "How many cars do you wish to catalog? ";
int nCars;
(cin >> nCars).get();
car* aCars = new car[nCars];
for (int i = 0; i < nCars; i++)
{
cout << "\nCar #" << (i + 1) << endl;
cout << "Please enter the make: ";
getline (cin, (aCars + i)->maker);
cout << "\nPlease enter the year made: ";
(cin >> (aCars + i)->year).get();
}
cout << "Here is your collection: \n";
for (int i = 0; i < nCars; i++)
{
cout << (aCars + i)->year << " " << (aCars + i)->maker << endl;
}
delete [] aCars;
return 0;
}
问题是,我没有机会进入任何制造商。程序直接进入我必须输入年份的地方,即使我使用的是“(cin >> nCars).get();” 摆脱换行符。
我忽略了什么吗?
提前致谢!