不会为循环周期添加从其他函数获得的内容 [英] Won't add what it got from the other function each for loop cycle
问题描述
#include < stdio.h >
// 将字母等级转换为数字的功能
float GradePoints( char 等级)
{
float gP;
开关(等级)
{
案例 ' A':
case ' a':
gP = 4 。 0 ;
break ;
case ' B' :
case ' b ':
gP = 3 。 0 ;
break ;
case ' C' :
case ' c ':
gP = 2 。 0 ;
break ;
case ' D' :
case ' d ':
gP = 1 。 0 ;
break ;
case ' F' :
case ' f ':
gP = 0 。 0 ;
break ;
}
return gP;
}
// 计算平均成绩点的函数
float Gpa( char grade [], char hours [], int arrLength)
{
// gradepoint *课堂上的时间
float totalGpHours = 0 跨度>;
// 课堂上的时间
float totalHours = 0 ;
// 最终成绩点平均值
float gradePointAverage;
int i;
for (i = 0 ; i< arrLength; ++ i )
{
// 在每个班级中添加用户输入时间以获得课堂上的总时间。这是分母
totalHours = hours [i] + totalHours;
// 将该课程的班级和成绩等级中的小时数相乘,并将产品添加到totalGpHours。这是分子
totalGpHours = totalGpHours +(GradePoints(grades [i])* hours [i]);
}
// 计算平均成绩点
gradePointAverage = totalGpHours / totalHours;
return gradePointAverage;
}
int main()
{
// 因为null
char 成绩[ 6 ];
int classTime [ 5 ];
int i;
// 向用户询问字母等级
printf(< span class =code-string> 请输入您收到的字母等级(大写字母,无空格):\ n) ;
// 得到输入加一个数组成绩
fgets(成绩, 6 ,stdin);
// 询问用户每个班级的时间
printf ( 请输入您在每门课程中花费的时间(以小时为单位));
for (i = 0 ; i< 5 ; ++ i)
{
// 添加一个给i,这样对于用户来说看起来很正常,你不会从0开始
printf( \ n课程%d:,i + 1 );
scanf( %d,& classTime [i]);
}
printf( \ n跨度>);
printf( 您的平均成绩是:%f\\\
\ n ,Gpa(成绩,classTime, 5 ));
getchar();
return 0 ;
}
我的尝试:
一切。如果我得到4个第一个周期和4个下一个周期,它应该添加它们,但它不会。
你应该学会尽快使用调试器尽可能。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。
调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。
调试器 - 维基百科,免费的百科全书 [ ^ ]
掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]
调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个bug。
我会告诉你相关的一行:
float Gpa(char grade [], char hours [],int arrLength)
// ...
char grade [6];
int classTime [5];
// ...
Gpa(成绩,班级时间,5)
你现在看到了吗?
Gpa
函数需要三个参数char []
,char []
和int
。但是你传递char []
,int []
和int
。
编译器也应该识别这一点并抛出警告或错误。如果你没有得到这样的编译器消息,请增加编译器的警告级别(例如,使用-Wall with GCC和/ Wall with MS compilers)。
要解决这会将您的Gpa
函数更改为
float Gpa(char grades [],int hours [],int arrLength)
#include <stdio.h>
//function to convert letter grade into a number
float GradePoints(char grade)
{
float gP;
switch (grade)
{
case 'A':
case 'a':
gP = 4.0;
break;
case 'B':
case 'b':
gP = 3.0;
break;
case 'C':
case 'c':
gP = 2.0;
break;
case 'D':
case 'd':
gP = 1.0;
break;
case 'F':
case 'f':
gP = 0.0;
break;
}
return gP;
}
//function to calculate grade point average
float Gpa(char grades[], char hours[], int arrLength)
{
//gradepoint * time in class
float totalGpHours = 0;
//time in class
float totalHours = 0;
//final grade point average
float gradePointAverage;
int i;
for (i = 0; i < arrLength; ++i)
{
//add the user input time in each class to get total time in class. this is the denominator
totalHours = hours[i] + totalHours;
//multiplies hours in class and number grade from that class and adds the product to totalGpHours. this is the numerator
totalGpHours = totalGpHours + (GradePoints(grades[i]) * hours[i]);
}
//calculate grade point average
gradePointAverage = totalGpHours / totalHours;
return gradePointAverage;
}
int main()
{
//add one more element to the array because of the null
char grades[6];
int classTime[5];
int i;
//Ask user for letter grades
printf("Please enter the letter grades you received (upper-case letters, no spaces):\n");
//gets input plus one for the array grades
fgets(grades, 6, stdin);
//Ask user for time in each class
printf("Please enter the time you spent in each course(in hours)");
for (i = 0; i < 5; ++i)
{
//add one to i so it looks normal for the user and you don't start at course 0
printf("\n Course %d: ", i + 1);
scanf("%d", &classTime[i]);
}
printf("\n");
printf("Your grade point average is: %f\n\n", Gpa(grades, classTime, 5));
getchar();
return 0;
}
What I have tried:
everything. if i got 4 the first cycle and 4 the next, it's supposed to add them, but it doesn't.
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
I will show you the relevant lines:
float Gpa(char grades[], char hours[], int arrLength) // ... char grades[6]; int classTime[5]; // ... Gpa(grades, classTime, 5)
Did you see it now?
TheGpa
function expects three parameters of typechar[]
,char[]
, andint
. But you are passingchar[]
,int[]
, andint
.
The compiler should recognize this too and throw out a warning or error. If you did not got such compiler message, increase the warning level of the compiler (e.g. by using -Wall with GCC and /Wall with MS compilers).
To solve this change yourGpa
function to
float Gpa(char grades[], int hours[], int arrLength)
这篇关于不会为循环周期添加从其他函数获得的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!