Reputation: 3
I'm trying to write a program to get a character and then check and print if it is in uppercase or lowercase. Then I want it to keep looping until the user enters a "0" which should produce a message. What doesn't work is the while condition at the bottom, the condition never seems to be met.
#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
}
Upvotes: 0
Views: 169
Reputation: 1837
Try while(ch!=48);
as 48 is the decimal number for the char '0'.
As Maroun Maroun mentioned, while(ch!='0'); is more comprehensible.
If you don't want the uppercase message to get displayed when the user enters a '0', you could do something like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned char ch,nope; // Int ch and nope variable used to terminate program
while (1)
{
printf("Enter a character : "); // asks for user input as a character
scanf("%c",&ch); // stores character entered in variable 'ch'
if(ch==48) {
break;
}
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");
}
}
printf("im ending now \n\n\n\n\n",nope); // shows that you get the terminate line
}
Upvotes: 2