Computer Science Canada

dos utility

Author:  alpesh [ Sat Oct 02, 2004 10:22 am ]
Post subject:  dos utility

Laughing Laughing Laughing

turbo c++
-----------------

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
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<'1') || (ch>'6'));
if(ch=='6')
break;
system(command[ch - '1']);
}
}

Author:  Dan [ Sat Oct 02, 2004 11:15 am ]
Post subject: 

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.

Author:  JHanson90 [ Sat Oct 02, 2004 5:58 pm ]
Post subject: 

alpesh, you might consider enclosing your source in [code] tags to make it appear correctly on the page, so it's more readable.

Author:  wtd [ Sat Oct 02, 2004 6:03 pm ]
Post subject: 

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.

Author:  wtd [ Sat Oct 02, 2004 6:35 pm ]
Post subject: 

And, for educational porpoises, the whole thing cleaned up.

code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>

/*******************************************************************/

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 <= length; option_number++)
        {
                output_padded_number(option_number, width_of_option_numbers, ' ');
                printf(". %s\n", options[option_number - 1]);
        }
}

bool get_option(int length, int * option)
{
        int temp;
        scanf("%d", &temp);

        if (temp > 0 && temp <= length)
        {
                *option = temp;
                return true;
        }
        else
        {
                return false;
        }
}


: