开关盒未打印c
用C中的另一个问题再次返回。 我的switch语句有一些问题。我的程序正在编译和运行,甚至将正确的值存储在正确的变量中,并按应有的方式运行switch语句,但打印特定输出时除外。请记住,我正在努力学习,我的知识非常基础。我非常感谢你们在这个迷人的社区中所给予的帮助,我希望你们的回答能帮助我了解更多 不费吹灰之力,这里出现了一堵编码文本的墙开关盒未打印c,c,switch-statement,printf,output,C,Switch Statement,Printf,Output,用C中的另一个问题再次返回。 我的switch语句有一些问题。我的程序正在编译和运行,甚至将正确的值存储在正确的变量中,并按应有的方式运行switch语句,但打印特定输出时除外。请记住,我正在努力学习,我的知识非常基础。我非常感谢你们在这个迷人的社区中所给予的帮助,我希望你们的回答能帮助我了解更多 不费吹灰之力,这里出现了一堵编码文本的墙 // Point of sale software that lists a set of packages with a subset of possibl
// Point of sale software that lists a set of packages with a subset of possible upgrades for each
/*This C program was written with a zoom value of 100 and with the use of wordwrap
the native resolution of the screen is set to 1920x1080
for easiest veiwing make sure wordwrap is up*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int package = 0, memory = 0, hd = 0, monitor = 0, gpu = 0, another = 0;
float price = 0, memprice = 0, hdprice = 0, monprice = 0, gpuprice = 0, subtotal = 0, hst = 0, total = 0;
printf("Welcome to the IPC Company's Computer System Calculator\nthe package desired\n(1: basic, 2 : professional, 3 : game system) : ");
scanf("%d", &package);
do // the condition to break the loop is to have the user input 0 into the variable another
{
if (package == 1) //selects the basic package and stores user selections of upgrades
{
price = 599;
printf("Enter additional memory required\n(0: 4 GB included, 1 : 8 GB, 2 : 12 GB) : ");
scanf("%d", &memory);
printf("Enter monitor required\n(0: 21 inch LED included, 1 : 27 inch LED) : ");
scanf("%d", &monitor);
printf("Enter Hard Drive drive required\n(0: 512 GB included, 1 : 128 GB SSD) : ");
scanf("%d", &hd);
printf("\n\n========================================\n");
printf("Basic Package 599.00\n");
}
else if (package == 2)// selects the professional package and stores user selections of upgrades
{
price = 899;
printf("Enter additional memory required\n(0: 8 GB included, 1 : 16 GB) : ");
scanf("%d", &memory);
memory += 3;
printf("Enter monitor required\n(0: 27 inch LED included, 1 : 32 inch LED) : ");
scanf("%d", &monitor);
monitor += 2;
printf("Enter Hard Drive drive required\n(0: 1 TB included, 1 : 256 GB SSD, 2 : 512 GB SSD) : ");
scanf("%d", &hd);
hd += 2;
printf("Enter Video Card required\n(0: IGP included, 1 : 2 GB Discrete) : ");
scanf("%d", &gpu);
gpu += 1;
printf("\n\n========================================\n");
printf("Professional Package 899.00\n");
}
else
{
price = 1499; //selects the gaming package and stores user selections for upgrades
printf("Enter additional memory required\n(0: 16 GB included, 1 : 32 GB) : ");
scanf("%d", &memory);
memory += 5;
printf("Enter monitor required\n(0: 32 inch LED included, 1 : 28 inch 4K HD) : ");
scanf("%d", &monitor);
monitor += 4;
printf("Enter Hard Drive drive required\n(0: 256 GB SSD included, 1 : 512 GB SSD, 2 : 1 TB SSD) : ");
scanf("%d", &hd);
hd += 5;
printf("Enter Video Card required\n(0: 2 GB Discrete included, 1 : 4 GB Discrete) : ");
scanf("%d", &gpu);
gpu += 3;
printf("\n\n========================================\n");
printf("Gaming Package 1499.00\n");
//values for each variable is upcast by an appropriate amount to keep input either 0 1 2 while storing them as 0 to 8+ to allow
//the use of four switches as apposed to a switch for each individual package/component combination
//partial credit to professor joseph hughes for shedding light on this methodology
}
switch (memory)//4 switch catagories based on memory hd gpu and monitor each containing a pre defined price
{
case 0:
memprice = 0;
printf("4 GB Memory: 0.00\n");
break;
case 1:
memprice = 99;
printf("8 GB Memory: 99.00\n");
break;
case 2:
memprice = 189;
printf("12 GB Memory: 189.00\n");
break;
case 3:
memprice = 0;
printf("8 GB Memory: 0.00\n");
break;
case 4:
memprice = 189;
printf("16 GB Memory: 189.00\n");
break;
case 5:
memprice = 0;
printf("16 GB Memory: 0.00\n");
break;
case 6:
memprice = 389;
printf("32 GB Memory: 389.00\n");
break;
}
switch (monitor)
{
case 0:
monprice = 0;
printf("21 inch LED Monitor 0.00\n");
break;
case 1:
monprice = 199;
printf("27 inch LED Monitor 199.00\n");
break;
case 2:
monprice = 0;
printf("27 inch LED Monitor 0.00\n");
break;
case 3:
monprice = 199;
printf("32 inch LED Monitor 199.00\n");
break;
case 4:
monprice = 0;
printf("32 inch LED Monitor 0.00\n");
break;
case 5:
monprice = 299;
printf("28 inch 4K LED Monitor 299.99\n");
break;
}
switch (hd)
{
case 0:
hdprice = 0; printf("512 GB Hard Drive 0.00\n");
break;
case 1:
hdprice = 119;
printf("128 GB SSD 119.00\n");
break;
case 2:
hdprice = 0;
printf("1 TB Hard Drive 0.00\n");
break;
case 3:
hdprice = 189;
printf("256 GB SSD 189.00\n");
break;
case 4:
hdprice = 399;
printf("512 GB SSD 399.99\n");
break;
case 5:
hdprice = 0;
printf("256 GB 0.00\n");
break;
case 6:
hdprice = 299;
printf("512 GB SSD 299.00\n");
break;
case 7:
hdprice = 599;
printf("1TB SSD 599.00\n");
break;
}
switch (gpu)
{
case 0:
gpuprice = 0;
break;
case 1:
gpuprice = 0;
printf("IGP Video 0.00\n");
break;
case 2:
gpuprice = 209;
printf("Discrete 2GB Video 209.00\n");
break;
case 3:
gpuprice = 0;
printf("Discrete 2GB Video 0.00\n");
break;
case 4:
gpuprice = 399;
printf("Discrete 4GB Video 399.00\n");
break;
}
subtotal = price + memprice + monprice + gpuprice + hdprice;
hst = subtotal*.13;
total = subtotal + subtotal*.13;
printf("========================================\n");
printf("Sub Total: %.2f\n", subtotal);
printf("HST: %.2f\n", hst);
printf("========================================\n");
printf("Total: %.2f\n", total);
printf("Do you wish to choose another computer package? (1: YES or 0: NO): ");
scanf("%d", &another);
// note most prices are registerd as string literals and not variable calls with the exclusion of subtotal hst and total
} while (another == 1);
return 0;
}
变量memory、hd、monitor等声明为整数,但switch语句使用的是char类型。只需删除单引号
case '1': // this is a char
case 1: // this is an integer
内存和监视器以及hd和gpu包含int值,而不是您的case语句正在测试的“char”值。此外,一些scanf调用看起来可疑:scanf%d,hd;应为扫描%d,&hd;后者将读取的整数存储到hd中;前者将其写入某个垃圾地址,该地址是从hd碰巧包含的任何值派生的!我的意思是,如果你把一个值1输入一个整数,你的case语句应该测试值1,而不是字符“1”。你看到区别了吗?通过对“1”进行测试,编译器会将其视为对值49的测试,该值是数值字符“1”的ASCII值@FadyAlkarmi:如果启用了警告,编译后的程序将/应该对scanf问题发出警告。我强烈建议在GCC中使用-Wall,如果使用其他编译器,请查看编译器手册。在switch语句中,最好始终包含“default:”大小写,尤其是在处理用户输入时,因为否则,程序如何知道输入是否有效。还建议在进入下一个输入之前测试每个输入,可能是在循环中,或者通过为每个输入调用子函数来确保输入值有效。次要的挑剔:“1”仍然具有int类型,但它可能与1的值不同。非常感谢!我的老师和他的笔记都没有提到这一点。这就是为什么我真的很感激这个社区,希望有一天我能再次回馈你,祝你度过一个美好的夜晚/一天@MartinTörnwall是的,“1”并不意味着任何数据类型,它是在使用它的上下文中表示数值的一种方式,在本例中,它是一个int。@WeatherVane see@user3629249您没有阅读Martin törnwall的链接或这些注释吗?c是字符1字节,但“x”不是。