我只想允許字符串或整數輸入,但我似乎無法弄清楚,這是我迄今爲止..我該如何比較這個變量,以便我可以在switch語句中使用它?
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
#include <stdlib.h>
#define maxL 9182
int main() {
char menuOption;
char cmd[10];
// DECLARE FUNCTIONS TO BE USED IN MENU
float circle() {
float numbers;
float calcFloat;
printf("\n\nYou've selected a circle!\n\n");
printf("Enter in a value between 1.00 and 1000.00: ");
scanf("%f%*c",&calcFloat);
printf("You've entered %f\n", calcFloat);
numbers = 2*calcFloat*3.14159;
printf("The circumference of your circle is: %f\n\n\n", numbers);
}
float sphere() {
float sphere;
float calcSphere;
printf("\n\nYou've selected a sphere!\n\n");
printf("Enter in a value between 10.5 and 1024.00: ");
scanf("%f%*c",&calcSphere);
printf("You've entered %f\n", calcSphere);
sphere = (4/3)*calcSphere*calcSphere*calcSphere*3.14159;
printf("The volume of a sphere with your value is: %.2f\n\n\n", sphere);
}
char checkInput(char input) {
if ((strcmp(input, "0") == 0) || (strcmp(input, "zero") == 0)) {
return 0;
}
}
// END OF FUNCTIONS
// CREATE AND SHOW MENU AND RECEIVE INPUT TO DIRECT TO PROPPER FUNCTION.
for (;;) {
printf("Menu: \n"
"(0)Circle\n"
"(1)Sphere\n"
"(2)Palindrome\n"
"(3)Shuffle\n"
"(4)Quit\n");
printf("Select your choice!: ");
scanf("%c", cmd);
switch (checkInput(tolower(cmd))) {
case 0:
circle();
break;
case 1:
sphere();
break;
}
}
// END OF SWITCH STATEMENT AND MENU
}
我想讓人們要麼類型0或零成scanf
以減少用戶錯誤,謝謝!
不要使用'scanf'但'getline',寫你的O代碼解析'0'和'零' - 我認爲這很愚蠢。 – 2013-03-21 06:48:26
@BasileStarynkevitch我想,但我對這個學校的項目限制.. – Datsik 2013-03-21 06:49:40
我恨它,當轉讓或一個項目需要使用它不一定需要只是課程科目的緣故功能。 – 2013-03-21 06:58:25