c++ - 如何将 "warning message"添加到我的程序中?

标签 c++ arrays oop

我用 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/

相关文章:

c++ - 在构造函数中实现一个函数

javascript - 根据其他所有元素对 Javascript 数组进行排序

ruby - 将 %w 与对象中的字符串一起使用

php - PHP中的接口(interface)有什么意义?

c++ - '{' token c++ 继承问题之前的另一个预期类名

c++ - 将条件转换为算术表达式

c++ - 一个类的实例是否仍然在同一进程内的线程之间共享相同的静态成员?

c++ - Openmp:嵌套循环和分配

arrays - 我应该如何初始化这个多维数组?

java - 为什么我们有非静态函数/方法?