为什么这个 while 循环突然停止工作
我正在用 C++ 实现链表来解决经典的快乐数字问题,但是这个 while 循环在中途停止工作,因为我是这门语言的新手,所以我不明白为什么会这样。代码如下:
#include<iostream>
#include "simplelists.h"
using namespace std;
int number() {
int x;
bool valid = false;
do {
cout << "Input the number to check if it's happy: ";
cin >> x;
if (cin.good() && x > 1) {
valid = true;
}
else { //to clear inputs
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\nInvalid input" << endl;
}
} while (!valid);
return x;
}
int main() {
int x = number();
int index;
int n = 0;
SimpleList* digits = new SimpleList();
while(true) {
index = x % 10;
n += index * index;
x /= 10;
cout << "hi " << index << " " << n << " " << x << endl;
if (n == 1) {
cout << "The number is happy! :)";
break;
}
else if (digits->search(n)->data != n) {
cout << "The number isn't happy :(";
break;
}
digits->InputAtEnd(n);
x = n;
cout << "hi " << x << " " << digits->search(n)->data << endl;
}
system("pause");
return 0;
}
具体来说,是 main() 中的 while 循环突然停止工作。我实际上正在打印 cout << “嗨”<<索引<< ” “<< n << ” “<< x <<结束;
和 cout << “嗨”<< x << ” “<<数字->搜索(n)->数据<< endl;
看看发生了什么,第一行只显示一次,而第二行根本不显示。
I'm doing the classic happy number problem on C++ implementing a linked list, but this while loop stops working mid-way through and since I'm new on this language I can't spot why is that. Here's the code:
#include<iostream>
#include "simplelists.h"
using namespace std;
int number() {
int x;
bool valid = false;
do {
cout << "Input the number to check if it's happy: ";
cin >> x;
if (cin.good() && x > 1) {
valid = true;
}
else { //to clear inputs
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\nInvalid input" << endl;
}
} while (!valid);
return x;
}
int main() {
int x = number();
int index;
int n = 0;
SimpleList* digits = new SimpleList();
while(true) {
index = x % 10;
n += index * index;
x /= 10;
cout << "hi " << index << " " << n << " " << x << endl;
if (n == 1) {
cout << "The number is happy! :)";
break;
}
else if (digits->search(n)->data != n) {
cout << "The number isn't happy :(";
break;
}
digits->InputAtEnd(n);
x = n;
cout << "hi " << x << " " << digits->search(n)->data << endl;
}
system("pause");
return 0;
}
Specifically it's the while loop from main() that suddenly stops working. I'm actually printingcout << "hi " << index << " " << n << " " << x << endl;
andcout << "hi " << x << " " << digits->search(n)->data << endl;
to see what's happening, and the first line is only displayed once while the second one isn't displayed at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Peter 评论的那样,问题在于
else if (digits->search(n)->data != n)
行,因为在我的 'simplelists.h' 类中,搜索方法返回 NULL if它没有找到任何数据,并且由于这是第一次迭代的情况,因此循环停止工作。为了解决这个问题,我将该条件更改为
else if (digits->search(n) != NULL)
。As Peter commented, the problem was the line
else if (digits->search(n)->data != n)
because in my 'simplelists.h' class the search method returned NULL if it found no data, and since that's the case for the first iteration, the loop just stopped working.To fix this, I changed that conditional to
else if (digits->search(n) != NULL)
.