fill-Arry函数不存储字符并输出符号
fill Arry function not storing characters and outputting symbols
我为类编写了这个程序。我在显示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]);
相关文章:
- Clang 给了我符号更改的警告,但代码仍然产生正确的输出
- 来自无符号字符缓冲区的虚拟输出
- 代码输出随机符号,我不确定出了什么问题
- 我没有得到一个数字作为输出,而是一个表情符号
- C ++中的输出字符数组会产生奇怪的符号
- 为什么printf和cout为此无符号int提供了不同的输出
- Linux Ubuntu 中的文件内输出 unicode 符号
- 为什么尽管变量是无符号整数,但该程序仍以 -25 给出输出
- 将怪异的符号打印到输出文件C
- 当程序与符号名称一起崩溃时,输出呼叫堆栈
- 程序将怪异的符号打印到输出文件C
- 未定义的符号:符号引用错误.没有输出写入主
- 无符号长长 - 奇怪的输出
- 未符号char响应[maxmem]的输出;`
- C:atoi()对未符号字符的输出输出
- gcc 预处理的表单哈希符号 + 数字"# 1"输出行是什么意思?
- 为什么我有一个"="符号输出和 2 个笑脸而不是正确的输出?C++
- 为什么当我放置定点符号时,我的输出是 0.00
- C++字符返回输出符号
- fill-Arry函数不存储字符并输出符号