要求輸入的name可含空格,不知道錯哪了,新手求好心人解答,萬分感謝!
#include
#include
using namespace std;
struct donation{
string name;
double number;
};
int main()
{
cout<<"Input the number of donater: ";
int d_number;
cin>>d_number;
donation * p = new donation[d_number];
for(int i = 0; i < d_number; i++)
{
cout<<"Input the name of the donater.";
getline(cin,p[i].name);
cout<<"Input the number.";
cin>>p[i].number;
}
for(int i = 0; i < d_number; i++)
cout<<p[i].name<<" "<<p[i].number<<endl;
return 0;
}
前一次輸入的換行符沒有清掉,導致getline函數讀到換行符之後就返回!可以在調用getline函數之前,調用cin.get()把換行符清掉,當然也可以用其他的方法!