quick c question

Started by
1 comment, last by Adam Hamilton 17 years, 6 months ago
// Lab3.cpp : Defines the entry point for the console application. //

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
char letter;

printf("Please type a key on the keyboard, and press enter:");

letter = getchar();
printf("You Hit:");
putchar(letter);

printf(" \nDecimal integer is:%d and hex integer is 0x%x \n", letter, letter);
printf("Goodbye!");



getchar();
getchar();
return 0;
}


-------------------- For this code...why do I need two "getchar()" at the end of the code??? I know that if I have one and try to run the program it will close directly after the user puts in a character, but I don't understnad why exactly you need two getchar() functions to prevent the ms dos window from closing....could somebody explain this?
Advertisement
Maybe to eat the carriage return?
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
It have something to do with the way the enter key is translated to the input stream (stdin)... I know that in a text document (saved by a windows app for instance) that an enter is usually translated to an 0x0D followed by an 0x0A (CR and LF respectively) so the first getchar() will eat the LF and the second getchar() will wait for a key.

This topic is closed to new replies.

Advertisement