簡體   English   中英

scanf單字符不適用於我的循環

[英]scanf single char not working with my loops

因此,我一直在為2D尋寶開發代碼,在該代碼中,我使用來自scanf的單個字符,無論它是否為n,s,w,e都會執行特定的輸出。 不幸的是,我的代碼沒有監聽我的輸入,例如,當我鍵入n時,什么也沒有發生。 我想知道你們是否可以指出我正確的道路

do
{
    printf("Please press n, s, w, or e to find the treasure\n");
    scanf(" %c", &n);
    if (scanf(" %c", &n) == 'n')
    {
        current - 11;
        new = current - 11;
        if (abs(potofgold - current) < abs(potofgold - new))
            printf("getting warmer\n");
        else if (abs(potofgold - current) > abs(potofgold - new))
            printf("Getting colder\n");
        else
            printf("Hooray\n");
        current = new;
    }
    else if (scanf("%c", &s) == 's')
    {
        current + 11;
        new = current + 11;
        if (abs(potofgold - current) < abs(potofgold - new))
            printf("Getting warmer\n");
        else if (abs(potofgold - current) > abs(potofgold - new))
            printf("getting colder\n");
        else
            printf("Hooray\n");
        current = new;
    }
    else if (scanf("%c", e) == 'e')
    {
        current + 1;
        new = current + 1;
        if (abs(potofgold - current) < abs(potofgold - new))
            printf("getting warmer\n");
        else if (abs(potofgold - current) > abs(potofgold - new))
            printf("getting colder\n");
        else
            printf("Hooray\n");
        current = new;
    }
    else (scanf("%c", &w) == 'w');
    {
        current - 1;
        new = current - 1;
        if (abs(potofgold - current) < abs(potofgold - new))
            printf("getting warmer\n");
        else if (abs(potofgold - current) > abs(potofgold - new))
            printf("getting colder\n");
        else
            printf("Hooray\n");

        current = new;
    }
}
while (current != potofgold);

return 0;

這些條件在任何情況下都不是正確的-

if (scanf(" %c", &n) == 'n')       // compare variable with character n=='n' 
....
else if (scanf("%c", &s) == 's')              // s=='s'
....
else if (scanf("%c", e) == 'e')               // e=='e'
                    ^ & missing here

scanf在每種情況下在成功時都返回1 不要將其與字符進行比較。

相反,您可以編寫這樣的條件-

 if (scanf(" %c", &n) == 1 && n=='n')     // if scanf will be successful then only n=='n' will be evaluated. Thus , on failure of scanf scond condition will not be evaluated
  ....
 else if (scanf("%c", &s) ==1 && s=='s')  
 ....
 else if (scanf("%c", &e) == 1 && e=='e')              

else if -

 else (scanf("%c", &w) == 'w');   // else ends here (It should be else if)
{                                      // this is after else part 
    current - 1;           // remove this , compiler will issue an error\warning
    new = current - 1;     
    if (abs(potofgold - current) < abs(potofgold - new))
        printf("getting warmer\n");
    else if (abs(potofgold - current) > abs(potofgold - new))
        printf("getting colder\n");
    else
        printf("Hooray\n");

    current = new;
}

還有這些

 current - 11;  // do you mean current-=11;
 current + 11;  // as current+=11;
 current +1 ;   // as current +=1;
 current -1 ;   // as current -=1;

使輸入條件優先於if條件之上的第一個scanf("%c", &variable) 對於每個if條件,使用if (n == 'n')代替if (scanf(" %c", &n) == 'n') 由於scanf返回輸入數,因此可以成功獲取

即使有這么小的片段,也存在多個問題:

do
{
    printf("Please press n, s, w, or e to find the treasure\n");
    scanf(" %c", &n);
    if (scanf(" %c", &n) == 'n')
    {

您最好使用" %c"來讀取字符。

首先,您無需檢查第一個scanf()是否成功。 理論上它將返回3個值之一(實際上是2個)。 這些值可以是EOF ,如果它檢測結束的文件上的標准輸入, 1如果它讀取一個非空白字符(可能是多個新行和空格和制表符之后),並且通常它可以返回0 ,如果它未能讀取輸入滿足轉換規范,而不會遇到EOF或錯誤。 對於數字轉換規范(例如%d ),如果第一個非空白字符是字母,則可能會發生這種情況; 當轉換規范僅讀取字符( %c )時,不會返回0

然后,在if語句中,您讀取了另一個字符。 但是,由於scanf()僅返回EOF1 ,所以它不會等於'n' ,因此if將失敗,並將移至您讀取另一個字符的else上,依此類推。

暫無
暫無

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

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