Jump to content
  • Advertisement
Sign in to follow this  
snm04

quick c question

This topic is 4351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

// 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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!