gpt4 book ai didi

c - 我正在尝试制作一个可以根据用户在 C 中的选择来运行不同功能的程序

转载 作者:行者123 更新时间:2023-11-30 16:10:49 24 4
gpt4 key购买 nike

我做了三个不同的函数。我正在尝试 atm 让用户选择要操作的功能。为此,我尝试使用 switch 语句。用户输入两个数字并选择对数字进行操作的函数。这是我目前的代码,它不起作用。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!
}

希望这对您有帮助! (但不得不说,还有很大的进步空间。)

请随时要求进一步澄清和/或解释。

关于c - 我正在尝试制作一个可以根据用户在 C 中的选择来运行不同功能的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58789391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com