简体   繁体   中英

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. But "n1" becomes 0 after I get "num2". 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. If you try to read with cin a two character array (ie 1a ) you will have a undefined behaviour . This example might give you some hints about '\\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.

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 . Maybe you expect the user to input 01 instead.

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:

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);
    ...
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM