someone could help, I trying to code, a program to read a char, and output 1 to lower char, 2 to Upper and -1 if it's not a letter. The program should keep working until the input be a Zero
I can't understand why the following code; keeping print -1 in all situations of the "if" block;
I try two ways:
first try:
#include <stdio.h>
int main() {
char entrada = 'x';
while (entrada != 48) {
scanf("%c", &entrada);
if (entrada > 96 && entrada < 123) {
printf("%d\n", 1);
} else if (entrada > 64 && entrada < 91) {
printf("%d\n", 2);
} else {
printf("%d\n", -1);
}
}
return 0;
}
second try:
#include<stdio.h>
int main() {
char entrada = 'x';
int teste;
while (entrada != 48){
scanf("%c", &entrada);
if (entrada > 96 && entrada <123){
printf("%d\n", 1);
continue;
} else {
if (entrada > 64 && entrada < 91) {
printf("%d\n", 2);
continue;
} else if (entrada <= 64 || (entrada >= 91 && entrada <= 96)
|| entrada >= 123 ) {
printf("%d\n", -1);
continue;
}
}
}
return 0;
}
When your program (either of them) executes scanf("%c", &entrada);
, scanf
reads a single character.
When you type “a” and press Enter or Return, two characters are sent to your program: “a” and a new-line character.
When your program reads “a“, it prints “1”.
When your program reads the new-line character, it prints “-1”.
So, as long as you enter a character and press Enter or Return, your program prints two numbers: one for the first character and one for the new-line character.