By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

help me understand this function

momotaro
100+
P: 357
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
Share this Question
Share on Google+
6 Replies


AdrianH
Expert 100+
P: 1,251
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
100+
P: 357
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
Expert 100+
P: 1,251
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
Expert Mod 5K+
P: 9,197
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
100+
P: 357
Expand|Select|Wrap|Line Numbers
  1. printf("thank you!");


p.s: trial :)
Jun 2 '07 #6

Savage
Expert 100+
P: 1,764
Expand|Select|Wrap|Line Numbers
  1. printf("thank you!");


p.s: trial :)

Original!!

Savage
Jun 2 '07 #7

Post your reply

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