[英]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.