c++ - 如何在数组中获取特定的用户输入?
问题描述
我正在编写一个程序,该程序通过加倍将二进制数转换为十进制数(链接到 wikihow 文章)。
如果用户输入不是 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;
}
解决方案
通过加倍方法将二进制数转换为十进制数的逻辑可以参考问题中的给定链接。
修改给定的代码以使其尽可能接近问题的参考代码。
注意:由于 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;
}
推荐阅读
- r - 如何将图标/图像添加到图例
- apache-kafka - 保留 7 天的主题的 max.compaction.lag.ms
- python - Flask:Jsonify 响应,然后使用响应调用第三方 API - 怎么做?
- wordpress - 我在 wordpress 中创建了一个名为 aboutUs 的页面,并在 functions.php 存在但未显示的文件夹中创建了一个 aboutUs.php
- javascript - 更改组件的样式但在 javascript 中使用不同的元素
- unity3d - 统一集成 agora RTM SDK
- php - 使用循环将图像插入wordpress动态字段
- javascript - Youtube iframe 使用自定义控件/在页面加载时切换静音/取消静音
- javascript - 如果使用 mongodb 匹配精确值,则返回记录
- python - Jupyter notebook 无法启动,需要帮助