我的代码中的数组有问题。有什么建议?

时间:2016-06-10 13:06:32

标签: c++ arrays

这是我的main.cpp的代码:

#include <iostream>

using namespace std;

int main()
{
    int input1[8];
    int input2[8];
    int output[8];

    cout << "Welcome to binary calculator!" << endl;
    cin >> input1[8];
    cin >> input2[8];
    if(input1[0]+input2[0]>1){
        output[8] = 0;
        if(input1[1]+input2[1]>1){
            output[7] = 0;
            cout << output[7] << output[8] << endl;
        }
    }else{
        output[8]=input1[8]+input2[8];
    }
    return 0;
}

我的问题是在输出的第8个元素应该等于input1的第8个元素和input2的第8个元素之和的行上。我试着放七个,因为我以为我记得那个数组的索引是如何工作的但是我错了,现在我被卡住了。

2 个答案:

答案 0 :(得分:2)

如果你试图操纵数组的第8个元素,应该使用input1 [7] / input2 [7]来访问它,因为数组索引总是从0开始。

请详细说明您面临的问题究竟是什么。

答案 1 :(得分:0)

你应该读取整数并操纵吗?

使用二进制运算符执行位操作或 twiddling &, |, ~, ^, +, and -

另请参阅:std::bitset,它有助于将数字视为位容器。

二进制算术在Stack Overflow答案中解释的概念太大了;在互联网上搜索“C ++二进制算术教程”。

相关问题