Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        4年前关闭。
                                                                                            
                
        
我目前正在研究可从文本文件配置的测验程序。当我尝试使程序仅打印数组0到3时出现问题。对不起,如果我的问题是一个菜鸟问题。我是C的新手。我的代码有什么问题?

更新:

在将array [i ++]更改为array [j ++]之后,代码才能工作。对于第一个循环,它工作正常。但是在下一个循环中,存储在数组中的数据似乎被弄乱了。我也编辑了testc.txt内容,只是为了测试Paul Ogilvie建议的新随机数。

第一输出:

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


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

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中0x5B4EFB53(msvcr120d.dll)的未处理异常:0xC0000005:读取访问位置0xCCCCCCCC的访问冲突,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35102131/

10-12 00:20
查看更多