1.描述问题:空格键造成cin不会显示其它好理解,但是循环3中为什么只显示了name,而没有volume和price?
2.代码:
// structur.cpp -- a simple structure
#include <iostream>
#include <cstring>
struct inflatable // structure declaration
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std;
inflatable guest =
{
"Glorious Gloria", // name value
1.88, // volume value
29.99 // price value
}; // guest is a structure variable of type inflatable
// It's initialized to the indicated values
inflatable pal =
{
"Audacious Arthur",
3.12,
32.99
}; // pal is a second variable of type inflatable
// NOTE: some implementations require using
// static inflatable guest =
cout << "Expand your guest list with " << guest.name;
cout << " and " << pal.name << "!\n";
// pal.name is the name member of the pal variable
cout << "You can have both for $";
cout << guest.price + pal.price << "!\n";
// cin.get();
// my structure array test
inflatable guest_array[3];
for (int i = 0; i < 3; i++)
{
cout << "Please input name: " << endl;
// cin.getline(guest_array[i].name, 20);
cin >> guest_array[i].name;
cout << "Please input volume: " << endl;
cin >> guest_array[i].volume;
cout << "Please input price: " << endl;
cin >> guest_array[i].price;
cout << "name: " << guest_array[i].name << endl;
cout << "volume: " << guest_array[i].volume << endl;
cout << "price: " << guest_array[i].price << endl;
}
return 0;
}
3.截图