我用 C++ 为我的项目编写程序;但是,我无法在我的算法中添加“返回警告消息”。
我的算法;
#include<iostream>
#include<conio.h>
using namespace std;
const int k=100;
class safearay{
int arr[k];
int getel(int index){ if(index>-1 && index<k) return arr[index];}};
void main(void)
{
cout<<"-------------------------------------------------------------------------------\n"<<endl;
safearay safea1; int temp=23456;
for{
safea1.putel(7, temp); temp=safea1.getel(7);
cout<<temp;
cout<<"\n\n !Press k to continue."<<endl<<endl;
}while(getch()=='k');
}
如何添加警告消息部分?
最佳答案
一种方法是返回一个标志,说明 putel
函数出了问题,并在 main 中打印错误。
bool putel(int index, int value){
if(index <= -1 || index == 10 || index > LIMIT) {//the conditions that are invalid
return false;
}
arr[index]=value;
return true;
}
主要是这样的
do{
if(!safea1.putel(7, temp)){
cout<<"Insert failed "<<endl; //Your warning message
} else {
temp=safea1.getel(7);
cout<<temp;
cout<<"\n\n !Press k to continue."<<endl<<endl;
} while(getch()=='k');
我希望这就是您要找的..
关于c++ - 如何将 "warning message"添加到我的程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28602355/