So I feel like I don't really understand the getchar()
function very well... What I thought the code would do is, if there happens to be a space it will just "eat" that space with the getchar()
function. And if there happens to not be a "p" in the input (as in 1:30 pm), then it will just keep "eating" the input until there is only '\n'
left. And at that point it will end the while loop. But anyways, here's the code:
int main(void)
{
int hour, min;
char ch;
printf("Enter a 12-hour time: ");
scanf("%d : %d", &hour, &min);
while ((ch = getchar()) != '\n')
{
if (toupper(ch) == 'P')
{ hour += 12; }
getchar();
}
printf("24-hour time: %d:%d", hour, min);
return 0;
}