#include <iostream>
#include <cstdlib>
using namespace std;
struct chaff {
char dross[20];
int slag;
};
const int data_size = 2;
void setstr(chaff & ch);
void showstr(const chaff & ch);
int main()
{
chaff *ps = new chaff[data_size];
chaff *pa = new(ps) chaff[data_size];
for (int i = 0; i < data_size; i++)
{
setstr(pa[i]);
showstr(pa[i]);
}
delete[]pa;
system("pause");
return 0;
}
void setstr(chaff & ch)
{
char dr[20];
cout << "Please enter a dross: ";
if (cin >> dr)
strcpy(ch.dross, dr);
else {
cout << "Bye!\n";
exit(EXIT_FAILURE);
}
cout << "Please enter a slag: ";
if (cin >> ch.slag)
return;
else {
cout << "Bye!\n";
exit(EXIT_FAILURE);
}
}
void showstr(const chaff & ch)
{
cout << "Dross : " << ch.dross << endl
<< "Slag : " << ch.slag << endl;
}
第九章编程练习(3)
最新推荐文章于 2024-05-14 15:27:08 发布