2013-03-21 55 views
0

我只想允許字符串或整數輸入,但我似乎無法弄清楚,這是我迄今爲止..我該如何比較這個變量,以便我可以在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以減少用戶錯誤,謝謝!

+0

不要使用'scanf'但'getline',寫你的O代碼解析'0'和'零' - 我認爲這很愚蠢。 – 2013-03-21 06:48:26

+0

@BasileStarynkevitch我想,但我對這個學校的項目限制.. – Datsik 2013-03-21 06:49:40

+0

我恨它,當轉讓或一個項目需要使用它不一定需要只是課程科目的緣故功能。 – 2013-03-21 06:58:25

回答

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];

+0

我試過,但我得到的開關(檢查)線2個錯誤,一個在第50行,if語句中checkInput,和一個右,都是 '過客「的tolower」的參數1(及其他) 'strcmp'使整數指針沒有投射[默認啓用]' 任何想法爲什麼?檢查我原來的帖子以獲取更新的代碼 – Datsik 2013-03-21 07:23:10

+0

tolower功能的參數是字符。不是指向char數組的指針。因此,要將ZERO轉換爲零,您必須對每個字符調用tolower函數。得到它了?? – 999k 2013-03-21 07:25:04

+0

不,我需要一個例子:) – Datsik 2013-03-21 07:26:46

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(); 
} 
比較你的字符串
+0

我想我應該說,我需要使用switch語句這個學校的項目:/ – Datsik 2013-03-21 06:52:57

+0

好了,上面的代碼可以在你的'checkInput'函數,而不是在主代碼中使用,並繼續使用目前的主代碼。無論如何,'switch'不能用在C中的字符串上。注意'checkInput然後需要接受'char *'作爲輸入。 – Fabien 2013-03-21 06:54:36

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); 
0

解決方法1: 作爲字符串

case "0": 

作爲整型

int cmd; // declare cmd as atring 




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

switch (cmd) { 

      case 0: 
       circle(); 
---- 
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; 
} 
相關問題