2012-04-14 62 views
1

我希望能夠將stdin上的字符與我的規範中的字符進行比較。這樣做的目的是將所有其他輸入過濾爲錯誤,同時僅將指定的單個字符保留爲命令。 stdin「nn」或「qddaw」 - >錯誤再次出現,但是「n」使一些有用的東西。如何比較字符與C中給定字符的集合?

這是我心目中的「代碼明智」:

if (input does not contain 'c' or 's' or 'q' or 'n') { 
    printf("some kind of error"); 
} 

嗯,我試圖創建一個特定的字符,如array[] = {'a', 'b', 'c'}數組,所以我可以能夠將其與上一個字符串比較stdin與功能strncmp ..就像

char c[256]; 
scanf("%s", c) 
if (strncmp(array, c, 1) != 0) printf("error"); 

但它似乎沒有工作。有什麼建議麼?

EDIT1:下面是實際的代碼:

char c[256]; 
char* s = "nsrld"; 
char* quiter = "q"; 

do 
    { 
     printf(">"); 
     scanf("%s", c); 
     if (only when there is no 'n' or 's' or other char from char* s on input) 
     { 
      errorHandle(ERROR_WRONG_CMD); 
     } 
     scanf("%*[^\n]"); scanf("%*c"); 
    } while (strcmp(c,quiter) != 0); 

,你可以看到我處理的「Q」的東西不錯,但多個字符都在屁股痛。感謝您的任何建議。

編輯2:或者換句話說我需要一個函數,其將比較與一組給定的字符的輸入且僅當有一個OR另一個(如「Q」或「S」的功能將通過(但如果有字符在一起,就像「QS」)

+0

使用getchar(),您是否嘗試打印'c'?打印出來就像這樣:printf(「c =%c /%d \ n」,c,c);另外,'c','char'或'int'的類型是什麼? – 2012-04-14 22:55:13

+0

@GeorgeSkoptsov我編輯的帖子,因爲我實際上需要區分輸入「qdqqdq」和「q」,在這種情況下,只有獨立的「q」才能完成這項工作,而其他任何輸入都會報告錯誤。 – Markus 2012-04-15 11:57:45

回答

1

我沒有讓自己清楚。我需要的是輸入式「任何你想要的東西」喜歡「wwqwqe」,並做了錯誤,除非輸入僅僅是「C」或只是「S」(和幾個)。

char usersInput[200] = "";   /* A buffer to hold the input values */ 
char *result = gets(usersInput);  /* Fill the buffer from stdin */ 
if (result != NULL)     /* If we got something */ 
{ 
    if (strlen(usersInput) == 1)  /* the input must be exactly 1 character */ 
    { 
     char ch = usersInput[0]; 
     if (strchr(ch, "csqn") == NULL) /* It must be a valid values */ 
     { 
      printf("Evil Bad Character <%c>\n", ch); 
     } 
     else 
     { 
      /* Do stuff with the known valid input value ch */ 
     } 
    } 
    else 
    { 
     puts("The input value must be exactly 1 character\n"); 
     puts("and must be 'c', 's', 'q' or 'n'"); 
    } 
} 
else 
{ 
    puts("EOF or ERROR while reading stdin\n"); 
} 

這應該做的工作。

一個警告。 得到是不夠聰明,知道用戶輸入是200個字符長。

它會高興地讓你輸入201個字符或更多,它會覆蓋內存中的其他字符。這種事情會導致難以發現的錯誤。

+0

嗯,如果我想要我的例子[鏈接](http://stackoverflow.com/a/10162127/1333847)無限循環和我想在返回兩次「回車」後返回錯誤。我該怎麼辦?在我的版本中,我可以根據需要多次進入,沒有任何反應,它仍在等待另一個輸入。 – Markus 2012-04-15 13:24:51

+0

多數民衆贊成在一個新的問題。 – EvilTeach 2012-04-15 14:05:10

1
char c = getchar(); 
switch (c) { 
case 'c': 
case 's': 
case 'q': 
case 'n': 
    do_something(); 
    break; 
default: 
    print_error(); 
}; 

上面的代碼應該工作。我不知道爲什麼你的if語句不能正常工作,一般開關在這種情形下效果很好

+0

這裏的問題與getchar因爲它只需要一個字符,所以像「cawwffawf」這樣的輸入將通過這個測試。我需要的是當輸入像「caawfaw」printf錯誤,但是當輸入正好是一個字符「c」時,做一些有用的事情。 – Markus 2012-04-15 08:12:50

0

撰寫功能

int IsGood(int c) 
{ 
    if(c=='a'||c=='b'||c=='c') 
    return 1; 
    else 
    return 0; 
} 
+0

我編輯了第一篇文章以反映我的需求。我需要能夠將輸入「cwdwdaf」與'c'區分開來,在這種情況下,只有'c'會使某些有用的東西有用。其他輸入將解析錯誤。 – Markus 2012-04-15 08:43:25

0

您的第一個解決方案應該可以工作。如果這是你發佈的完全相同的代碼 - 那麼你的問題可能,因爲printf需要換行符以刷新到控制檯。

1
int ch = getchar(); 
if (ch != EOF) 
{ 
    if (strchr("csqn", ch) == NULL) 
    { 
     printf("Evil Bad Character <%c> in Hex %02X\n", ch, ch); 
    } 
    else 
    { 
     /* Do stuff with ch */ 
    } 
} 
else 
{ 
    printf("EOF on input\n"); 
} 
+0

我編輯了第一篇文章以反映我的需求。我需要能夠將輸入「cwdwdaf」與'c'區分開來,在這種情況下,只有'c'會使某些有用的東西有用。其他輸入將解析錯誤。 – Markus 2012-04-15 08:43:05

+0

你的問題和例子有誤導性。 getchar一次獲取一個字符。如果你想處理檢查所有的輸入,然後執行一些操作,你需要修改你的問題。 – EvilTeach 2012-04-15 12:44:30

+0

耶對不起,我已經想通了,謝謝你的時間和精力,我真的很感激! :) – Markus 2012-04-15 12:57:09

0

我已經想好了字符串作爲套......所以,如果它們的交集是無效設置,那麼我們就會失敗 - >的printf(「錯誤」)......否則輸出爲無...

#include <stdio.h> 
#include <string.h> 

int intersection(char* source, char* search) 
{ 
    int i,j; 
    for(i = 0; i < strlen(search); i++) 
    if(strchr(source,search[i]))j++; 
    if(j != strlen(search))return 0; 
    else return 1; 
} 

int main() 
{ 
    char *letters = "eo"; 
    char *p = "hello"; 
    int e = intersection(p,letters); 
    if(e==1)puts("Non Void"); 
    else puts("Void"); 
} 
+0

感謝你的這段代碼,但也許我沒有讓自己清楚。我需要的是輸入式「任何你想要的東西」喜歡「wwqwqe」,並做了錯誤,除非輸入僅僅是「C」或只是「S」(和幾個)。用你的代碼,如果我將p指定爲「cs」,即使像「cs」這樣的輸入也會通過。代碼'q'結束編。 'char c [256]; char * s =「nsrld」; char * quiter =「q」; do { printf(「>」); scanf(「%s」,c); if(dunno) errorHandle(ERROR_WRONG_CMD); } scanf(「%* [^ \ n]」);的scanf( 「%* C」); (strcmp(c,quiter)!= 0);' – Markus 2012-04-15 08:24:30

0

這爲我工作:

char c[256]; 
    char* s = "nqsrld"; 
    char* quiter = "q"; 

    do 
    { 
     printf(">"); 
     scanf("%s", c); 
     if ((strpbrk(s, c) == 0) || (strlen(c) >= 2)) 
     { 
      errorHandle(ERROR_WRONG_CMD); 
     } 
     scanf("%*[^\n]"); scanf("%*c"); 
    } while (strcmp(c,quiter) != 0); 

感謝大家的幫助。