0

我只想允许字符串或整数输入,但我似乎无法弄清楚,这就是我到目前为止所拥有的......

#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以减少用户错误,谢谢!

4

5 回答 5

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();
}
于 2013-03-21T06:52:17.463 回答
0

将输入读入一个以 NUL 结尾的字符串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;
}
于 2013-03-21T07:04:29.547 回答
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);
于 2013-03-21T07:00:40.613 回答
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;
}

您正在接受一个字符串到 variable cmd。将其声明为这样的字符数组char cmd[10];

于 2013-03-21T07:01:00.187 回答
0

解决方案1:作为字符串

case "0":

作为整数

int cmd; // declare cmd as atring




printf("Select your choice!: ");
    scanf("%d", &cmd); 

switch (cmd) {

            case 0:
                circle();
----
于 2013-03-21T07:01:10.063 回答