读取文件并添加到数组字符
Read file and add to array char
#include<iostream>
#include<fstream>
#include<cctype>
#include<string>
using namespace std;
int main(){
ifstream fin;
int i,size,j=8,t=0;
char kunci[21]="TFFTFFTTTTFFTFTFTFTT";
string data;
fin.open("datajawaban.txt");
//getline(fin,data);
char jawab[31];
cout <<"ini isi file " <<data<<endl;
for(i=0;i<=31;i++){
fin >> jawab[i];
jawab[31] = '/0';
if(jawab[j]==kunci[i])t++;
cout <<"data ke "<<i <<" "<< jawab[j] << " - " <<kunci[i]<<endl;
j++;
}
cout << "jumlah benar "<< t;
fin.close();
system("pause");
return 0;
}
那是我的代码
输出为±
±`(v
☺
p ↨ v
P ☺ v
-
0 \ ± C
P ☺ v
这是错误的输出,我的代码有什么问题?
这次请帮帮我,这是我的作业
> jawab[31] = '/0';
是未定义的行为,因为数组大小为 31,数组索引从 0 开始,最大索引值可以是大小 - 1 对于jawab
数组为 30。
不仅如此,您的for
循环运行i<=31;
,您在循环kunci[i]
中索引i
i> 19 这是索引错误之外的另一个数组点。 -- 只需声明足够大的数组来纠正这两个错误。
相关文章:
- 指向指向字符数组的指针数组的指针
- 比较字符数组
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 使用无符号字符数组有效存储内存
- 按字符值访问int数组
- 错误:字符数组的初始值设定项太多
- 对字符数组中的元素执行逐位操作
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 移动二维数组中的字符
- C++ 传递二维字符数组
- 无法在 C++ 中输入字符数组
- C++ 带结构的数组字符
- 数组字符包含量超出预期
- 读取文件并添加到数组字符
- 如何在Qt中更改或替换数组字符
- 为什么编译器不在参数中传递数组字符 *arr[] 的大小?
- 井字棋数组字符错误
- 分割数组字符