gpt4 book ai didi

c - 我在 myfirstproject.exe 中收到此错误 : Unhandled exception at 0x5B4EFB53 (msvcr120d. dll):0xC0000005:访问冲突读取位置 0xCCCCCCCC

转载 作者:行者123 更新时间:2023-11-30 19:40:40 25 4
gpt4 key购买 nike

我目前正在开发一个可通过文本文件配置的测验程序。当我尝试让程序只打印数组 0 到 3 时,问题就出现了。抱歉,如果我的问题是一个菜鸟问题。我是 C 新手。我的代码有什么问题?

更新:

将 array[i++] 更改为 array[j++] 后,代码可以运行。对于第一个循环,它工作得很好。但在下一个循环中,数组内存储的数据似乎困惑了。我也编辑了testingc.txt内容,只是为了测试Paul Ogilvie建议的新随机数。

第一个输出:

This is the first question
A.Really
B.No
C.Yes

当我输入 C 作为答案后,它打印出 Correct!和标记。但对于下一个循环,它给出以下输出:

Which one of this is an OS

s 10
B.Microsoft Word

代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
//Variables
int i, linepick, line = 1, found = 0, marks = 0, j = 0;
char chars[1000], answer[1000], questionfile[] = "C:\\Users\\cbtcode\\Desktop\\testingc.txt";

FILE *file;
file = fopen(questionfile, "r");

if (file == NULL) {
printf("Can't find question file.\n");
}
else {
while (fgets(chars, 1000, file))
{
linepick = rand() % 20 + 1;
for (i = 0; i < linepick; i++)
{
if (fgets(chars, 1000, file) == NULL)
{
break;
}
else
{
found = 1;
char *p;
char *array[1000];

p = strtok(chars, ";");
while (p != NULL)
{
array[j++] = p;
p = strtok(NULL, ";");
}
if (array != NULL)
{
printf("%s\n", array[0]);
printf("%s\n", array[1]);
printf("%s\n", array[2]);
printf("%s\n", array[3]);
scanf("%s", answer);

if (strcmp(answer, array[4]) == 0) {
printf("Correct!\n");
marks = marks + 10;
printf("Your Marks: %d%%\n\n", marks);
}
else {
printf("Wrong!\n");
marks = marks - 10;
printf("Your Marks: %d%%\n\n", marks);
}
}
else
{
printf("ERROR: Can't find the question!");
}
}
}
line++;
}
if (found == 0)
{
printf("ERROR: Line number %d was not found.", linepick);
}
}

我的testingc.txt内容:

This is the first question;A.Really;B.No;C.Yes;C;
This is the first question;A.Really;B.No;C.Yes;C;
Which one of these is an OS;A.Windows 10;B.Microsoft Word;C.Notepad;C;

最佳答案

问题出在下面的代码中:

            while (p != NULL) 
{
array[i++] = p;
p = strtok(NULL, ";");
}
printf("%s\n", array[0]);
printf("%s\n", array[1]);
printf("%s\n", array[2]);
printf("%s\n", array[3]);
scanf("%s", answer);

if (strcmp(answer, array[4]) == 0) {
printf("Correct!\n");
marks = marks + 10;
printf("Your Marks: %d%%\n", marks);
}

你怎么知道你在循环中分配了 array[1], array[2], array[3], array[4] 等。您在访问它们之前没有检查。如果未分配它们,它们将具有垃圾值,因为您尚未初始化数组。

此外,您正在执行array[i++] = p;。可能您需要使用不同的计数器 j。

根据OP的要求进行编辑:
试试这个代码:

       if (linepick == line) 
{
found = 1;
char *p = NULL;
char *array[1000] = {NULL};
int j = -1, k = 0;

p = strtok(chars, ";");
while (p != NULL)
{
j += 1:
array[j] = p;
p = strtok(NULL, ";");
}
if ( j >= 0 ) {
for (k=0; k < j && k < 4; k++) {
printf("%s\n", array[k]);

scanf("%s", answer);

if (j >= 4 && array[4] && (strcmp(answer, array[4]) == 0)) {
printf("Correct!\n");
marks = marks + 10;
printf("Your Marks: %d%%\n", marks);
}
else {
printf("Wrong!\n");
marks = marks - 10;
printf("Your Marks: %d%%\n", marks);
}
}
else {
printf("Wrong!\n");
marks = marks - 10;
printf("Your Marks: %d%%\n", marks);
}
}

关于c - 我在 myfirstproject.exe 中收到此错误 : Unhandled exception at 0x5B4EFB53 (msvcr120d. dll):0xC0000005:访问冲突读取位置 0xCCCCCCCC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35102131/

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