![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programmer
Join Date: Dec 2004
Posts: 31
Rep Power: 0
![]() |
Come on, I know you guys know what it means.
-Mike |
|
|
|
|
|
#12 |
|
Programmer
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 5
![]() |
Option Explicit: All they saying is if you want read in a single key from the keyboard ie: user input than use getChar( ). Here is an example:
char c;
printf( "Are you sure you want to exit: [Y/N]");
c = getChar( );
if ( c == 'Y' )
{
// exit program
return 0;
}
if ( c== 'N' )
{
// do something else
}putChar( c ); Is just used to display the character stored in c on the screen.
__________________
Ravilj's OpenGL Terrain aka WinTerrain Last Updated: 17/01/2005! |
|
|
|
|
|
#13 |
|
Programmer
Join Date: Dec 2004
Posts: 31
Rep Power: 0
![]() |
I started to re-read it and it made a bit more sense, but your explanation clarified almost all of it. Thanks a lot Ravilj, this section makes a lot more sense now.
-Mike |
|
|
|
|
|
#14 |
|
Programmer
Join Date: Dec 2004
Posts: 31
Rep Power: 0
![]() |
I have another question! I got more into the getchar and putchar commands and wrote the example code out. Supposedly it counts input lines but I'm not sure what exactly the program does. If anyone could clarify, it would be helpful.
#include <stdio.h>
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%1d\n", nc);
} |
|
|
|
|
|
#15 |
|
Programmer
Join Date: Dec 2004
Posts: 35
Rep Power: 0
![]() |
>Supposedly it counts input lines
A line is any sequence of characters ending with '\n'. The code as posted counts the number of characters, not the number of lines. >while (getchar() != EOF) getchar reads a character. If it's anything but the macro EOF (which designates an error or the end of input) then nc is incremented. If you want to count the number of lines then this would be more appropriate: #include <stdio.h>
int main(void)
{
long int nl = 0; /* Counter for the lines */
int ch; /* Used to save the input character for testing */
/* Read characters until EOF and save them in ch */
while ((ch = getchar()) != EOF) {
/* Is ch an end of line marker? */
if (ch == '\n')
++nl;
}
/* Match the format modifier with the variable type. */
/* %ld is used for long int, not %d */
printf("# of lines: %ld\n", nl);
/* Always return a value! */
return 0;
} |
|
|
|
|
|
#16 |
|
Programmer
Join Date: Dec 2004
Posts: 31
Rep Power: 0
![]() |
Thank you Tama; I was actually looking at the wrong page when I entered the previous code, so it makes much more sense now. Thank you for clarifying this for me. Oh and the book I am using never mentioned returning zero, but I understand what it does, so I shall make sure I include that in my writing.
-Mike |
|
|
|
|
|
#17 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
i personally use Dev-C++ as an editor and i use borland (free command-line tools) as a compiler. ooble has posted a reply from me earlier on this forum about a simple way to configurate borland so that it is very simple to use.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|