View Single Post
Old 04-24-2005, 04:10 PM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
redhead is on a distinguished road
Alright, here is my version of this program..
What it is supposed to do with the otehr 4 selections in the menu, I'm not that involved with.
Code:
/* A good description of the program should always be at the beginning
 *
 * Program name: Wordgame
 * Author:       Valerie
 * Date:         April 24. 2005
 * Usage:        Wordgame
 *               The rest is delth with by 
 *               questions asked to the user
 *
 * Description:  This program will ask the user to provide
 *               it with a word, this word will be repeated
 *               and followed by the word spelled backwards.
 *               It will not be allowed to provide the program 
 *               with more than one word at a time.
 *               Whould the input given, contain more than one
 *               word, it is _only_ the first word that will be 
 *               repeated and spelled backwards.
 *
 * Return value: The following return values will be given
 *                0 -> success
 *               -1 -> Error getting input from user
 *               -2 -> Error trying to reverse word from user
 */

#include <stdio.h>   /* provides access to fgets()/printf()/fflush() */
#include <string.h>  /* provides access to strlen() */
#include <stdlib.h>  /* provides access to atoi() */

/* Since we like to make this program only
 * read words of upto 20 chars in length,
 * It is essential that we keep that limit
 * strict
 */
#define WORD_LENGTH 20

 
/* I never belive in functions returning void
 * It's better to let them return an int
 * especialy in this case, the display_menu will
 * return the selection the user makes as an integer.
 * when 0 is returned the user selected exit/quit
 */
int display_menu(void);


/* It is required to hava a function, which gets the word
 * needed in order to spell it backwards.
 * This will also check for any error in the user input
 * so there will only be _one_ word to spell backwards.
 * The word provided by the user will be in the argument
 * given to the function, after a call to this function.
 */
int get_word(char* word);


/* Since this is the wordgame program, which is intended to
 * spell the provided word backwards, we need to have a function
 * that knows how to reverse a word.
 * The first argument is the word intended for reversing,
 * the second argument will be holding the reversed word after
 * a call to this function.
 */
int reverse_word(char* orrig_word, char* rev_word);


/* This is where it is all combined, give a clean run with as much 
 * responsebility spread out across all the different function,
 * which makes the main function as clean and slinder as possible.
 */
int main(void)
{
  char normal_word[WORD_LENGTH +1], reversed_word[WORD_LENGTH +1];
  int user_choice;
  
  while(0 < (user_choice = display_menu()))
    {
      switch(user_choice)
	{
	case 1:
	  /* what ever the program should do when user selects 1 */
	  break;
	case 2:
	  /* what ever the program should do when user selects 2 */
	  break;
	case 3:
	  /* Our world famous word reverse program weee... */
	  printf("\nWelcome the the wordgame of reversing words\n");
	  printf("Your word to be reversed: ");
	  fflush(stdout);
	  if(get_word(normal_word))
	    {
	      printf("Error reading the word\n");
	      return -1;
	    }
	  if(reverse_word(normal_word, reversed_word))
	    {
	      printf("Error reversing word\n");
	      return -2;
	    }
	  printf("Provided word: %s\n", normal_word);
	  printf("Reversed word: %s\n", reversed_word);
	  break;
	case 4:
	  /* what ever the program should do when user selects 4 */
	  break;
	case 5:
	  /* what ever the program should do when user selects 5 */
	  break;
	default:
	  /* Some unknown input, user_choise is holding it */
	  printf("Sorry, the provided choice [%d] is unknown.\n", user_choice);
	  break;
	}
      /* let the user know, we're about to continue */
      printf("Press enter to continue.\n");
      /* make sure nothing is held up in the out stream */
      fflush(stdout);
      /* fetch the users "enter" for continued running */
      fgets(normal_word, WORD_LENGTH, stdin);
      /* make sure there's no garbadge in normal_word, for future read */
      normal_word[0] = '\0';
    }
  
  /* Should there have been an error, let the program reflect that */
  return user_choice;
}


/* This is the user interaction, making sure the user only inputs 
 * valid selection, adn cleaning up the stdin/stdout stream for
 * any following requests.
 * the function will basicaly provide the user with a menu, and 
 * return the value given from the user, shoudl that be a number
 * a char or anything else, if the user gives q/Q it will return 0
 * indicating a successful return, thus basis for program termination
 */
int display_menu(void)
{
  char choice[WORD_LENGTH +1];

  /* make the user aware of what choices this program provides */
  printf("\t\t.---------------------.\n");
  printf("\t\t| Welcome to Wordgame |\n");
  printf("\t\t|---------------------|\n");
  printf("\t\t| 1) Sequential update|\n");
  printf("\t\t| 2) Control break    |\n");
  printf("\t\t| 3) Word Game 1      |\n");
  printf("\t\t| 4) Word Game 2      |\n");
  printf("\t\t| 5) Some action      |\n");
  printf("\t\t| q) QUIT             |\n");
  printf("\t\t'---------------------'\n");
  printf("\t\t Your choice: ");
  fflush(stdout);
  if(!fgets(choice, WORD_LENGTH, stdin))
    {
      printf("Error getting selection from user\n");
      return -1;
    }
  if(choice[0] == 'q' || choice[0] == 'Q')
    return 0;
  return atoi(choice);
}


/* Here the read of the user provided word will be set,
 * anything not conforming with somethign that whoudl be 
 * in a word, will make it stop readign the word provided.
 */
int get_word(char* word)
{
  char buffer[WORD_LENGTH +1];
  int i = 0;
  if(!fgets(buffer, WORD_LENGTH, stdin))
    return -1;
  fflush(stdin);
  /* make sure only the very first word gets returned */
  for( ;  buffer[i] &&
        buffer[i] != '\t' &&
        buffer[i] != ' '  &&
        buffer[i] != '\r' &&
        buffer[i] != '\n'; ++i)
    word[i] = buffer[i];
  word[i] = '\0';
  return 0;
}


/* Reverse the word given.
 */
int reverse_word(char* orrig_word, char* rev_word)
{
  /* since get_word() strips it for '\n' we correct
   * the length for the '\0' which will be there 
   */
  int i = 0, j = strlen(orrig_word) -1;
  if(j <= 0)
    return -1;
  for( ; j >= 0; )
    rev_word[i++] = orrig_word[j--];
  /* make sure reversed word isn't ending in garbadge */
  rev_word[i] = '\0';
  return 0;
}
Sorry, but at this hour of teh night, I'm not upto coloring every preserved word in the program and such.. So you will have to do with only the comments beeing colored.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote