gpt4 book ai didi

c++ - 双数程序意外值

转载 作者:行者123 更新时间:2023-11-27 23:07:59 24 4
gpt4 key购买 nike

关于计划

该程序采用用户输入的数字并将该数字加倍输出。我创建了两个函数,一个收集数字 (getnumber),另一个将数字加倍 (doublenumber)。该程序确实可以正常工作;但是,输出并不完全准确。

问题

输出只是部分正确。即用户输入 50,值加倍,输出应该是 100。相反,值输出为 100114。只有前几个数字似乎是我想要的。

源代码:

#include <iostream>

void doublenumber(int&);
void getnumber(int&);

int main() {

int value;

getnumber(value);
doublenumber(value);

std::cin.get();
std::cin.get();


return 0;
}

void doublenumber(int &refvar) {

refvar*= 2;
std::cout << "\nThe value you entered doubled is: " << refvar << '.\n';

}

void getnumber(int &userNum) {

std::cout << "\nEnter a number to double: ";
std::cin >> userNum;
}

最佳答案

std::cout << "\nThe value you entered doubled is: " << refvar << '.\n';
^^^^^
|
multicharacter literal

它是一个多字 rune 字,类型为 int

C++11 §2.13.2 Character literals

A character literal is one or more characters enclosed in single quotes, as in ’x’, optionally preceded by the letter L, as in L’x’. A character literal that does not begin with L is an ordinary character literal, also referred to as a narrow-character literal. An ordinary character literal that contains a single c-char has type char, with value equal to the numerical value of the encoding of the c-char in the execution character set. An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal has type int and implementation-defined value.

查看这篇文章:Why does this code with '1234' compile in C++? .

关于c++ - 双数程序意外值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22049889/

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