![](/img/trans.png)
[英]I'm trying to run C on VS code and this is what's on terminal. How can I fix this?
[英]I'm trying to make a program that can run different functions depending on the user's choice in C
我做了三个不同的功能。 我正在尝试 atm 让用户选择要操作的 function。 为此,我正在尝试使用 switch 语句。 用户输入两个数字,选择function对数字进行操作。 这是我目前的代码,它不起作用。 MultbyTwo、isFirstBigger 和 addVat 是函数。
#include <stdio.h>
int multbyTwo (int a){
return a*2;
}
int isFirstBigger (int a, int b){
if (a<b){
printf("1");
}
else {
printf("0");
}
}
float addVat(float a, float 20){
return (a/100)*20;
}
int main(){
int a,b;
int choice;
printf("Which Function [0:multbyTwo,1:isFirstBigger,2:addVat]:");
scanf("%d",&choice);
printf("a:\n");
scanf("%f",&a);
printf("b:\n");
scanf("%f",&b);
switch (choice){
case 0: printf(multbyTwo(a));
break;
case 1: printf(isFirstBigger(a,b));
break;
case 2: printf(addVat(a));
break;
}
}
除了我在评论中提到的问题之外,您的许多printf
调用中还缺少格式字符串。 这是您的代码的工作版本,在我更改某些内容的地方添加了“///”注释:
#include <stdio.h>
int multbyTwo(int a) {
return a * 2;
}
int isFirstBigger(int a, int b) {
if (a < b) {
// printf("%s", "1"); /// You print the result in main!
return 1; /// You MUST return a value!
}
else {
// printf("%s", "0"); /// You print the result in main!
return 0; /// You MUST return a value!
}
}
float addVat(float a, float vat) { /// Change here to allow a value of vat to be passed
return (a / 100) * vat; /// ... and here to use the passed value (not fixed)
}
int main() {
int a, b;
int choice;
printf("Which Function [0:multbyTwo,1:isFirstBigger,2:addVat]:");
scanf("%d", &choice);
printf("a:\n");
scanf("%d", &a); /// Use %d for integers - %f is for float types
printf("b:\n");
scanf("%d", &b); /// Use %d for integers - %f is for float types
switch (choice) { /// each call to printf MUST have the format string as the first argument...
case 0: printf("%d", multbyTwo(a));///
break;
case 1: printf("%d", isFirstBigger(a, b));///
break;
case 2: printf("%f", addVat((float)a, 20));/// Now, we must pass the value of "vat" to use in "addVat()"
break;
}
return 0; /// It's good practice to always have this explicit "return 0" in main!
}
我希望这对你有帮助,(但我不得不说。还有很大的改进空间。)
随时要求进一步澄清和/或解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.