我只想允许字符串或整数输入,但我似乎无法弄清楚,这是我到目前为止所拥有的......
#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
}
我想让人们在scanf
中键入0或0以减少用户错误,谢谢!
答案 0 :(得分:1)
getline
功能可让您将字符串存储到cmd
。
然后,您可以将字符串与strcmp
if ((strcmp(cmd, "0") == 0) || (strcmp(cmd, "zero") == 0)) {
circle();
} else if ((strcmp(cmd, "1") == 0) || (strcmp(cmd, "one") == 0)) {
sphere();
}
答案 1 :(得分:0)
我只想允许字符串或整数输入
最好的方法是使用正则表达式验证输入
#include <regex.h>
regex_t checkInteger;
regex_t checkFloat;
bool isInteger = false;
bool isFloat = false
int reti;
char input[100];
// read input from user
regcomp(&checkInteger, "^[1-9][0-9]*$", 0);
regcomp(&checkFloat, "^[1-9][0-9]*\.[0-9]+$", 0);
isInteger = !regexec(&checkInteger, input, 0, NULL, 0);
isFloat = !regexec(&checkFloat, input, 0, NULL, 0);
if (isInteger)
{
// input is integer
}
else if (isFloat)
{
// input is float
}
regfree(&checkInteger);
regfree(&checkFloat);
答案 2 :(得分:0)
像这样更改checkInput
功能
int checkInput(char* input) {
int i = 0;
while (i < strlen(input))
input[i] = tolower(input[i]);
if ((strcmp(input, "0") == 0) || (strcmp(input, "zero") == 0)) {
return 0;
} else if ((strcmp(input, "1") == 0) || (strcmp(input, "one") == 0)) {
return 1;
}
return 4; // Quit Option
}
在main
这样的电话中
switch (checkInput(cmd)) {
case 0:
circle();
break;
case 1:
sphere();
break;
}
您正在接受变量cmd
中的字符串。将它声明为像char cmd[10];
答案 3 :(得分:0)
解决方法1: as String
case "0":
as Integer
int cmd; // declare cmd as atring
printf("Select your choice!: ");
scanf("%d", &cmd);
switch (cmd) {
case 0:
circle();
----
答案 4 :(得分:0)
将输入读入以NULL结尾的字符串cmd
,然后
switch (((strcmp(cmd, "0") == 0) || (strcmp(cmd, "zero") == 0)) +
((strcmp(cmd, "1") == 0) || (strcmp(cmd, "one") == 0)) * 2)
{
case 1:
circle();
break;
case 2:
sphere();
break;
default:
// handle the error
break;
}