我只是 C/編程領域的菜鳥/新手。我將首先發布代碼,然后我將描述我的問題。
#include <stdio.h>
#include <string.h>
int
main ()
{
char f[30], j[30] = "mount everest";
int s,a=0;
printf ("\n");
printf ("2) Which is the tallest mountain in the world?\n");
printf ("\n");
printf ("Please type your answer below\n");
scanf("%[a-z]s",f);
s = strcmp (f, j);
printf ("\n");
if (s == 0)
{
printf ("Congratulations! You have recieved a point!\n");
a ;
}
else
{
printf ("Sorry! Better luck next time!");
a--;
}
return 0;
}
因此,當我嘗試將答案輸入為“mount everest”時,輸出會直接轉到我的“else”(抱歉!下次運氣更好!)陳述句。另外,我希望程式只接受小寫字母(因為我已經將它定義為在整個測驗中要遵循的嚴格規則)。
**OUTPUT**
2) Which is the tallest mountain in the world?
Please type your answer below
mount everest
Sorry! Better luck next time!
但是當我在 if 條件中使用 (not)'!s' 而不是 's' 時,它滿足 if 條件并將訊息列印為“恭喜!您已獲得一分!'。我真的不知道這是怎么回事?我對其他 2 個問題采用了相同的方法,沒有這個“!”,它們都可以完美運行。問題。
#include <stdio.h>
#include <string.h>
int
main ()
{
char f[30], j[30] = "mount everest";
int s,a=0;
printf ("\n");
printf ("2) Which is the tallest mountain in the world?\n");
printf ("\n");
printf ("Please type your answer below\n");
scanf("%[a-z]s",f);
s = strcmp (f, j);
printf ("\n");
if (!s == 0)
{
printf ("Congratulations! You have recieved a point!\n");
a ;
}
else
{
printf ("Sorry! Better luck next time!");
a--;
}
return 0;
}
**OUTPUT**
2) Which is the tallest mountain in the world?
Please type your answer below
mount everest
Congratulations! You have recieved a point!
So someone can please explain this to me of what is happening(as I'm new so I kinda don't know how to understand debugging) and what am I doing wrong here? Also, if you cannot understand from this section of the code, I'll post the whole code here but for now I thought it would be too overwhelming so if u want then pleasedo let me know! Thank you for taking the time to go through my problem. Any help is very much appreciated.
EDIT
I programmed the code again with the help of such useful comment from you guys/contributers(thank u all for taking the time to help this newbie) now I'll show you the revised/new code first:
#include <stdio.h>
#include <string.h>
int
main ()
{
char f[30], j[30] = "mount everest";
int s,a=0;
printf ("\n");
printf ("2) Which is the tallest mountain in the world?\n");
printf ("\n");
printf ("Please type your answer below\n");
fgets(f,15,stdin);
printf("%s\n",f);
printf("%s\n",j);
printf ("\n");
s=strcmp(f,j);
if (s==0)
{
printf ("Congratulations! You have recieved a point!\n");
a ;
}
else
{
printf ("Sorry! Better luck next time!");
a--;
}
return 0;
}
But now I'm again being thrown back at the else condition. Like this:
Output
2) Which is the tallest mountain in the world?
Please type your answer below
mount everest
mount everest
mount everest
Sorry! Better luck next time!
I know there is an issue with the 'if' condition but can't seem to figure out what it is. So if anyone can detect my error then I would be really glad. Once again thank you all for helping me.
uj5u.com熱心網友回復:
你的程式不作業的原因是你的 scanf 沒有把整行作為輸入。相反,它采用第一個詞。嘗試使用 scanf("%[^\n]%*c",f);
將來當您的代碼不起作用時,請嘗試列印并驗證您收到的輸入并從中進行驗證。
uj5u.com熱心網友回復:
scanf("%[a-z]s",f);
應該
scanf("%[a-z ]",f);
%[a-z]意味著尋找一個非空的小寫字符序列。你忘了告訴它尋找空間。
也%[a-z]s意味著尋找一個非空的小寫字符序列,后跟一個“s”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/347694.html
標籤:c string if-statement char stdio
