Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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++ 这个程序有什么问题?为什么它不退出循环?_C++ - Fatal编程技术网

C++ 这个程序有什么问题?为什么它不退出循环?

C++ 这个程序有什么问题?为什么它不退出循环?,c++,C++,所以我正试图制作一个程序来确定这个数字是否是快乐数字。但是程序似乎没有退出循环,并且在输入数字后什么也没有发生 我是否遗漏了什么,或者我的程序做错了?(仅学习for循环、while循环、do while循环以及if语句,并且只能使用它们。) 我已经做了三天这个程序,但仍然不能找出这个程序的错误。如果你能告诉我哪里出了问题就好了。谢谢 #include <iostream> using namespace std; int main(){ int number, temp, cou

所以我正试图制作一个程序来确定这个数字是否是快乐数字。但是程序似乎没有退出循环,并且在输入数字后什么也没有发生

我是否遗漏了什么,或者我的程序做错了?(仅学习for循环、while循环、do while循环以及if语句,并且只能使用它们。)

我已经做了三天这个程序,但仍然不能找出这个程序的错误。如果你能告诉我哪里出了问题就好了。谢谢

#include <iostream>
using namespace std;

int main(){

int number, temp, count;
cout<<"please enter a number :";
cin>>number; // to ask for input

while(number != 1 and number != 4)
{
    while(number !=0)
    {
        number = number/10;
        count += 1;  //start to calculate how many digits are in the number
    }
    while(count!=0)
    {
        number == number + (temp/(10^(count-1))^2); // to add the square of the number
        temp = temp%(10^(count-1));
        count-=1;
    }
    if(count == 0)
    {
        temp = number; // to set temp as number after the program is over so it can run again if it is not done
    }
}

if(number == 1){
    cout<<"This is a happy number"; // to print result
}
else if(number == 4){
    cout<<"This is not a happy number"; // to print result
}


    return 0;
}
#包括
使用名称空间std;
int main(){
整数、温度、计数;
coutnumber;//请求输入
while(数字!=1和数字!=4)
{
while(数字!=0)
{
数字=数字/10;
count+=1;//开始计算数字中的位数
}
while(计数!=0)
{
number==number+(temp/(10^(count-1))^2);//添加数字的平方
温度=温度%(10^(计数-1));
计数-=1;
}
如果(计数=0)
{
temp=number;//在程序结束后将temp设置为number,以便在未完成时可以再次运行
}
}
如果(数字==1){

不能首先正确初始化变量, count是取一些任意值,然后计算带有此类垃圾值的位数将给出错误的答案,而且temp Variable的值未初始化,这也取垃圾值 并使用库下cpp中的pow(x,y)函数计算数字的幂,x^y不会计算cpp中的幂


如果您遇到任何错误,您可以使用gdb进一步调试代码

正如其他一些人在评论中指出的那样,程序的逻辑是不明确的,对于您描述的简单程序,我认为您不需要内部while循环。正如LoveTronic所指出的,回顾一些基本循环概念可能会有所帮助。但是,为了帮助你玩弄一个工作程序我修改了你的程序来做你在问题中所描述的

#include <iostream>
using namespace std;

int main() {
   int number;

   cout << "please enter a number :";
   cin >> number; // to ask for input

   while (true) {
      if ( number == 1 ) {
         cout << "This is a happy number\n";
         return 1;
      }
      else if ( number == 4 ) {
         cout << "this is not a happy number\n";
         return 1;
      }
      cout << "please enter a number :";
      cin >> number; // to ask for input
   }
   return 0; // this will never reach
}
#包括
使用名称空间std;
int main(){
整数;
cout>number;//请求输入
while(true){
如果(数字==1){

在C++中,CUT^不是指数,它是唯一的OR和.<代码>和 >与代码>和代码>不一样,因为我看不懂程序应该做什么。你的问题是被否决了。此外,while循环永远不会退出,直到满足条件,比如<代码>号码<代码>等于0。每个开发环境都有一个可用于控制程序执行的工具,如果需要,可以停止每一行,以便检查程序的状态(变量)在继续之前。调试器可能是你所能找到的最高效的工具。它可以将3天的搜索和一篇帖子变成几分钟的步履和精神痛苦的呻吟。还要注意编译器的警告。他们经常告诉你,你的代码在语法上是正确的,但在逻辑上是错误的UESTIABLE。实际上要小心使用该
pow
函数。它以浮点运算,可能会导致近似值在转换回整数时被截断为错误的数字。