fill-Arry函数不存储字符并输出符号

fill Arry function not storing characters and outputting symbols

本文关键字:输出 符号 字符 存储 函数 fill-Arry      更新时间:2023-10-16

我为类编写了这个程序。我在显示arry中的内容时遇到问题我得到的只是符号而不是字符,尽管我用教授提供的文件中的字符填充了它们。请帮我弄清楚这里有什么问题我明天需要

//  This program grades multiple choice exams1. Each exam consists of 20 questions. 
//Each question has one of four possible answers: A, B, C, or D.
#include <stdio.h>
#include<stdlib.h>
#include <math.h>
const int maxsize = 20;
int fillArry (FILE *fi,char arry[],const int maxsize);
double gradeExam (char arry[], char studarry[],const int maxsize);
void desplayResults (double right);
int main ()
{
    char arry[maxsize];
    char studarry[maxsize];
    int num,num1,i,right;
    FILE *fi,*sd;
    fi = fopen ("CorrectAnswers.txt","r");
    if ( fi == NULL )
    {
        printf( "Error opening input file.nn" );
        exit(1);
    }
    sd = fopen ("StudentAnswers.txt", "r");
    if ( sd == NULL )
    {
        printf( "Error opening input file.nn" );
        exit(1);
    }
    num = fillArry (fi,arry,maxsize);
    num1 = fillArry (sd,studarry,maxsize);
    for (i=0;i<maxsize;i++)
    {   
    printf("%cn",arry);
    }
    for (i=0;i<maxsize;i++)
    {   
    printf("%cn",studarry);
    }
    right = gradeExam (arry, studarry ,maxsize);
    printf("you got %i rightn", right);
    desplayResults (right);
    return 0;
}
int fillArry (FILE *fi,char arry[],const int maxsize)
{
    int u = 0;
    while((fscanf(fi,"%c",&arry)==1 )&& u< maxsize)
    {
        u++;    
    }

    return u;
}
double gradeExam (char arry[], char studarry[],const int maxsize)
{
    int i, right=0, wrong=0;
    for (i=1;i<=maxsize;i++)
    {
        if (arry[i]==studarry[i])
            right++;
        else
        {    
            printf ("question number %i :%c is wrong, the correct answer is %cnn",i,studarry,arry);
            wrong++;
        }
    }
    printf("nnumber of wrong answers: %in",wrong);
    return right;   
}
void desplayResults (double right)
{
    double res;
    res = (right/20)*100;
    printf("You got %s %.2lf on the examn","%",res);
    if (res<70)
    printf ("You failed the examn");
    else
    printf ("You passed the examn");
}

问题出在fscanf语句上。试试这个。

fscanf(fi,"%s",arry)

此外,为了显示数组内容,您必须这样做:

for(i=0; i<maxsize; i++)
{
     printf("%c",arry[i]);
}

EDIT1:我已经在我的一端验证了同样的东西,它正在工作。请检查CorrectAnswers.txt.文件的内容

第2版:我遇到问题了。它在您的打印声明中:

printf("问题编号%i:%c错误,正确答案是%c\n\n",i,studarry,arry);

请更正为:

printf("问题编号%i:%c错误,正确答案是%c\n\n",i,studarry[i],arry[i]);