我正在尝试设计该程序,这总是给我错误的成绩输入错误有人可以帮助我找出错误在哪里吗?
谁能看到我犯了什么错误,并可能将我推向正确的方向?我感谢任何人的时间和建议:)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char str[100], ch;
int i, grade[20];
int p;
float credit[20], gpa = 0.0, totCredit = 0.0;
printf("SIKKIM MANIPAL INSTITUTE OF TECHNOLOGY\nMajhitar, Rangpo - 737136\n\nGPA CALCULATOR \n\n");
printf("PROGRAM CREATED BY: SHAMSH SAMEED ASHAN\n");
printf("FOR ANY INFO PLEASE CONTACT +91-9083454677\n\n");
printf("------*------*------*-----*------*------*------*------*------*------*------*------*------*------\n\n");
printf("NOTE:\n\n");
printf("1. Enter Grades in CAPITALS \n");
printf("2. Enter Grade and credit for the subjects in the following format:\n");
printf(" Example: |Subject 'n' (Grade|Credit):S4 |(If you enter S4 Here S is the grade and 4 is the credit)\n");
printf("3. If you have got 'F' grade then enter F followed by credit for that subject. \n 'F' grade subjects will not be used in calculation.\n\n");
printf("Enter Grade & Credits for each subject:\n\n");
printf("------*------*------*-----*------*------*------*------*------*------*------*------*------*------\n\n");
printf("Enter number of subject:");
scanf("%d",&p);
/* get the letter grade and credits from the user */
for (i = 0; i < p; i++) {
printf("Subject %d(Grade|Credit):", i + 1);
ch = getchar();
grade[i] = ch;
scanf("%f", &credit[i]);
getchar();
}
/* print the input grades and credits */
printf("\nSubject | Grade | Credit\n");
for (i = 0; i < p; i++) {
printf(" %d | %c | %.1f\n", i + 1, grade[i], credit[i]);
}
/* calculate gpa value */
for (i = 0; i < p; i++) {
switch (grade[i]) {
case 'S':
gpa = gpa + 10 * credit[i];
totCredit = totCredit + credit[i];
break;
case 'A':
gpa = gpa + 9 * credit[i];
totCredit = totCredit + credit[i];
break;
case 'B':
gpa = gpa + 8 * credit[i];
totCredit = totCredit + credit[i];
break;
case 'C':
gpa = gpa + 7 * credit[i];
totCredit = totCredit + credit[i];
break;
case 'D':
gpa = gpa + 6 * credit[i];
totCredit = totCredit + credit[i];
break;
case 'E':
gpa = gpa + 5 * credit[i];
totCredit = totCredit + credit[i];
break;
case 'F':
gpa = gpa + 0 * credit[i];
totCredit = totCredit + credit[i];
printf("\nNOTE: F Grade SUBJECTS will not be used in calculation.\n");
break;
default:
printf("WRONG GRADES FORMAT ENTERED\n PLEASE ENTER GRADES IN CAPITAL LETTERS!!\n\n");
exit(0);
}
}
printf("Total GPA: %.1f\tTotal Credit: %.1f\n", gpa, totCredit);
gpa = gpa / totCredit;
printf("Your GPA: %.2f\n", gpa);
printf("\nPress ENTER a few times to terminate the program");
fflush(stdout);
getchar(); getchar();
return 0;
}
最佳答案
ch = getchar();
这将从
STDIN
获取下一个字符。除非您在数字后直接输入字母(例如,在提示输入主题时通过输入2A
),否则这将是\n
。要跳过空格,您可以阅读如下字符:
scanf(" %c", &ch);
初始空格将使其跳过空格并读取第一个非空格字符。
关于c - C代码中的GPA计算器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36472638/