Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 搜索floop在第一次通过时有效,但在第二次通过时无效_C++_Loops - Fatal编程技术网

C++ 搜索floop在第一次通过时有效,但在第二次通过时无效

C++ 搜索floop在第一次通过时有效,但在第二次通过时无效,c++,loops,C++,Loops,我使用这个函数来搜索用户输入的数字,即x。 其思想是,用户输入马拉松赛跑者的号码,程序输出该号码所属的跑步者的信息。 如果用户输入0,循环也应该结束 问题是,它要么只在第一次工作,而不是第二次,要么根本不工作,只显示runner[0]的信息 这是关于西班牙人的密码 void search(race *runner){ int flag = 1; int x; while (flag = 1){ cout << "Porfavor entre

我使用这个函数来搜索用户输入的数字,即x。 其思想是,用户输入马拉松赛跑者的号码,程序输出该号码所属的跑步者的信息。 如果用户输入0,循环也应该结束

问题是,它要么只在第一次工作,而不是第二次,要么根本不工作,只显示runner[0]的信息

这是关于西班牙人的密码

void search(race *runner){

    int flag = 1;
    int x;
    while (flag = 1){

        cout << "Porfavor entre el numero del corredor, si desea terminar la busqueda entre el numero 0 "<<endl;
        cin >> x;
        cout << endl;
        for (int i = 0; i < size-1; i++){

            if (x = runner[i].number)
                cout << "El corredor que usted busca es " <<
                runner[i].name << " con el numero " << runner[i].number<<endl;

            if (x = 0)
                flag = 0;

            }

        }


    }

除了明显的改变之外

while (flag = 1){

改变

        if (x = runner[i].number)

通过移动到检查x是否等于0的位置,可以避免代码中一些不必要的工作


现在它应该可以工作了,实际上你把=和==混淆了,首先学习如何比较两个元素

void search(race *runner){

    int flag = 1;
    int x;
    while (flag == 1){

        cout << "Porfavor entre el numero del corredor, si desea terminar la busqueda entre el numero 0 "<<endl;
        cin >> x;
        cout << endl;
        for (int i = 0; i < size-1; i++){

            if (x == runner[i].number)
                cout << "El corredor que usted busca es " <<
                runner[i].name << " con el numero " << runner[i].number<<endl;

            if (x == 0)
                flag = 0;

            }
        }
    }

代码中有一些简单的错误。您在代码中的三个位置使用了=而不是==,即

while (flag = 1)

if (x = runner[i].number)

if (x = 0)
把这些换成

while (flag == 1)

if (x == runner[i].number)

if (x == 0)
你要做的是,你给他们分配这些值,而不是检查是否相等。这是许多人经常犯的错误,可能会变得非常混乱,有时可能会导致无限循环。因此,您应该在开始时检查类似的错误

希望这能解决您的问题。

您的打字错误。如果x=runner[i].number,则需要if x==runner[i].number,而不是if x=runner[i].number。您需要==而不是=。在while flag=1和if x=0中也有输入错误
void search(race *runner){

    int flag = 1;
    int x;
    while (flag == 1){

        cout << "Porfavor entre el numero del corredor, si desea terminar la busqueda entre el numero 0 "<<endl;
        cin >> x;
        cout << endl;
        for (int i = 0; i < size-1; i++){

            if (x == runner[i].number)
                cout << "El corredor que usted busca es " <<
                runner[i].name << " con el numero " << runner[i].number<<endl;

            if (x == 0)
                flag = 0;

            }
        }
    }
while (flag = 1)

if (x = runner[i].number)

if (x = 0)
while (flag == 1)

if (x == runner[i].number)

if (x == 0)