C++练习中的一些问题

cheliangzhang cheliangzhang 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
为什么输入两三个字符,可以继续,输入六七个字符的字符串就会失败呢?
#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;
}
...全文
评论 1
收藏 已收藏
分享
返回顶部
cheliangzhang
C++ 语言 65,214 成员 250,514 内容