2014-10-30 47 views
0

我写了这个程序的类。我有一个问题,显示什么在arry 我所得到的是符号而不是一个字符,即使我从教授提供的文件中填充字符。请帮我弄清楚这里有什么问题,我明天需要它填写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"); 
} 
+0

为什么你不关闭打开的文件? – 2014-10-30 05:56:48

+0

你能提供输入文件的例子吗? – Marcin 2014-10-30 05:58:15

回答

0

问题在于你的fscanf语句。尝试一下。

的fscanf(FI, “%S”,ARRY)

此外,用于显示数组的内容,你必须做这样的:

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

EDIT1:我已经验证了同样的事情在我的结尾,它正在工作。请检查文件CorrectAnswers.txt的内容。

EDIT2:我得到了问题。这是在你的打印声明:

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

请其修正为:

的printf( “问题编号%I:%C是错误的,正确的答案是%C \ n \ n”,我,studarry [I],ARRY [1]) ;

+0

所有的计算都是正确的。然而它仍然输出符号而不是文件中的字符,这就是文件的组织方式。 – 2014-10-30 06:23:10

+0

一个 d 乙 Ç 一个 一个 d 乙 d Ç 一个 d 乙 一个 ç Ç d 乙 Ç 一个 – 2014-10-30 06:23:47

+0

谢谢你,这是真正有用的,现在它是我不得不改变为什么数据是在文件中组织的。我把它们全部放在一行中,没有空格,而不是每行一行。 – 2014-10-30 07:17:22