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