fill数组函数不存储字符和输出符号

时间:2014-10-30 05:50:25

标签: c++ arrays

我为课程编写了这个程序。我在显示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.\n\n" );
        exit(1);
    }

    sd = fopen ("StudentAnswers.txt", "r");
    if ( sd == NULL )
    {
        printf( "Error opening input file.\n\n" );
        exit(1);
    }

    num = fillArry (fi,arry,maxsize);
    num1 = fillArry (sd,studarry,maxsize);
    for (i=0;i<maxsize;i++)
    {   
    printf("%c\n",arry);
    }

    for (i=0;i<maxsize;i++)
    {   
    printf("%c\n",studarry);
    }

    right = gradeExam (arry, studarry ,maxsize);
    printf("you got %i right\n", 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 %c\n\n",i,studarry,arry);
            wrong++;
        }

    }

    printf("\nnumber of wrong answers: %i\n",wrong);
    return right;   
}

void desplayResults (double right)
{
    double res;
    res = (right/20)*100;
    printf("You got %s %.2lf on the exam\n","%",res);
    if (res<70)
    printf ("You failed the exam\n");
    else
    printf ("You passed the exam\n");
}

1 个答案:

答案 0 :(得分:0)

问题在于你的fscanf语句。试试这个。

的fscanf(FI,&#34;%S&#34;,ARRY)

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

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

EDIT1 :我已经验证了同样的事情并且它正在运行。请检查CorrectAnswers.txt文件的内容。

EDIT2 :我遇到了问题。它在你的打印声明中:

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

请更正为:

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