簡體   English   中英

不明白如何在C中循環輸入/打印和比較字符串

[英]Don't understand how to input/print and compare string in loop in C

我是C的新人,我被卡住了。 我想寫一個簡單的程序,它將從鍵盤輸入並輸出它,如果它不是一個'退出'字。 我嘗試了幾種不同的方法,但沒有一種方法可行。 幾乎在所有情況下,我都獲得了第一個輸入的無限輸出。

這是我的方法之一:

#include <stdio.h>

int main() {
    char word[80];

    while (1) {
        puts("Enter a string: ");
        scanf("%79[^\n]", word);

        if (word == "exit")
            break;

        printf("You have typed %s", word);
    }

    return 0;
}

我想在它完成每個循環后它應該再次給我提示,但事實並非如此。 我做錯了什么。

如果你知道請給我一些建議。

提前致謝。 真的,如果你幫助我理解我做錯了什么,我會很高興的。

哦,順便說一句,我注意到當我輸入一些單詞並按'Enter'時,結果字符串也包括Enter 我怎么能擺脫這個?

  1. 不正確的字符串比較 - 使用strcmp()

    if (word == "exit")簡單地比較2個地址:所述第一地址charword和第一的地址char在字符串文字"exit" 代碼需要比較從這些地址開始的內容: strcmp()這樣做。

  2. 從上一行的Enter鍵中留下'\\n' scanf()格式添加空格以使用可選的前導空格。 還要檢查scanf()結果。

    scanf()說明符如"%d""%u""%f"本身使用可選的前導空格。 3個例外: "%c""%n""%["

  3. printf()格式的末尾添加'\\n' @Matt McNabb

     #include <stdio.h> int main() { char word[80]; while (1) { puts("Enter a string: "); // v space added here if (scanf(" %79[^\\n]", word) != 1) break; // Nothing saved into word or EOF or I/O Error if (strcmp(word, "exit") == 0) break; printf("You have typed %s\\n", word); } return 0; } 

很好,OP在scanf()使用了適當的寬度限制值79

哦,順便說一句,我注意到當我輸入一些單詞並按'Enter'時,結果字符串也包括Enter。 我怎么能擺脫這個?

這是因為你沒有在printf("You have typed %s", word);之后輸出換行符printf("You have typed %s", word); 執行的下一個語句是puts("Enter a string: "); 所以你會看到You have typed helloEnter a string: 要解決此問題,請更改為printf("You have typed %s\\n", word);

正如其他人提到的那樣,使用strcmp來比較C中的字符串。

最后,scanf格式字符串"%79[^\\n]"與換行符不匹配。 因此輸入流仍包含換行符。 下次到達此語句時,換行符仍在流中,但仍然不匹配,因為您明確排除了換行符。

在獲得下一行之前,您需要丟棄該換行符(以及該行上的任何其他輸入)。 一種方法是將輸入更改為scanf("%79[^\\n]%*[^\\n]", word); getchar(); scanf("%79[^\\n]%*[^\\n]", word); getchar(); 這意味着:

  • 最多可讀取79個非換行符
  • 閱讀所有非換行內容,不要存儲它們
  • 讀取一個字符(現在必須是換行符)並且不存儲它

最后檢查scanf的返回值是個好主意,這樣如果出現錯誤,那么你可以退出程序而不是進入無限循環。

如果下一個字符是換行符( \\n ),則說明符[^\\n]將中止scanf而不讀取換行符 因此, scanf調用后第一個不會讀取任何輸入。

如果要讀取單個單詞,請使用%79s說明符和以下代碼刪除字符串末尾的\\n

if(word[strlen(word)]=='\n')
    word[strlen(word)]='\0';

如果要讀取整行,可以通過以下方式從輸入緩沖區中刪除換行符:

char line[80];
int i;
while(1)
{
    puts("Enter a string:");
    i=-1;
    scanf("%79[^\n]%n",line,&i);
    //%n returns the number of characters read so far by the scanf call

    //if scanf encounters a newline, it will abort and won't modify i
    if(i==-1)
        getchar(); //removes the newline from the input buffer

    if(strcmp(line,"exit")==0)
        break;

    printf("You have typed %s\n",line);
}
return 0;

在讀取內存緩沖區之前,最好用memset(3)清除(以獲得可重現的內容),並且應該使用strcmp(3)來比較字符串。 另外,考慮在輸入之前使用fflush(3) (即使在你的情況下實際上並不需要),不要忘記測試scanf(3)的結果 ,也是大多數printf(3)格式控制字符串應該以一個\\n結尾\\n用於沖洗的終點 - 所以:

#include <stdio.h>
int main() {
  char word[80];
  while(1) {
     puts("Enter a string: ");
     memset (word, 0, sizeof(word)); // not strictly necessary
     fflush(stdout); // not strictly necessary
     if (scanf("%79[^\n]", word)<=0)  exit(EXIT_FAILURE);
     if (!strcmp(word,"exit"))
        break;
     printf("You have typed %s\n", word);
  };
  return 0; 
}

我建議用fgets(3)讀取整行並刪除它的結束換行符(使用strchr(3) )。 另請閱讀getline(3)

不要忘記編譯所有警告和調試信息(例如gcc -Wall -g )並學習如何使用調試器 (例如gdb

您的第一個問題是您無法將字符串與'=='進行比較。 所以:

if (word == "exit")

應該

if ( strncmp( word, "exit", 4 ) == 0 )

(你也可以使用strncmp( word, "exit", strlen(word) )如果你知道這個詞是零終止的並且不是壞的值。還有其他一些選項。)

你的第二個問題是scanf()沒有消耗輸入,可能是因為它與你所說的預期不匹配。 以下是如何做你想做的事情的一個很好的解釋:

http://home.datacomm.ch/t_wolf/tw/c/getting_input.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM