我想寫一個程序來獲取一個字符,然後檢查並打印它是否使用大寫或小寫。然後,我希望它保持循環,直到用戶輸入一個應該產生消息的「0」。不起作用的是底部的條件,這個條件似乎從未得到滿足。這個循環不符合條件時結束
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ch,nope; // Int ch and nope variable used to terminate program
do // start 'do' loop to keep looping until terminate number is entered
{
printf("Enter a character : "); // asks for user input as a character
scanf("%c",&ch); // stores character entered in variable 'ch'
if(ch>=97&&ch<=122) { // starts if statement using isupper (in built function to check case)
printf("is lower case\n");
} else {
printf("is upper case\n");
}
}
while(ch!=0); // sets condition to break loop ... or in this case print message
printf("im ending now \n\n\n\n\n",nope); // shows that you get the terminate line
}
'CH = 0'是錯誤的,應該是' ch!='0'' – Maroun 2013-03-17 21:53:17
幻數97和122是個不錯的主意。使用''a''和''z'',或者只需調用''中聲明的'islower()'。此外,代碼還會爲數字和標點符號報告「大寫」。 –
2013-03-17 22:20:55