以下是用户输入拍摄对象数量的简单程序 - >等级(A,B,C等) - >程序计算并打印学生成绩单,包括所有cgpa。
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[10];
char grade[10];
float out[10];
int num;
float cgpa=0.0;
for(int x=0;x<num;x++)
{
printf("\nEnter the number of subjects? \n ");
scanf("%d",&num);
printf("\nEnter Student Name: \n");
scanf("%s",&string[x]);
printf("\nEnter Student Grade: \n");
scanf("%s",&grade[x]);
if(grade[x]>="A" || grade[x]>="a")
out[x]==4.0;
else if(grade[x]>="B" || grade[x]>="b")
out[x]==3.0;
else if(grade[x]>="C" || grade[x]>="c")
out[x]==2.0;
else if(grade[x]>="D" || grade[x]>="d")
out[x]==1.3;
else if(grade[x]>="F" || grade[x]>="f")
out[x]==0.1;
else
printf("You've entered a wrong grade");
}
cgpa+=out;
cgpa=cgpa/num;
printf("%s\n", string);
printf("%s\n", grade);
printf("%f\n", cgpa);
getch();
}
我的主要问题是我继续收到以下两个错误:
我得到的错误如下:
if(grade[x]>="A" || grade[x]>="a")
out[x]==4.0;
else if(grade[x]>="B" || grade[x]>="b")
out[x]==3.0;
else if(grade[x]>="C" || grade[x]>="c")
out[x]==2.0;
else if(grade[x]>="D" || grade[x]>="d")
out[x]==1.3;
else if(grade[x]>="F" || grade[x]>="f")
out[x]==0.1;
非法使用浮点错误就在这一行:
cgpa+=out;
答案 0 :(得分:1)
请参阅解决方案。您的原始解决方案存在很多问题,并且存在根本缺陷。上述评论强调了您所犯的大部分错误,但原始解决方案的主要缺陷是缺乏清晰的流程结构。
建议在您将来开始编码之前起草您的算法或流程
1. Grab user input for student name and number of subjects
2. For every subject
a. Get user to input grade
b. Check grade is valid
c. Add to cumulative GPA value
Until num_subjects is met
3. Print out Student Name, Num Subjects and his GPA (cgpa/num_subjects)
请参阅下面的示例解决方案,该解决方案遵循我在上面定义的流程。 我希望这能帮助你完成编程之旅:)。
#include <stdio.h>
// #include <conio.h> - Commented this out because this is MS Dos specific and makes solution non portable
#include <ctype.h>
int main(void)
{
int num_subjects;
char name[10];
char grade;
float cgpa=0.0;
int x;
// These do not need to be within your loop. Especially num_subjects
printf("\nEnter Student Name: \n");
scanf("%s", &name[0]);
printf("\nEnter the number of subjects? \n ");
scanf("%d", &num_subjects);
// I've replaced this with a while loop, because you need a continuous loop until a valid grade is required
while( x < num_subjects )
{
printf("\nEnter Student Grade: \n");
scanf("%c", &grade);
// Upper case the value, so there is no ambiguity in 'a' or 'A'
grade = toupper(grade);
printf("\nGrade Entered: %c\n", grade);
if (grade == 'A') {
cgpa+=4.0;
}
else if (grade == 'B') {
cgpa+=3.0;
}
else if (grade == 'C') {
cgpa+=2.0;
}
else if (grade == 'D') {
cgpa+=1.3;
}
else if (grade == 'F') {
cgpa+=0.1;
}
else {
printf("You've entered a wrong grade");
// Being lazy here. I'm decrementing the counter, because I am lazy.
// By right, the efficient thing to do is to increment the counter on a valid value
// But in the interest of writing less code, I've decided to decrement the value on an invalid value.
// And add more comments :P
x--;
}
// Increment x if a valid grade was entered.
x++;
}
// Final output line
printf("\nStudent: %s, Number Subjects: %d, GPA: %.2f", name, num_subjects, cgpa/num_subjects);
}
答案 1 :(得分:0)
您无法将整个数组添加到浮点数。
等级[x]是一个字符,而&#34; A&#34;是一个char数组。
此外,您必须先扫描您的号码,然后才能在for循环中使用它。
答案 2 :(得分:0)
改变这个: if(grade [x]&gt; =&#34; A&#34; || grade [x]&gt; =&#34; a&#34;) OUT [X] == 4.0;
为此: if(grade [x]&gt; =&#39; A&#39; || grade [x]&gt; =&#39; a&#39;) 出[X] = 4.0;
为什么呢? 原因1:成绩是一个字符数组,而不是字符串。 原因2:==是一个布尔运算符(如示例中的&gt; =)。要设置变量值,请使用just =
在编码前稍微研究一下。这是一个非常基本的问题。