Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++十六进制计算器运行时错误_C++ - Fatal编程技术网

C++十六进制计算器运行时错误

C++十六进制计算器运行时错误,c++,C++,我正试图让这东西正常工作,但我就是做不到。例如,它添加了六位小数,比如F+F=000000001E,我认为这是正确的,对吗?但如果我输入第一个十进制数1D后尝试加1D+1D,则不允许我输入第二个十进制数1D,并输出总和为00000000 2e #include <iostream> using namespace std; void output(char number[]); void hex_sum(char hex1[], char hex2[], char sum[]);

我正试图让这东西正常工作,但我就是做不到。例如,它添加了六位小数,比如F+F=000000001E,我认为这是正确的,对吗?但如果我输入第一个十进制数1D后尝试加1D+1D,则不允许我输入第二个十进制数1D,并输出总和为00000000 2e

#include <iostream>
using namespace std;

void output(char number[]);
void hex_sum(char hex1[], char hex2[], char sum[]);

int main()
{
  char answer;

  do
    {

      char hex1[10] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
      char hex2[10] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
      char sum[10] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};

      cout << "Enter the first hexadecimal number: \n";
      cin >> hex1[0];
      cout << "Enter the second hexadecimal number: \n";
      cin >> hex2[0];

      hex_sum(hex1, hex2, sum);

      cout << "The sum is: \n";

      for(int i = 9; i >=0; i--)
    {
      cout << sum[i];
    }

      cout << "\n" << "Would you like to try again? (Y/N)?\n";
      cin >> answer;

    } while(answer == 'Y' || answer == 'y');

    cout << "Good-bye! \n";

    return 0;
}

void hex_sum(char hex1[], char hex2[], char sum[])
{
  int x, y;
  int carry = 0;
  int other_carry = 0;

    for(int i = 0; i < 10; i++)
    {
        if('0' <= hex1[i] && hex1[i] < '0' + 10)
            x = hex1[i] - '0';

        else
            x = hex1[i] - 'A' + 10;

        if('0' <= hex2[i] && hex2[i] < '0' + 10)
            y = hex2[i] - '0';

        else
            y = hex2[i] - 'A' + 10;

        carry = other_carry;

    int z;

        z = (x + y + carry) % 16;
        other_carry = (x + y + carry) / 16; 

        if(0 <= z && z < 10)
            sum[i] = char('0' + z);

        else if(10 <= z && z < 16)
            sum[i] = char('A' + z - 10);
    }

    if(1 == carry && 1 == other_carry)
        cout << "Addition overflow.\n";
}
如果我从中删除[0]

cout << "Enter the first hexadecimal number: \n";
cin >> hex1[0];
cout << "Enter the second hexadecimal number: \n";
cin >> hex2[0];

它允许我输入第二个1D,但输出1D+1D的结果是00000000A2,而不是000000003A。如果我这样做的话,像F+F这样只由一个字母组成的十六进制的输出也是错误的。我得到的不是F+F=000000001E,而是000000000E。

因为您使用char作为数据类型,它读取您所写内容的前两个字母。在本例中为1和D,并将它们保存在两个不同的数组中。尝试改用字符串。

问题在于输入的方式,您使用的字符数组一次只能取一个值,因此当您键入2位字符时,它实际上会将第2位数字存储在第2个数组中,即hex2[0]。请尝试FF,它将为您提供与F+F相同的00000000 1E。要使其正确,您应该添加一个条件,即如果用户未键入两位数字符,则应将数组中的第二个元素设置为nulli.e。hex1[1]='\0'第二次输入也一样,希望它能帮助你。

如果你真的想输入数字,为什么要读取字符数组?检查I/O操纵器。