C++ 我在读取变量、比较变量然后在使用循环时打印输出时遇到问题

C++ 我在读取变量、比较变量然后在使用循环时打印输出时遇到问题,c++,C++,我需要接受来自用户的两个数字,并且我需要将这些数字与之后他们将输入的数字列表进行比较。这是我的密码: #include <iostream> using namespace std; int main () { int i = 1, count, num1, num2, num3, n; bool check= false; cout << "Please, enter the first number: "; cin >> num1; cout <&

我需要接受来自用户的两个数字,并且我需要将这些数字与之后他们将输入的数字列表进行比较。这是我的密码:

#include <iostream>
using namespace std;
int main ()
{
int i = 1, count, num1, num2, num3, n;
bool check= false;

cout << "Please, enter the first number: ";
cin >> num1;
cout << "Please, enter your second number: ";
cin >> num2;
cout <<"-----------------------------"<<endl;
cout << "How many numbers you will enter? "<<endl;
cin >> count;

for (i; i<= count; i++)
{
    cout<<"Enter your #"<<i<<" number: ";
    do
    {
    cin >> num3;
    if (num1 == num3 && num2 == num3)
        {   
            n = 1;
            check = true;
        }
        else if (num1 == num3 && num2 != num3)
        {   
            n = 2;
            check = true;
        }
        else if (num1 != num3 && num2 == num3)
        {   
            n = 3;
            check = true;
        }
        else
        {
            n = 4;
            check = true;
        }
    }
    while (!check);
}
if (n == 1)
cout<<"Both "<<num1<<" and "<<num2<<" are found."<<endl;
else if (n == 2)
cout<<"Only the first number "<<num1<<" was found."<<endl;
else if (n == 3)
cout<<"Only the second number "<<num2<<" was found."<<endl;
else if (n == 4)
cout<<"Neither numbers "<<num1<<" and "<<num2<<" were found."<<endl;
return 0;
}
这里的问题是,它永远不会告诉我用户输入的两个数字是否都出现在他们输入的数字列表中。我可以让它告诉我是否输入了一个或另一个,但决不能同时输入两个。如何解决此问题,以便在找到这两个数字后,它将在末尾打印该数字?

在for循环中:


fori=0;这里的问题是,当你发现一个与num1或num2不匹配的数字时,你一直在重置n。如果输入5和10作为要查找的数字,输入5和1作为要比较的数字,则会找到5,将n设置为2,然后当您选中1时,它不匹配,因此将n设置为4。您需要的是两个bool变量,一个用于查找num1,另一个用于查找num2

您也不需要do..while循环。它不会做任何事情,因为你只会循环一次。通过以上两个更改,代码可以压缩为

#include <iostream>
using namespace std;
int main ()
{
    int i = 1, count, num1, num2, num3;
    bool found1 = false, found2 = false;

    cout << "Please, enter the first number: ";
    cin >> num1;
    cout << "Please, enter your second number: ";
    cin >> num2;
    cout <<"-----------------------------"<<endl;
    cout << "How many numbers you will enter? "<<endl;
    cin >> count;

    for (; i<= count; i++)
    {
        cout<<"Enter your #"<<i<<" number: ";
        cin >> num3;
        if (num3 == num1)
            found1 = true;
        if (num3 == num2)
            found2 = true;
    }
    if (found1 && found2)
        cout<<"Both "<<num1<<" and "<<num2<<" are found."<<endl;
    else if (found1)
        cout<<"Only the first number "<<num1<<" was found."<<endl;
    else if (found2)
        cout<<"Only the second number "<<num2<<" was found."<<endl;
    else 
        cout<<"Neither numbers "<<num1<<" and "<<num2<<" were found."<<endl;
    return 0;
}
你也应该养成为循环写东西的习惯,比如

for (int i = 0; i < count; i++)
而不是

for (; i<= count; i++)

第一种情况是标准循环,因为它是访问数组的方式,因为数组是0索引的。它还将我的作用域保持在for循环上,因为在循环之外不需要它,所以它应该在那里。您希望尽可能地保持变量的局部性,这样您就不会因为不需要的变量而使作用域变得混乱。

程序逻辑似乎有缺陷,即使没有,也没有必要使此任务过于复杂化。以下是一个更简单的方法:

#include <iostream>
using namespace std;
int main ()
{
    int count, num1, num2, num3;

    cout << "Please, enter the first number: ";
    cin >> num1;
    cout << "Please, enter your second number: ";
    cin >> num2;
    cout << "-----------------------------" <<endl;
    cout << "How many numbers you will enter? " <<endl;
    cin >> count;

    bool if_1_in = false; // if the first seen
    bool if_2_in = false; // if the second seen

    for (int i = 0; i != count; ++i)
    {
        cout << "Enter your #" << i+1 << " number: ";
        cin >> num3;
        if (!if_1_in || !if_2_in) { // if both are seen, we don't check anymore
            if (num1 == num3) // we set the flag if the first one is seen
            {   
                if_1_in = true;
            }
            if (num2 == num3) // we set the flag if the second one is seen
            {   
                if_2_in = true;
            }
        }
    }
    if (if_1_in && if_2_in) // both are seen
    cout<<"Both "<<num1<<" and "<<num2<<" are found."<<endl;
    else if (if_1_in)       // just the first one is seen
    cout<<"Only the first number "<<num1<<" was found."<<endl;
    else if (if_2_in)       // just the second one is seen
    cout<<"Only the second number "<<num2<<" was found."<<endl;
    else                    // neither one is seen
    cout<<"Neither numbers "<<num1<<" and "<<num2<<" were found."<<endl;

    return 0;
} 
您甚至可以使用std::unordered_set使此程序在更一般的情况下有用:

#include <iostream>
#include <unordered_set>
using namespace std;
int main ()
{
    int count, num1, num2, num3;

    cout << "Please, enter the first number: ";
    cin >> num1;
    cout << "Please, enter your second number: ";
    cin >> num2;
    cout << "-----------------------------" <<endl;
    cout << "How many numbers you will enter? " <<endl;
    cin >> count;

    std::unordered_set<int> seen;

    for (int i = 0; i != count; ++i)
    {
        cout << "Enter your #" << i+1 << " number: ";
        cin >> num3;
        seen.insert(num3);
    }
    if (seen.find(num1) != seen.end() && seen.find(num2) != seen.end()) // both are seen
    cout << "Both " << num1 << " and " << num2 << " are found." <<endl;
    else if (seen.find(num1) != seen.end())                             // just the first one is seen
    cout << "Only the first number " << num1 << " was found." << endl;
    else if (seen.find(num2) != seen.end())                             // just the second one is seen
    cout << "Only the second number " << num2 << " was found." << endl;
    else                                                                // neither one is seen
    cout << "Neither numbers " << num1 << " and " << num2 << " were found." << endl;

    return 0;
} 

只是一个猜测,但是您不应该在for循环中写入输出吗?然后重置check和n变量?在任何情况下都可以将check设置为true,只需在初始化时将其设置为true,而不必触摸它。很难说您想要实现什么。我在声明时初始化了它,它可能不是最好的样式,但也不是错误。OP将其初始化为适当的值以进行计数迭代。是的,最好是将其设置为零,但您还需要修复条件。此代码与我的代码具有相同的行为。它只会将num3的最后一个值与我的前两个数字进行比较。我想将num3的所有输入值与我的两个数字进行比较。@Mohammed这将检查所有数字。如果你为num1和num2输入10和5,为count an输入4,那么1,2,10,5会说它找到了两个数字:@Mohammed这是两个不同的bool变量的用途。如果我们看到num3与num1匹配,那么我们将found1设置为true。我们从未取消设置它,因此在循环结束时,如果found1为真,我们知道在某个点num3与num1匹配。num2也会发生同样的情况。我们只需要记住我们是否找到了号码,这就是。非常感谢。我现在明白为什么我的逻辑不是那么好了。
#include <iostream>
#include <unordered_set>
using namespace std;
int main ()
{
    int count, num1, num2, num3;

    cout << "Please, enter the first number: ";
    cin >> num1;
    cout << "Please, enter your second number: ";
    cin >> num2;
    cout << "-----------------------------" <<endl;
    cout << "How many numbers you will enter? " <<endl;
    cin >> count;

    std::unordered_set<int> seen;

    for (int i = 0; i != count; ++i)
    {
        cout << "Enter your #" << i+1 << " number: ";
        cin >> num3;
        seen.insert(num3);
    }
    if (seen.find(num1) != seen.end() && seen.find(num2) != seen.end()) // both are seen
    cout << "Both " << num1 << " and " << num2 << " are found." <<endl;
    else if (seen.find(num1) != seen.end())                             // just the first one is seen
    cout << "Only the first number " << num1 << " was found." << endl;
    else if (seen.find(num2) != seen.end())                             // just the second one is seen
    cout << "Only the second number " << num2 << " was found." << endl;
    else                                                                // neither one is seen
    cout << "Neither numbers " << num1 << " and " << num2 << " were found." << endl;

    return 0;
}