📄 newcerr.cpp
字号:
//这个程序在本书所带软盘中,文件名为NEWCERR.CPP
//这个程序利用cerr来处理动态地址字节分配时以及输入时的错误。
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
void main(void)
{
int size;
float sum,average;
char choice;
do {
cout << "please enter number of student in the class: ";
cin >> size;
cout << endl;
sum = 0;
float *score = new float[size]; //动态分配地址字节
if (score == NULL)
cerr << endl << "Error allocating memory! Try use smaller number..." << endl;
else if (cin.fail())
cerr << endl << "Error reading input! Try use right input..." << endl;
else {
for (int i = 0; i < size; i++) {
cout << endl << "Please enter student " << (i+1) << "'s score: ";
cin >> score[i];
sum += score[i];
}
average = sum/size;
delete score; //把地址空间释放回系统
cout << endl << "The average score of the class is: " << average << endl;
}
cout << "Do you want to compute another class's average?(y/n): ";
choice = getche();
cout << endl;
} while(toupper(choice) == 'Y');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -