GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Oct-2005, 06:24
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Post

Re: Things to Avoid in C/C++ -- scanf / epilogue, Part 9


I just posted a GID Community blog entitled: Things to Avoid in C/C++ -- scanf / epilogue, Part 9.

Quote:
scanf() / So what's a programmer to do?

scanf() is not designed for production code, but for creating test programs to see if other aspects of the program are working. scanf() will replace the complicated input routines so that other modules can be exercised and perfected. Then the scanf() is replaced with the real input function that gets the actual data from a file, a device, wherever the actual data is stored. read more...

Please let me know what you think.

This is an auto-generated message posted by GIDNetwork™ on behalf of the author.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #2  
Old 14-Sep-2008, 12:42
GeertVc GeertVc is offline
New Member
 
Join Date: Sep 2008
Posts: 3
GeertVc is on a distinguished road

Re: Things to Avoid in C/C++ -- scanf / epilogue, Part 9


Quote:
Originally Posted by WaltP
Please let me know what you think.

In the last example, I see this:

CPP / C++ / C Code:
int  getCharacter()
{
    int ch;         // define the character
    do              // loop until a good character is read
    {
        ch = getchar();         // read a character
    } while ((ch <= 0x20) ||    // check above SPACE
             (ch >= 0x7F)       // check below the last 
            );                  //    ASCII char
    return ch;
}
Should the signs in the while() condition not be reversed?

Best rgds,
--Geert
Last edited by admin : 15-Sep-2008 at 00:30. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #3  
Old 14-Sep-2008, 14:13
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Things to Avoid in C/C++ -- scanf / epilogue, Part 9


Quote:
Originally Posted by GeertVc
Should the signs in the while() condition not be reversed?
If you look at a chart of ASCII values:

http://www.asciitable.com/

...you will see that characters below 0x20 are considered non-printable, & 0x7f represents the upper boundary. Given that the code uses a do-while loop, the loop will repeat while the condition is true. In other words, as if the character read is not printable or above DEL, get another character. If the character read is a printable character, exit the loop & return the character as the return value of the function.

Basically, the function ensures that the keypress received as input is a printable ("good") character. If it is not, then repeat ad infinitum until a printable character is received.

You can always compile the code yourself in a test application to verify its behaviour.
  #4  
Old 15-Sep-2008, 12:08
GeertVc GeertVc is offline
New Member
 
Join Date: Sep 2008
Posts: 3
GeertVc is on a distinguished road

Re: Things to Avoid in C/C++ -- scanf / epilogue, Part 9


Quote:
Originally Posted by ocicat
If it is not, then repeat ad infinitum until a printable character is received.

You're right. I completely missed the way the do-while construction has been made...

Best rgds,
--Geert
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Things to Avoid in C/C++ -- scanf / number, Part 8 WaltP C Programming Language 0 27-Sep-2005 23:03
Re: Things to Avoid in C/C++ -- scanf / string, Part 7 WaltP C Programming Language 0 26-Sep-2005 19:12
Re: Things to Avoid in C/C++ -- scanf, Part 5 WaltP C Programming Language 0 20-Sep-2005 20:40
Re: Things to Avoid in C/C++ -- feof(), Part 3 WaltP C Programming Language 0 19-Sep-2005 21:00
Re: Things to Avoid in C/C++ -- fflush(stdin), Part 2 WaltP C Programming Language 0 17-Sep-2005 21:39

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:18.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.