Unix help system

This is a discussion on Unix help system within the C Programming forums, part of the General Programming Boards category; Hi, I have a fully functioning help system, no bugs (that i can see), no errors. So i took it ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    41

    Unix help system

    Hi,

    I have a fully functioning help system, no bugs (that i can see), no errors. So i took it to the lecturer and tells me that I'm gonna get marked down for using global variables. He wants me to initiate the char arrays in the main function and use the user inputs as arguments for the functions. I know i am capable and able to strip it out and do it all again, but i was wondering if all of you people that can actually do this like writing english (I'm sure some of you are more coherant in C...) could have a look, and maybe see if there was a trick, or a shortcut.

    basically the only thing i need to do to it is to put the initialised char arrays in the main function and use the user input from the main function (IE the comparitive string) and use that as an argument for the function.

    the codes quite long, but complete and below:

    Code:
    /*
    
    /* Interactive help system to train users into unix, 
       using searches within strings and functions and all of it*/
    /*mitchell kent, 323786*/
    
    #include <stdio.h>
    #include <string.h>
    
    char command[6][5]={
       {"ls"},{"cd"},{"mv"},
       {"cp"},{"rm"},{"more"}};
    char description[6][42]={
       {"list files in a directory"},
       {"change directory"},
       {"rename or move files to another directory"},
       {"copy files"},
       {"delete files"},
       {"display the contents of files"}};
    
    
    
    
    /*this function searches for an inputted command 
       and prints out the assc. description*/
    
    int lookup(void)
    {
    
    	int loop,notfound=0;
    	char comparison[5];
    
    	printf("Please enter command to look up");
    	printf("\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n");
    
    	scanf("%s",comparison);
    
    
    	for(loop=0;loop<=5;loop++)
    	{
    		if (strcmp(comparison, command[loop]) == 0)
    		{
    			printf("\n\nyour command was found:\n");
    			printf("%s - %s",command[loop],description[loop]);
    
    			notfound=1;
    		}
    		
    	}
    
    	if(notfound!=1)
    		printf("Sorry, yourr command was not found");
    
    
    
    
    	return 1;
    }
    
    /*this prog will find words within the description 
       and print out all commands with that word in*/
    
    
    
    
    int findWID(void)
    {
    
    	int loop,notfound=0;
    	char comparison[5];
    
    	printf("Please enter words to look up");
    	printf("\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n");
    
    	scanf("%s",comparison);
    
    
    	for(loop=0;loop<=5;loop++)
    	{
    		if (strstr(description[loop], comparison) != 0)
    		{
    			printf("\n\nyour word(s) can be found below:\n");
    			printf("%s - %s",command[loop],description[loop]);
    
    			notfound=1;
    		}
    		
    	}
    
    	if(notfound!=1)
    		printf("Sorry, your word was not found");
    
    
    
    
    	return 1;
    }
    
    
    
    
    /*this function will display the list of commands available 
       for this help system*/
    
    int disp_all_commands(void)
    {
    
    	int loop;
    
    	for(loop=0;loop<=5;loop++)
    	{
    		printf("%s",command[loop]);
    		printf("\n");
    	}
    
    	return 1;
    }
    
    /*this function prints out all commands and their descriptions*/
    
    int disp_all(void)
    {
    
    	int loop;
    
    	for(loop=0;loop<=5;loop++)
    	{
    		printf("%s - %s",command[loop],description[loop]);
    		printf("\n");
    	}
    
    
    
    	return 1;
    }
    
    
    /*this main function will be a simple switch clause, 
       to choose the required function*/
    
    
    int main(void)
    {
    
    	int choice,exit=0;
    	char command[6][5]={
    		{"ls"},{"cd"},{"mv"},
    		{"cp"},{"rm"},{"more"}};
    	char description[6][42]={
    		{"list files in a directory"},
    		{"change directory"},
    		{"rename or move files to another directory"},
    		{"copy files"},
    		{"delete files"},
    		{"display the contents of files"}};
    
    	
    	
    	
    	printf("Welcome to the UNIX help system:\n");
    	printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n");
    	
    	do
    	{
    
    		printf("\n\n\nPlease choose you method of help:\n");
    		printf("---------------------------------\n\n");
    	
    		printf("1 - Search for a specific command\n");
    		printf("2 - Search for a word within the description\n");
    		printf("3 - Display a list of commands\n");
    		printf("4 - Display a list of all commands and descriptions\n");
    		printf("5 - Exit\n\n");
    	
    		printf("Choice:");
    		scanf("%d",&choice);
    
    		switch(choice)
    		{
    		case 1: lookup();
    			break;
    	
    		case 2: findWID();
    			break;
    	
    		case 3: disp_all_commands();
    			break;
    	
    		case 4: disp_all();
    			break;
    	
    		case 5:	exit=1;
    			break;
    
    		default: printf("Your entry was not valid\n\n\n");
    			break;
    		
    		}
    
    	}while(exit!=1);
    
    	return 0;
    
    }
    
    */
    I can strip it, but the deadline is nearing, so i really would appreciate any help on this,

    Cheers!

    Mitch
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    A couple of tips
    1. Don't use tabs in your code, use 4 spaces.
    The board gets carried away with the indentation if you have too many tabs

    2. Format some of the longer lines - I've edited your post to fold some of the long lines

    > marked down for using global variables
    What is worse is that you have globals and locals of the same name, containing the same information.

    If you passed your command and description arrays (the ones in main) to your functions, then you could easily delete your globals.

    Code:
    char command[6][5]={
       {"ls"},{"cd"},{"mv"},
       {"cp"},{"rm"},{"more"}};
    Is much easier to write as
    Code:
    char *command[6]={
       {"ls"},{"cd"},{"mv"},
       {"cp"},{"rm"},{"more"}};
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    41
    Cheers, I got it in on time. Basically i spoke to one fo the teaching assistants, i get marked down for using globals but dont get marked up for using the functions, so i removed the functions and put it all in the switch clauses. looks a lot messier but it does the trick. In my original post the only reason i have both L and G variables was because i had forgotten to remove one of them.

    I will take heed of the indentations advice, but a lot of the structure comes from visual studio, it puts all the tabs in for me.

    Cheers, and good luck to all for the xmas comp's. I'm looking forward to seeing how they go!

    Mitch
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  2. School Mini-Project on C/C++ (Need Your Help)..
    By EazTerence in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 01:08 AM
  3. Replies: 4
    Last Post: 06-13-2005, 09:03 AM
  4. question about open file in unix system
    By wu7up in forum C Programming
    Replies: 6
    Last Post: 03-13-2003, 05:20 AM
  5. AIX unix system script (need help)
    By Liam Battle in forum Linux Programming
    Replies: 4
    Last Post: 02-10-2002, 04:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21