I have the following program, which compares three characters and outputs their comparison.
When I run the program, whenever I input three integers only the last else (all initials are different) statement runs correctly for its condition. But, for other conditions, only the last else if (first and last initials are the same) runs. I also checked it after adding curly braces but nothing changed.
#include<stdio.h>
#include<stdlib.h>
int main()
{
char ch1, ch2, ch3;
printf("Enter 3 character values into ch1, ch2, and ch3: ");
scanf("%c%c%c", &ch1, &ch2, &ch3);
if(ch1==ch2)
{
if(ch2==ch3)
printf("All initials are the same!\n");
else
printf("First two initials are the same!\n");
}
else if(ch2==ch3)
{
printf("Last two initials are the same!\n");
}
else if(ch1==ch3)
{
printf("First and last initials are the same!\n");
}
else
{
printf("All initials are different!\n");
}
system("pause");
return 0;
}
Aucun commentaire:
Enregistrer un commentaire