gpt4 book ai didi

c++ - 一个简单的 C++ While 循环不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:56:13 25 4
gpt4 key购买 nike

我有一个简单的 while 循环,我正在尝试实现,但我终究无法弄清楚我遗漏了什么。我在顶部将 currentuser 初始化为 -1

while(currentuser = -1){
cout << "Enter user ID: ";
cin >> id;
currentuser = search(a, length, id);
}

我的搜索功能是这样的:

int search (User a[ ], int length, string userID){
User u;
string tempid;
int templegnth; //I ignore length for now as I will use it later
for(int i=0; i<50; i++){
tempid = a[i].getID();
templegnth = tempid.length();
if((tempid == userID)){
return i;
}
}
return -1;


}

我知道这很简单,但我现在想不出答案。

最佳答案

=(赋值)运算符与 ==(相等)运算符不同。

行:

while(currentuser = -1){

首先将-1赋值给currentuser,然后检查currentuser是否有非零值。情况总是如此 (-1 != 0),因此循环永远不会结束。

你可能是这个意思:

while(currentuser == -1){

它将 currentuser-1 进行比较,并在比较为真时继续循环。

关于c++ - 一个简单的 C++ While 循环不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7546023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com