gpt4 book ai didi

C++ 二进制条码赋值

转载 作者:行者123 更新时间:2023-11-28 02:08:10 24 4
gpt4 key购买 nike

我正在学习计算机科学入门类(class),并使用 C++。

我们的任务是创建不同的“条形码扫描器”——二进制、代码 39 和代码 48。这是二进制部分的代码,其中条形码由交替的黑白条纹组成,其中每个条纹要么宽要么宽狭窄的。宽条解释为 1,窄条解释为 0。

作业的指导方针是:

  • 读取一个数字 b 表示代码中的柱数(奇数 1 ≤ b ≤ 21 )
  • 一次读取 b 个字符
  • 每个字符要么是 w 表示宽,要么是 n 表示窄
  • 宽条为 1,窄条为 0
  • 将条码转换成对应的数字并显示数字

我觉得我的代码没问题,但它不会返回超过 6 位数字的任何内容。如果我输入 b 为 7 到 21 之间的任何值,它仍然要求输入那么多整数,但只打印其中的 6 个。

感谢您的帮助!

#include <iostream>

using namespace std;

int main(){
int b; //number of bars
int i = 0; //loop variables
int k = 0;
int l = 0;

cout << "Number of bars in code (between 1 and 21): " ;
cin >> b;
char arraychars[b]; //barcode as w's and n's
int arrayint[b]; //whole barcode as ints
while (b <= 21 && b >= 1) //b is in range
{
for (i=0; i<b; i++)
{
cout << "Enter character (w for wide, n for narrow): " << endl;
cin >> arraychars[i];
}
for (k=0; k<b; k++)
{
if (arraychars[k] == 'w')
arrayint[k] = 1;
else if (arraychars[k] == 'n')
arrayint[k] = 0;
else
break;
}
for (l=0; l<b; l++) //prints
cout << arrayint[l] ;
break; //exits loop
}
return 0;
}

最佳答案

main() 的前两行应该已经发出大量警告。关键是 C++ 中的数组是静态大小的,在这里,您创建了一个大小由 b 的值决定的数组,它之前没有被初始化。天知道编译器对此做了什么。将其替换为 std::vectorstd::string

关于C++ 二进制条码赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36703436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com