C++练习中的一些问题
为什么输入两三个字符,可以继续,输入六七个字符的字符串就会失败呢?
#include<iostream>
#include<ctime>
struct donation
{
char name[20];
double dons;
};
int main()
{
int ps;
std::cout << "Enter the numbers of donation:";
std::cin >> ps;
donation * donor = new donation[ps];
for (int i = 0; i < ps; i++)
{
std::cout << "Enter the #" << i+1 << " donator's name:";
std::cin.getline((donor + i)->name,20);
//std::cin.clear();
std::cin.get();
std::cin.get();
//std::cin.get();
std::cout << "Enter the #" << i+1 << " donations:";
std::cin.get();
std::cin.get();
std::cin>>((donor + i)->dons);
}
std::cout << "Grand Patrons:" << std::endl;
int count = 0;
int i = 0;
while (i<=ps)
{
if ((donor + i)->dons > 10000)
{
std::cout << (donor + i)->name[20] << " " << (donor + i)->dons << std::endl;
count++;
}
i++;
}
delete[] donor;
int secs = 10;
clock_t delay = secs * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock()-start<delay)
{
;
}
std::cin.get();
std::cin.get();
return 0;
}