我的程序打印出与预期不同的结果

时间:2017-04-12 04:14:46

标签: c++ operators operations

因此,每当我尝试输入“n”时,第一行将输出一个等于n + 1的数字。所以请帮助我做错了!谢谢

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS
#include "../../../std_lib_facilities.h"
int main()
{
cout << "Enter a number: ";
double n;
cin >> n;
cout << "n: " << n
    << "\n++n: " << ++n
    << "\nThree times of n: " << n * 3
    << "\nTwice n: " << n + n
    << "\nHalf of n: " << n / 2
    << "\nSquare root of n " << sqrt(n);
int m = n;
cout << "\nInteger of n: " << m
    << "\nRemainder when divide n by 2: " << m % 2
    << "\nDivision when divide n by 2: \n" << m / 2;
keep_window_open();
}

2 个答案:

答案 0 :(得分:1)

export KAFKA_HEAP_OPTS="-Xmx1G -Xms1G" export KAFKA_JMX_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=12346 -Dcom.sun.management.jmxremote.rmi.port=12346 -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false" bin/kafka-server-start.sh -daemon config/server.properties 更改了n++的值。你的其他表达都没有。你得到n这一事实很清楚,在输出所有其他“东西”之前,n+1正在被评估(因此n++已经递增)。

答案 1 :(得分:0)

您正在使用n并在同一表达式中执行n++(实际上会增加n)。这是undefined behavior