开发者

check if any of the conditions that occur in the loop have been fulfilled c++

How would you write the following code with a for loop? i.e. how do you use a for loop to check if any of the conditions that occur in the loop have been fulfilled? I know there must be a way and I'm sure someone has probably asked this 开发者_C百科on SO but I wasn't quite sure how to phrase it. So if there is a duplicate, maybe you can point me in the right direction.

  string topping;
  cout << "Enter a topping ";
  cin >> topping;
  string toppings_offered[5] = {"onions", "bell peppers", "olives", "spinach", "tomatoes"};

  if ( (topping == toppings_offered[0]) || (topping == toppings_offered[1]) || (topping == toppings_offered[2]) || (topping == toppings_offered[3]) || (topping == toppings_offered[4]))
           cout << "yes";


A bit of logic theory:

All conditions satisfied is the inverse of one of the conditions not being satisfied.

In your for() loop, if one of the conditions is not satisfied, the answer is false. Otherwise, it's true.

However, I'm not convinced that you're asking the right question, because in your example, a topping can only match one of the toppings_offered, not all of them.


In C++0x:

#include <algorithm>
#include <iterator>
#include <string>

bool is_offered(const std::string& s)
{
    // look up table
    static const std::string toppingsOffered[] =
                                {"onions", "bell peppers", /* etc */ };

    const auto toppingsBegin = std::begin(toppingsOffered);
    const auto toppingsEnd = std::end(toppingsOffered);

    return std::find(toppingsBegin, toppingsEnd, s) != toppingsEnd;
}

if (is_offered())
    std::cout << "yes";    

In C++03:

#include <algorithm>
#include <string>

bool is_offered(const std::string& s)
{
    // look up table
    static const std::string toppingsOffered[] =
                                {"onions", "bell peppers", /* etc */ };

    const std::string* toppingsBegin = &toppingsOffered[0];
    const std::string* toppingsEnd =
                            toppingsBegin +
                                sizeof(toppingsOffered) / sizeof(std::string);

    return std::find(toppingsBegin, toppingsEnd, s) != toppingsEnd;
}

if (is_offered(topping))
    std::cout << "yes";

In C++03 with utilities:

#include <algorithm>
#include <cstddef>
#include <string>

template <typename T, std::size_t N>
T* begin(T (&array)[N])
{
    return *array[0];
}

template <typename T, std::size_t N>
T* end(T (&array)[N])
{
    return begin(array) + N;
}


bool is_offered(const std::string& s)
{
    // look up table
    static const std::string toppingsOffered[] =
                                {"onions", "bell peppers", /* etc */ };

    const std::string* toppingsBegin = begin(toppingsOffered);
    const std::string* toppingsEnd = end(toppingsOffered);

    return std::find(toppingsBegin, toppingsEnd, s) != toppingsEnd;
}

if (is_offered(topping))
    std::cout << "yes";


bool isok = false;
for(...)
{
    if(cond)
        isok = true;
}

if(isok)
    cout << "yes"


for (int i = 0; i < 5; i++)
    if (topping == toppings_offered[i])
    {
        cout << "yes";
        break;
    }

Will do what your asking. You don't need to check if ALL of them have been fulfilled. You need to check whether the topping entered is one of the ones offered. At least that's what your code implies.

Hope it helps!


int i;

for( i = 0; i < sizeof(toppings_offered) / sizeof(string); i++)
{
    if(topping == toppings_offered[i])
    {
       cout << "yes";
    }
}


Pseudocode:

  result = false
  for i = 1 to n do
     result = result or (input = target[i])
  if result then print("At least one matched")
  else then print("None matched")

Similar code can be used to see if all matched or at least one didn't match.


You mean if one of the conditions has been fulfilled. They can't all be fulfilled, although they can all be checked.

int i = 0;
for (; i < 5; ++i)
  if (topping == toppings_offered[i])
    break;
if (i < 5)
  cout << "yes";

Unchecked code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜