带有条件的C ++查询 [英] C++ query with condtions

查看:123
本文介绍了带有条件的C ++查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有n个案例,如果给定案例中的任何一个满足我的标准,我想要在给出所需答案之前检查,然后命令得到执行,否则

with if(case 1 || ....直到案例n)

这种类型的东西???

C ++



我尝试过什么:



没有什么可以想到的,为了更加...........

i have n cases that i want to check before giving desired answer if any 1 of given cases satisfies my criteria then command gets executes else not
with if(case 1||....till case n)
such type thingy???
C++

What I have tried:

nothing is coming to mind tried while with for togrther ...........

推荐答案

根据用户输入的检查次数,你不能使用静态的...

你需要一个函数来检查任意值,a循环运行在用户输入上并调用该函数...

为函数失败的第一个值你放弃整个循环,否则运行你的其他函数...
As number of checks based on user input ,you can not use a static if...
You need a function that does the checking for any arbitrary value, a loop that runs over the user input and calls that function...
for the first value the function fails you drop the whole loop, otherwise run your other function...


// get user selection value (not sure how you plan to do this)
switch selectionValue
{
case 1:
    // do something
    break;
case 2:
    // do something else
    break;
case 3:
    // do yet another thing
    break;
default:
    // handle unknown value
    break;
}


如果你必须在运行时提供(并检查)一组允许的值,那么std::set [ ^ ]正是您所需要的:



If you have to provide (and check) a set of allowed values at runtime, then the std::set[^] is precisely what you need:

#include <set>
#include <iostream>

using namespace std;

int main()
{

  set<int> goodno;

  // add the allowed 'good' selections
  goodno.insert(3);
  goodno.insert(5);
  goodno.insert(-12);

  int i;
  cout << "please insert your choice" << endl;
  cin >> i;

  if ( goodno.find(i) != goodno.end())
  {
    cout << i << " is a good selection, we might proceed..." << endl;
  }
  else
  {
    cout << i << " unfortunaly is a bad selection..."  << endl;
  }
}


这篇关于带有条件的C ++查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆