【问题标题】:How can I take specific user inputs in an array?如何在数组中获取特定的用户输入?
【发布时间】:2019-06-02 13:45:36
【问题描述】:

我正在编写一个程序,通过加倍 (link to wikihow article) 将二进制数转换为十进制数。

如果用户输入不是 1 或 0,那么它不是二进制数,在这种情况下,我希望循环“中断”并说如下内容:

“糟糕!二进制数只有 1 或 0”。

如果不是“则”循环应该继续。

那是我想编写类似的代码

for(int digits = 0; digits != digitsINbinNum; ++digits){
      if(a condition that checks if user input is anything else than 1 or 0){
         coût << ""Oops! Binary numbers have only 1 or 0" << endl; 
         break;
          }else{
        cin >> binArray[digits];/*<-----------Here's the part where I am trying to do that*/
      }
    }


有关详细信息,请参阅下面给出的代码:

#include <iostream>
#include <iterator>

using namespace std;

int main(){
    int digitsINbinNum;
    cout << "If you don't mind. Please enter the number of digits in your binary number: ";
    cin >> digitsINbinNum;
    int binArray[digitsINbinNum];

    cout << "Enter the binary number: ";
    for(int digits = 0; digits != digitsINbinNum; ++digits){
        cin >> binArray[digits];/*<-----------Here's the part where I am trying to do that*/
    }

/*using the doubling method as found in wikihow.com*/
    int total = 0;
    for(int posiOFdigit = 0; posiOFdigit != sizeof(binNum[noOFdigits]); posiOFdigit++){
        total = total * 2 + binNum[posiOFdigit];
    }

    /*Printing the number*/
    cout << "Decimal form of ";
    for(int n = 0; n != noOFdigits; n++){
        cout << binNum[n];
    }
    cout << " is " << total;
    return 0;
}

【问题讨论】:

  • 改用std::vector怎么样?
  • int binArray[digitsINbinNum]; 不是标准 C++。
  • @JVApen 我还没学会。但是谢谢你的建议。
  • @πάνταῥεῖ 我使用的是 c++ 14,我的意思是数组的大小将由用户给出。
  • 这对于 c++14 没有改变。 VLA 没有成为标准。您充其量使用的是编译器扩展。

标签: c++ arrays c++11 c++14


【解决方案1】:

二进制数转十进制数的逻辑可以参考题中给定的link

修改给定的代码,使其尽可能接近问题的参考代码。

注意:由于 ISO C++ 禁止变长数组,我正在更改 int binArray[digits]int *binArray = (int *)malloc(sizeof(int) * digitsINbinNum);。 这种修改使它成为一个整数指针,并在运行时获得所需大小的内存。

#include <iostream>

using namespace std;

int main(){
    int digitsINbinNum,
        /* variable to keep decimal conversion of given binary */
        decimal_val = 0;
    bool is_binary = true;
    cout << "If you don't mind. Please enter the number of digits in your binary number: ";
    cin >> digitsINbinNum;
    /*
     ISO C++ forbids variable length array,
     making it int pointer and allocating dynamic memory
    */
    int *binArray = (int *)malloc(sizeof(int) * digitsINbinNum);
    if (binArray == NULL)
    {
        cout << "Memory allocation failure" << endl;
        exit -1;
    }
    cout << "Enter the binary number: ";
    for(int digits = 0; digits != digitsINbinNum; ++digits){
        cin >> binArray[digits];
        /*<-----------Here's the part where I am trying to do that*/
        /* doubling method logic for conversion of given binary to decimal */
        if ((binArray[digits] == 0) ||
            (binArray[digits] == 1))
        {
            decimal_val = (decimal_val * 2) + binArray[digits];
        }
        else /* not a binary number */
        {
            is_binary = false;
            cout << "Oops! Binary numbers have only 1 or 0" << endl;
            break;
        }
    }

    /* if conversion is successful: print result */
    if (is_binary)
    {   
        cout << "Decimal Value for given binary is: " << decimal_val << endl;
    }
    if (binArray)
    {
        free(binArray);
    }
    return 0;
}

【讨论】:

  • warning: ISO C++ forbids variable length array ‘binArray’ [-Wvla]
  • 你会编辑这个答案来解释它是如何回答这个问题的吗?有同样问题的其他用户喜欢这种材料,因为它可以帮助他们学习。
  • @phinz 感谢您指出这一点。我已修改答案以采用 ISO C++ 标准。
  • @halfer 我在代码中添加了解释和内联 cmets,使其更加清晰和不言自明。感谢您指出。
  • malloc 在 C++ 中没有对应的 free 在某种程度上比 VLA 更糟糕。
【解决方案2】:

你不需要一个数组。这是一个简单的解决方案:

#include <iostream>

int main(){
    int digitsINbinNum;
    std::cout << "If you don't mind. Please enter the number of digits in your binary number: ";
    std::cin >> digitsINbinNum;

    std::cout << "Enter the binary number: ";
    int ret = 0;
    for(int digits = 0; digits != digitsINbinNum; ++digits) {
        int bin;
        std::cin >> bin;
        if (bin == 1 || bin == 0) {
            ret = 2 * ret + bin;
        } else {
            std::cout << "Oops! Binary numbers have only 1 or 0" << std::endl;
            return -1;
        }
    }
    std::cout << ret << std::endl;

    return 0;
}

【讨论】:

  • 你能解释一下return -1在做什么吗?
  • 这仍然存在 OP 代码中的错误,其中std::cin &gt;&gt; bin; 将读取整个数字并将其解释为十进制整数,而您只需要第一个数字。 Proof
  • @JayedYeameen 从main 返回非 0 值通常表示运行时错误。
  • @AlexanderZhang 用户输入必须是 0 或 1。因此用户必须在一行中输入每个数字,然后输入一个换行符。我就是这么理解这个问题的。
猜你喜欢
  • 2018-09-11
  • 1970-01-01
  • 2022-11-24
  • 2021-12-29
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 2021-08-15
  • 1970-01-01
相关资源
最近更新 更多