C++ primer plus 练习7.9
来源:互联网 发布:淘宝好评能修改吗 编辑:程序博客网 时间:2024/06/07 05:04
(提前声明,以下皆为我自己做题时所记录,方便以后查看,当然也希望大家一起来观看并指出我的错误。)
#include<iostream>
using namespace std;
const int c_nSLEN = 30;
struct student{
char fullname[c_nSLEN];
char hobby[c_nSLEN];
int nOoplevel;
};
int fnGetinfo(student sta[], int n);
void fnDisplay1(student st);
void fnDisplay2(const student* pst);
void fnDisplay3(const student* pst,int n);
int main()
{
cout << "Enter class size: ";
int nClassSize;
cin >> nClassSize;
while (cin.get() != '\n')
{
continue;
}
student* ptStu = new student[nClassSize];
int nEntered = fnGetinfo(ptStu, nClassSize);
for (int i = 0; i < nEntered; i++)
{
fnDisplay1(ptStu[i]);
fnDisplay2(&ptStu[i]);
}
fnDisplay3(ptStu, nEntered);
delete[] ptStu;
cout << "Done.\n";
return 0;
}
int fnGetinfo(student sta[], int n)
{
int i;
for (i = 0; i < n; i++)
{
cout << "Please enter your name: ";
cin.get(sta[i].fullname, c_nSLEN).get(); //cin.get遇到空行会设置失效位
if (cin.fail())
{
cin.clear();
while (cin.get() != '\n')
continue;
break;
}
cout << "Please enter your hobby: ";
cin.get(sta[i].hobby, c_nSLEN).get();
if (cin.fail())
{
cin.clear();
while (cin.get() != '\n')
continue;
break;
}
cout << "Please enter your ooplevel: ";
cin >> sta[i].nOoplevel;
while (cin.get() != '\n')
continue;
}
return i;
}
void fnDisplay1(student st)
{
cout << "Your name: " << st.fullname << endl
<< "Your hobby: " << st.hobby << endl
<< "Your ooplevel: " << st.nOoplevel << endl;
}
void fnDisplay2(const student* pst)
{
cout << "Your name: " << pst->fullname << endl
<< "Your hobby: " << pst->hobby << endl
<< "Your ooplevel: " << pst->nOoplevel << endl;
}
void fnDisplay3(const student* pst, int n)
{
for (int i = 0; i < n; i++)
{
cout << "Your name: " << (pst+i)->fullname << endl
<< "Your hobby: " << (pst+i)->hobby << endl
<< "Your ooplevel: " << (pst+i)->nOoplevel << endl;
}
}
- 【C++】primer plus 练习7.1
- 【C++】primer plus 练习7.2
- 【C++】primer plus 练习7.3
- 【C++】primer plus 练习7.5
- 【C++】primer plus 练习8.1
- C Primer Plus 练习p31
- C Primer Plus 练习 P59
- C Primer Plus 练习 P85
- c primer plus 练习一
- C++primer plus第六版课后编程练习答案7.9
- C++ primer plus 练习7.9
- C Primer Plus 练习6-1
- C Primer Plus 练习 6-2
- C Primer Plus 练习 6-3
- C Primer Plus 练习 6-4
- C Primer Plus 练习 6-5
- C Primer Plus 练习 6-6
- C Primer Plus 练习 6-9
- Picking Release Parameter
- openGL 学习1
- sgu-243 Broken Chessboard
- 《Linux/UNIX系统编程手册(上、下册)》
- 聘之有道面试读心术之读书笔记
- C++ primer plus 练习7.9
- 黑马程序员-java中堆和栈的区别
- mysql 远程数据库导入到本地数据库
- oracle外连接符号(+)的用法
- FLASH与EEPROM的区别
- C#list<T>泛型集合习题
- Divide Two Integers ——解题报告
- Eclipse中添加文档注释快捷键+自动添加注释
- 理解JavaScript作用域