473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help me understand this function

momotaro
357 100+
the function is quite simple but there is one line of it that i did not get the point of its use it the one in bold please help!
Expand|Select|Wrap|Line Numbers
  1. char GetCommand()
  2. {
  3.     char command;
  4.     printf(
  5.         "\n\t\t\t[I]nsert entry\n\n"
  6.         "\t\t\t[P]rint list\n\n"
  7.         "\t\t\t[S]ize of teh list\n\n"
  8.         "\t\t\t[D]elete list\n\n"
  9.         "\t\t\t[C]lear list\n\n"
  10.         "\t\t\t[T]raverse the list\n\n"
  11.         "Select your command and press <Enter> : "
  12.         );
  13.     while(TRUE){
  14.         while((command = getchar()) == '\n')
  15.             ;
  16.         command = tolower(command);
  17.     if(command == 'i' || command == 'p' ||
  18.        command == 's' || command == 'd' ||
  19.        command == 'c' || command == 't'){
  20.            while(getchar() != '\n')
  21.                ;            
  22.                                          return command
  23.     }
  24.     printf("Please enter a valid command");
  25.     }
  26. }
Jun 2 '07 #1
6 1111
AdrianH
1,251 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. char GetCommand()
  2. {
  3.     char command;
  4.     printf(
  5.         "\n\t\t\t[I]nsert entry\n\n"
  6.         "\t\t\t[P]rint list\n\n"
  7.         "\t\t\t[S]ize of teh list\n\n"
  8.         "\t\t\t[D]elete list\n\n"
  9.         "\t\t\t[C]lear list\n\n"
  10.         "\t\t\t[T]raverse the list\n\n"
  11.         "Select your command and press <Enter> : "
  12.         );
  13.     while(TRUE){
  14.         while((command = getchar()) == '\n')
  15.             ;
  16.         command = tolower(command);
  17.     if(command == 'i' || command == 'p' ||
  18.        command == 's' || command == 'd' ||
  19.        command == 'c' || command == 't'){
  20.            while(getchar() != '\n')
  21.                ;            
  22.                                          return command
  23.     }
  24.     printf("Please enter a valid command");
  25.     }
  26. }
The semicolon after the while() indicates an empty body. It would be equivialant to putting empty braces ('{}')

BTW, I'd use comments next time. bolding doesn't show up well.


Adrian
Jun 2 '07 #2
momotaro
357 100+
thank you, i know that but my question was about the usefulness of that line of code since it add no thing to teh correctness!!!
i omited it but my function still works fine!!
please illuminate me
Jun 2 '07 #3
AdrianH
1,251 Expert 1GB
thank you, i know that but my question was about the usefulness of that line of code since it add no thing to teh correctness!!!
i omited it but my function still works fine!!
please illuminate me
Try typing in 'is a grand day' as your command with and without the while loop you highlighted. It should become clear.


Adrian
Jun 2 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1. while((command = getchar()) == '\n')
  2.             ;
  3.  
is the same as:

Expand|Select|Wrap|Line Numbers
  1. while((command = getchar()) == '\n')
  2. {
  3.  
  4. }
  5.  
That is, you stay in the while loop until you stop pressing the enter key and enter a real command.
Jun 2 '07 #5
momotaro
357 100+
Expand|Select|Wrap|Line Numbers
  1. printf("thank you!");


p.s: trial :)
Jun 2 '07 #6
Savage
1,764 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. printf("thank you!");


p.s: trial :)

Original!!

Savage
Jun 2 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: hope | last post by:
Hi Access 97 I'm lost on this code please can you help ================================= Below is some simple code that will concatenate a single field's value from multiple records into a...
6
by: Astroman | last post by:
Hi guys and girls. This is my first time posting here so go easy :) . I was wondering if someone could please interpret how this csum() function works in the following C code. I know that the...
3
by: ash | last post by:
Hey I am new, but I don't have time to intruduce myself yet. I am intro to C++ and this is a programme I have to write. all the direction are here, It will be very nice of someone to figure this...
16
by: Allen | last post by:
I have a class that returns an arraylist. How do I fill a list box from what is returned? It returns customers which is a arraylist but I cant seem to get the stuff to fill a list box. I just...
2
by: Trevor | last post by:
I'd really appreciate it if someone could please help me understand the code below. The only question I have about c_table() is about the t && range( 1 )... line; what does the t && part mean? The...
2
by: RC | last post by:
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_sort2 You can see above link or read below i copy/paste from above link <script type="text/javascript"> function sortNumber(a, b)...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
3
by: TC | last post by:
Hey All, I have some classes that I recently built for a website which uses the HttpWebRequest & HttpWebResponse objects from the System.Net namespace. Basically, the classes rap submitted data...
5
by: mohammaditraders | last post by:
Write a program which overloads a binary Minus (-) operator, The program will contain a class Matrix, This class will contain a private data member Array which store int values. The class will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.