我正在開關操作員的幫助下制定一個加法,減法,除法和乘法程序。在這裏,我想製作一個只有當用戶輸入任何其他字符而不是'Y'時纔會停止的程序。問題是:當我運行程序時,在相應案例的結果之後,它只會打印'是否要繼續',之後程序會停止。請幫忙!先謝謝你! :)請解釋一下這個程序有什麼問題,我是新的
#include<stdio.h>
int main() {
int n,a,b;
char answer;
while(1) {
printf("enter \n1.Addition \n2.subtraction \n3.division \n4.multiplication\t");
scanf("%d",&n);
switch(n) {
case 1: {
printf("enter value of a and b:\t");
scanf("%d %d",&a,&b);
printf("addition of %d+%d is %d",a,b,a+b);
goto ans;
}
case 2: {
printf("enter the value of a and b:\t");
scanf("%d %d",&a,&b);
printf("Subtraction of %d-%d is %d",a,b,a-b);
goto ans;
}
case 3: {
printf("enter the value of a and b:\t");
scanf("%d %d",&a,&b);
printf("Division of %*% is %d",a,b,a/b);
goto ans;
}
case 4: {
printf("enter the value of a nad b:\t");
scanf("%d %d",&a,&b);
printf("Multiplication of %d*%d is %d",a,b,a*b);
goto ans;
}
default: printf("invalid value!");
goto ans;
}
ans: {
printf("Do you want to continue?(Y/N):\t");
scanf("%c",&answer);
if(answer=='Y') continue;
else break;
}
}
return 0;
}
我編輯原始格式使用http://prettyprinter.de/module.php?name=PrettyPrinter – Antonio 2014-09-30 09:58:21
你不應該使用'goto'。嘗試使用「while」循環或其他方式來檢查您的狀況。 – pzaenger 2014-09-30 09:59:44
您的代碼顯示的警告類似於「30:6:警告:轉換在格式結尾處缺少類型[-Wformat]」 – Chandru 2014-09-30 10:10:00