[英]Using Array as parameter in function in C++
This program gets two hexadecimal numbers and converts them to decimal numbers. 该程序获取两个十六进制数字并将它们转换为十进制数字。 And it finally returns sum of two number in decimal form.
它最终以十进制形式返回两个数字的总和。 Before I enter "num2", "n1" gets right value.
在我输入“num2”之前,“n1”得到正确的值。 But "n1" becomes 0 after I get "num2".
但是当我得到“num2”后,“n1”变为0。 I don't know why this happens.
我不知道为什么会这样。 Please tell me the reason in hurry...
请快点告诉我原因......
#include <iostream>
using namespace std;
class HextoDec {
public:
void getNum();
int add();
private:
char num1[2], num2[2];
int convert(char num[]);
int n1, n2;
};
int main()
{
HextoDec a;
a.getNum();
cout << "Sum of two number is " << a.add() << endl;
}
void HextoDec::getNum() {
cout << "Enter first number : ";
cin >> num1;
n1 = convert(num1);
cout << "Enter second number : ";
cout << endl << n1 << endl; // Value of n1 is correct
cin >> num2;
cout << n1 << endl; // Problem occurs. Value of n1 becomes 0
n2 = convert(num2);
}
int HextoDec::convert(char num[]) {
int j = 16, n = 0;
for (int i = 0 ; i < 2 ; i++) {
switch (num[i]) {
case '0':
n = n + j * 0; break;
case '1':
n = n + j * 1; break;
case '2':
n = n + j * 2; break;
case '3':
n = n + j * 3; break;
case '4':
n = n + j * 4; break;
case '5':
n = n + j * 5; break;
case '6':
n = n + j * 6; break;
case '7':
n = n + j * 7; break;
case '8':
n = n + j * 8; break;
case '9':
n = n + j * 9; break;
case 'A':
n = n + j * 10; break;
case 'B':
n = n + j * 11; break;
case 'C':
n = n + j * 12; break;
case 'D':
n = n + j * 13; break;
case 'E':
n = n + j * 14; break;
case 'F':
n = n + j * 15; break;
}
j = 1;
}
return n;
}
int HextoDec::add() {
cout << "*****" << endl;
cout << n1 << endl;
cout << n2 << endl;
return n1 + n2;
}
What's the reason? 什么原因? What makes this happens?
是怎么回事? What can I do or should I do to solve this problem?
我该怎么做才能解决这个问题?
As other people have mentioned, your char
arrays only contain one element, plus the '\\0'
character. 正如其他人所提到的,你的
char
数组只包含一个元素,加上'\\0'
字符。 If you try to read with cin
a two character array (ie 1a
) you will have a undefined behaviour . 如果你尝试用
cin
读取一个双字符数组(即1a
),你将有一个未定义的行为 。 This example might give you some hints about '\\0'
这个例子可能会给你一些关于
'\\0'
提示
const char *example1 = "hi";
strlen(example1); // This is 2
const char *example1 = "hi\0";
strlen(example1); // This is also 2
But answering to your question, if you only want to read 2-digit hexadecimal values, char num1[3]
should fix the issue. 但回答你的问题,如果你只想读取2位十六进制值,
char num1[3]
应该解决问题。
However, I think you might have another issue. 但是,我想你可能还有另外一个问题。 Just copied/pasted your code and if I enter just
1
the result that I get is 16
, but it should be 1
. 只需复制/粘贴你的代码,如果我只输入
1
,我得到的结果是16
,但它应该是1
。 Maybe you expect the user to input 01
instead. 也许您希望用户输入
01
。
I guess this is a didactic example, so you might want to play a bit with the code, and for example be able to convert any number to decimal. 我想这是一个说教的例子,所以你可能想要对代码玩一点,例如能够将任何数字转换为十进制。 Maybe you can modify your function
convert
with something like: 也许你可以用以下方法修改你的函数
convert
:
int HextoDec::convert(char num[]) {
int n = 0;
int sizeNumber = strlen(num);
for (int i = sizeNumber - 1; i >= 0; i--)
{
int j = pow(16, sizeNumber - i - 1);
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.