首页 > 解决方案 > I cannot solve the else part in my if-else ladder?

问题描述

I am trying to write a simple program that iterates through the numbers in Vector to find the number the user asks for. I ask the user to add random numbers to a list (the vector). Then I ask them to enter the number they wish to find. I used a if else ladder to check if the Vector is empty before looping through it. Else I displayed :"The list is empty" I used a Range based for loop to loop through the numbers of the vector, then I tried comparing every number in the loop to the WantedNum. if a number in the loop==wantedNum, I assign it to wantedNum, then display WantedNum back to the user. My code displays the wanted number but it does not stop there like intended.

The case I can't solve is when the user asks for a number that's not in the list.

Well here's my code (I initialized the vector explicitly with bunch of random numbers until I can solve the "Find a number"part, then I'll move on to the "add a number" logic)

I am a beginner so I'd appreciate explanations, especially what have I done wrong.. Of course advice is most welcomed. Thank you in advice.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    char selection {};
    vector<int> numbers {10, 20, 30, 46, 78, 100, -2};
    int wantedNum {};

    cout << "Welcome to my menu: \n";
    cout << "Enter F to find a number.\nEnter P to display the numbers in the "
            "list.\nEnter Q to quit\n";

    do
    {
        cout << "Enter your choice: ";
        cin >> selection;
        switch (selection)
        {
            case 'f':
            case 'F':
                cout << "Enter the number you wish to find: ";
                cin >> wantedNum;
                if (numbers.size() != 0)
                {
                    for (auto i : numbers)
                    {
                        if (i == wantedNum)
                            wantedNum = i;
                    }
                    cout << "Here's your number: " << wantedNum << endl;

                    for (auto i : numbers)
                    {
                        if (i != wantedNum)
                        {
                            cout << "Searching..." << endl;
                        }
                    }
                    cout << "Your number is not in the list.\n";
                }
                else
                {
                    cout << "List is empty - []\n";
                }
                break;
            case 'p':
            case 'P':
                cout << "The numbers in the list are: ";
                for (auto i : numbers)
                {
                    cout << i << " ";
                }
                break;
            case 'q':
            case 'Q': cout << "GoodBye !\n"; break;
            default: cout << "I dont recognize this input, please try again.\n";
        }
    } while (selection != 'Q' && selection != 'q');
}

标签: c++

解决方案


下面的代码块

for (auto i : numbers) {
    if (i == wantedNum)
        wantedNum = i;
}
cout << "Here's your number: " << wantedNum << endl;

for (auto i : numbers) {
    if (i != wantedNum) {
        cout << "Searching..." << endl;
    }
}
cout << "Your number is not in the list.\n";

向我表明您不清楚逻辑。

你必须:

  1. 寻找项目。
  2. 如果找到该项目,则设置一些标志以指示该项目已找到。找到该项目时打破外观。找到项目后继续循环遍历数字没有任何作用。
  3. 在循环结束后适当地打印一条消息。

这是一个更新的版本

bool found = false;
for (auto i : numbers) {
    if (i == wantedNum)
    {
       found = true;
       break;
    }
}

if ( found )
{
   cout << "Found " << wantedNum << " in the list.\n"
}
else
{
   cout << "Did not find " << wantedNum << " in the list.\n"
}

您可以使用它std::find来稍微减少代码量。

bool found = (std::find(numbers.begin(), numbers.end(), wantedNum) != numbers.end());

if ( found )
{
   cout << "Found " << wantedNum << " in the list.\n"
}
else
{
   cout << "Did not find " << wantedNum << " in the list.\n"
}

推荐阅读