c++高分,完善代码

来源:百度知道 编辑:UC知道 时间:2024/05/29 23:54:30
帮我完善一下我的代码,多考虑一些用户输入错误的情况,谢谢。
#include<iostream>
#include<stdlib.h>
using namespace std;
int input_and_max(int n);

void main(void)
{
int n;
int maxint;

cout<<"Give the number of values you are going to input"<<endl;
cin>>n;

maxint=input_and_max(n); // the function input_and_max is called here
cout<<endl<<"The maximum value of the "<<n<<" input integers is "<<maxint<<endl;

}

int input_and_max(int n)
{
int *pnum,maxint;
if(!(pnum=new int[n]))
{
cout<<"Not enough free memory, programme stopped"<<endl;
exit(1);
}

int i=0;
while(i<n)
{
cout<<"type in value "<<(i+1)<<" and press return"<<endl;
if(!(cin>>pnum[i]))
{

#include<iostream>
#include<stdlib.h>
using namespace std;
int input_and_max(int n);

int main()
{
int n;
int maxint;
while(1)
{
cout << "Give the number of values you are going to input" << endl;
if(cin >> n)
{
maxint = input_and_max(n); // the function input_and_max is called here
break;
}
else
{
cerr << "your input is invalid!" << endl;
cin.sync();
cin.clear();
}
}
cout << endl <<"The maximum value of the " << n << " input integers is "<< maxint << endl;
return 0;
}

int input_and_max(int n)
{
int *pnum, maxint;
if(!(pnum = new int[n]))
{
cout << "Not enough free memory, programme stopped!" << endl;
exit(1);