dev 这C++代码始终提供相同的输出
dev this C++ code always gives same output
这是一个程序,用于检查数字及其反向是否相等
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int num,n,digit,j,newNum;
n=num;
j=1;
newNum=0;
cout<<"Enter a numbern";
cin>>num;
while(n>=1)
{
digit=n%10;
n=n/10;
digit=digit*j;
newNum=newNum+digit;
j=j*10;
}
cout<<"The reverse of given number is:"<<newNum<<endl;
if(num==newNum)
cout<<"The given number and its reverse are equal";
else
cout<<"The given number and its reverse are not equal";
getch();
}
'该程序接受一个数字作为输入,然后找到它的反向,然后检查反向是否等于数字。 每当我运行这个程序以及我作为输入给出的任何数字时,它都会给出相反的数字1975492148。 谁能帮我确定它的原因?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int num;
cout<<"Enter a numbern";
cin>>num;
int n = num;
int rev = 0;
while( n >= 1 )
{
int rem = n%10;
rev = (rev*10) + rem;
n=n/10;
}
cout<<"The reverse of given number is:"<<rev<<endl;
if(num==rev)
cout<<"The given number and its reverse are equal" << endl;
else
cout<<"The given number and its reverse are not equal" << endl;
getch();
}
赋值n=num;
的行为未定义。这是因为此时尚未初始化num
。
然后,您将在程序的后面进一步使用n
作为您的while
条件。这不会有好下场。
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 为什么rk4.do_step不输出C++中的初始条件
- 输出没有重复元素的动态数组(收缩数组)C++
- 如何根据单词在文本中出现的概率输出单词