我的代码出了问题。我的程序是一个注册系统,每当我选择一个开关案例时,它应该显示总价格,但在选择最后一个要注册的主题后,总加价似乎是错误的。请帮忙。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, none, ntwo, nthree, Total;
float Algebra, Trigonometry, Calculus, Engiana, Physics;
char password[20], username[8];
Algebra = 100;
Trigonometry = 300;
Calculus = 500;
Engiana = 750;
Physics = 1500;
Total = (none + ntwo + nthree);
printf("Welcome to the Enrollment System \n");
printf("Here is the list of Available Subjects \n");
printf("\n");
printf("Course Code Price\n");
printf("\n");
printf("1. Algebra %.2f \n",Algebra);
printf("2. Trigonometry %.2f \n",Trigonometry);
printf("3. Calculus %.2f \n",Calculus);
printf("4. Engiana %.2f \n",Engiana);
printf("5. Physics %.2f \n",Physics);
printf("You can only select three courses to enroll for the Term \n");
printf("\n Select First Course to Enroll \n");
scanf("%d", &none);
switch(none)
{
case 1: printf(" You Enrolled Algebra %.2f \n",Algebra); break; //if conditions met , immediately goes to system pause
case 2: printf(" You Enrolled Trigonometry %.2f \n", Trigonometry); break; //if conditions not met goes to another case
case 3: printf(" You Enrolled Calculus %.2f \n", Calculus); break;
case 4: printf(" You Enrolled Engiana %.2f \n", Engiana); break;
case 5: printf(" You Enrolled Physics %.2f \n", Physics); break;
default: printf(" The Course you entered is not valid \n"); break;
}
printf("\n Select Second Course to Enroll \n");
scanf("%d", &ntwo);
switch(ntwo)
{
case 1: printf(" You Enrolled Algebra %.2f \n",Algebra); break; //if conditions met , immediately goes to system pause
case 2: printf(" You Enrolled Trigonometry %.2f\n", Trigonometry); break; //if conditions not met goes to another case
case 3: printf(" You Enrolled Calculus %.2f\n", Calculus); break;
case 4: printf(" You Enrolled Engiana %.2f\n", Engiana); break;
case 5: printf(" You Enrolled Physics %.2f \n", Physics); break;
default: printf(" The Course you entered is not valid \n"); break;
}
printf("\n Select Third Course to Enroll \n");
scanf("%d", &nthree);
switch(nthree)
{
case 1: printf(" You Enrolled Algebra %.2f \n",Algebra); break; //if conditions met , immediately goes to system pause
case 2: printf(" You Enrolled Trigonometry %.2f \n", Trigonometry); break; //if conditions not met goes to another case
case 3: printf(" You Enrolled Calculus %.2f \n", Calculus); break;
case 4: printf(" You Enrolled Engiana %.2f \n", Engiana); break;
case 5: printf(" You Enrolled Physics %.2f \n", Physics); break;
default: printf(" The Course you entered is not valid \n"); break;
}
printf("Total Tuition Price = %.2f \n",Total);
system("PAUSE");
return 0;
}
答案 0 :(得分:2)
Total = (none + ntwo + nthree);
此行应该是最后一行。
,即代码的底部应该如下所示
Total = (none + ntwo + nthree);
printf("Total Tuition Price = %.2f \n",Total);
system("PAUSE");
return 0;
}
请记住,这些说明是按照编写顺序执行的,而在原始代码中,Total
是在用户有机会回答之前计算出来的。
顺便说一下,我知道你可能刚开始学习,但这是我遵循的格言,如果你复制粘贴超过你自己代码的一行,你可以做得更好。一旦你得到函数和数组和结构,也许你会重新考虑这个并尝试删除任何类型的代码重复。
其他两位回答者指出了同样有效的问题,您目前只是添加了用户输入,而您可能希望自己添加课程的成本(?)。
答案 1 :(得分:1)
您遇到了逻辑错误。您需要在scanf之后每次都放置变量Total的赋值语句。但是你必须在switch语句中以正确的数量更新Total。
示例:
case 1: printf(" You Enrolled Algebra %.2f \n",Algebra); Total += 100; break; //if conditions met , immediately goes to system pause
case 2: printf(" You Enrolled Trigonometry %.2f \n", Trigonometry); Total += 300; break; //if conditions not met goes to another case
case 3: printf(" You Enrolled Calculus %.2f \n", Calculus); Total += 500; break;
case 4: printf(" You Enrolled Engiana %.2f \n", Engiana);Total += 750; break;
case 5: printf(" You Enrolled Physics %.2f \n", Physics); Total += 1500; break;
default: printf(" The Course you entered is not valid \n"); break;
}
printf("\n Select Second Course to Enroll \n");
scanf("%d", &ntwo);`
PS:使用循环可以改善您的代码..
答案 2 :(得分:1)
主要有2个错误
您的Total
变量是int
数据类型。因此,在打印时,您应该使用%d
。要使用%.2f
,您应该Total
变量float
要查找总学费,您应该在每种情况下添加学费
case 1:Total += Algebra; printf(" You Enrolled Algebra %.2f \n",Algebra); break; //if conditions met , immediately goes to system pause
case 2:Total += Trigonometry; printf(" You Enrolled Trigonometry %.2f \n", Trigonometry); break; //if conditions not met goes to another case
case 3:Total += Calculus; printf(" You Enrolled Calculus %.2f \n", Calculus); break;
case 4:Total += Engiana; printf(" You Enrolled Engiana %.2f \n", Engiana); break;
case 5:Total += Physics; printf(" You Enrolled Physics %.2f \n", Physics); break;
default: printf(" The Course you entered is not valid \n"); break;
此外,您应该使用0而不是Total
(none + ntwo + nthree)