
-----------------------------------
alpesh
Sat Oct 02, 2004 10:22 am

dos utility
-----------------------------------
:lol:  :lol:  :lol: 

turbo c++
-----------------

#include
#include
#include
void main()
{
	   char *command[]={
	   "DIR",
	   "CHKDSK",
	   "TIME",
	   "DATE",
	   "LABEL"
	   };
	   char ch;
	   for( ; ; )
	   {
	  do
	  {
	  int i;
	  clrscr();
	   printf("Prg. To Run DOS Commands\n");
	   printf("1.DIR\n");
	   printf("2.CHKDSK\n");
	   printf("3.TIME\n");
	   printf("4.DATE\n");
	   printf("5.LABEL\n");
	   printf("6.EXIT\n");
	   printf("\nchoice : ");
	   ch=getche();
	   printf("\n");
	   }
	   while((ch'6'));
	   if(ch=='6')
		 break;
	   system(command[ch - '1']);
	   }
}

-----------------------------------
Dan
Sat Oct 02, 2004 11:15 am


-----------------------------------
Wow that is some unportable code....thats only going to work on windows and even then on compliers that only let you do void main. Ushely it is a good idea to use int main so you can return an error code to the OS (witch in this case whould prity much have to be windows...).

Edit: Also the way you are using that for loop is not realy that good progaming, i whould use a while or do loop in it's place.

-----------------------------------
JHanson90
Sat Oct 02, 2004 5:58 pm


-----------------------------------
alpesh, you might consider enclosing your source in [code] tags to make it appear correctly on the page, so it's more readable.

-----------------------------------
wtd
Sat Oct 02, 2004 6:03 pm


-----------------------------------
As I noted in your other threa, but will note here again for emphasis, that is not C++ code, but rather C code.  The two are quite different languages.

-----------------------------------
wtd
Sat Oct 02, 2004 6:35 pm


-----------------------------------
And, for educational porpoises, the whole thing cleaned up.

#include 
#include 
#include 
#include 

/*******************************************************************/

int length_of_int(int n);
void output_padded_number(int number, int total_spaces, char pad_with);
void draw_menu(char * msg, char * options[], int length);
bool get_option(int length, int * option);

/*******************************************************************/

int main()
{
	char * commands[] = {"DIR", "CHKDSK", "TIME", "DATE", "LABEL", "EXIT"};
	int option;

	while (1)
	{
		do
		{
			draw_menu("Prg. To Run DOS Commands", commands, 6);
		} while (!get_option(6, &option));

		if (option == 6)
		{
			break;
		}
		else
		{
			system(commands[option - 1]);
		}
	}
}

/*******************************************************************/

int length_of_int(int n)
{
	if (n == 0)
	{
		return 1;
	}
	else
	{
		int length;
		for (length = 1; n / (int)pow(10, length) > 0; length++) { }
		return length;
	}
}

void output_padded_number(int number, int total_spaces, char pad_with)
{
	int width = length_of_int(number);
	int padding_spaces = total_spaces - width;
	int i;

	for (i = 0; i < padding_spaces; i++)
	{
		printf("%c", pad_with);
	}

	printf("%d", number);
}

void draw_menu(char * msg, char * options[], int length)
{
	int width_of_option_numbers = length_of_int(length);
	int option_number;

	printf("%s\n", msg);

	for (option_number = 1; option_number  0 && temp 