난 그냥 문자열 또는 정수 입력을 허용 할,하지만 난 그것을 알아낼 수없는 것, 이것은 내가 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'과 '0'을 모두 파싱하는 코드 - 바보 같다고 생각합니다. –
@BasileStarynkevitch하지만이 학교 프로젝트에 대한 제한이 있습니다. – Datsik
과제 나 프로젝트에서 과목을 위해서만 필요하지 않은 기능을 사용해야하는 경우 싫습니다. –