Chargen -- A character Generator

Mahen 0 Tallied Votes 346 Views Share

Chargen is a program created entirely by mahen of Mauritius. The simple task of Chargen is to generate characters, but the most important aspect of this particular program is it's abillity to process user input. Due to the way the program was programmed, any input that the user will make will be processed efficiently by the program.

The main task of Chargen is to generate a specified number of a particular type of character in a particular target. To make this more clear, let's look at the usage of Chargen:

Chargen -Help To show this Text
-File To Select the output File
-Stdout To print the Characters on the screen itself
-Exec To execute a program with the specified number of Generated Characters

If you enter -help as command line argument for Chargen, the usage of Chargen will be displayed.

If you enter -file, all the generated characters will be redirected at the output file of the user.

If -stdout was entered, all the generated characters will be displayed on screen.

If -exec was used

//******************************************************************************
//   Title:  Chargen

//   File:   Chargen.exe

//   Description:  A program to generate characters according to user's demand

//   Author:  Mahen

//   Developped:   Pentium 3 733 Mhz, 192MB Ram, Windows XP Pro

//   Target:       Platform Independent

//   Revision History: Took 1 Week to create it from Scratch

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



#include <iostream.h>
#include <stdlib.h>
#include <windows.h>
#include <fstream.h>
#include <string.h>


int main(int argc,  char* argv[])
/* This is the Main() function. The first thing to do is to make sure that a command
has been entered! Once we receive a command, we compare it with all the possible
commands in all the possible cases and work it out! */


{
 int num; //This is used to calculate the number of characters to generate
 char answer[2]; //This is used to store the Y/N answers of the user
 int color;  //This is to change the color of the console
 char output[1024] = "c:\\chargen.txt"; // This is the default output file.
 char outchar[2] = "A";  // This is the default character.

 // This is for color
 HANDLE  Console;
 Console = GetStdHandle(STD_OUTPUT_HANDLE);
 //We First Display a Welcome Message
 color = 14;
 SetConsoleTextAttribute(Console, color);
 printf("Thank You For Using Chargen\nThis Program Has Been Entirely Programmed By Mahen\n\n");
 // We return the color of the console back to normal
 color = 7;
 SetConsoleTextAttribute(Console, color);



 if (argc == 2)
 {



 /* The Default Output Character is "A",so the program will ask the User if he
  wants to change the default character */
 cout << "The Default Output Character is " << outchar <<endl;
 cout << "Do you want to change it: Y/N ";
 cin >> answer;
 if (strcmp(answer, "Y") == 0 || strcmp(answer, "y") == 0)
 {
   cout << "Enter your Character: ";
   cin  >> outchar;
 }



       if (strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "-HELP") == 0 || strcmp(argv[1], "-Help") == 0)
      {
          printf(
          "Enter:  -Help            To show this Text\n"
          "        -File            To Select the output File\n"
          "        -Stdout          To print the Characters on the screen itself\n"
          "        -Exec            To execute a program with the specified number of Generated Characters\n");
     }




      else if (strcmp(argv[1], "-file") == 0 || strcmp(argv[1], "-FILE") == 0 || strcmp(argv[1], "-File") == 0)
       {
          cout << "The default output file is " << output;
          cout << "\nDo you want to change it ? Y/N \n";
          cin>>answer;
          if (strcmp(answer, "Y") == 0 || strcmp(answer, "y") == 0)
          {
            LABEL:
            cout<<"Enter the Output Filename: ";
            cin>>output;
          }
          ifstream FILEEXISTS (output);
          if (FILEEXISTS)
          {
             cout << "File " << output << " Already Exists\n";
             printf("Do you wish to overwrite it? Y/N ");
             cin>>answer;
             if (strcmp(answer, "N") == 0 || strcmp(answer, "n") == 0)
             {
             goto LABEL;
             }
          }
          ofstream FILE (output);
          cout<<"Enter the Number of characters to Generate: ";
          cin>>num;
          for(int alpha = 0; alpha < num; alpha++)
          {FILE<<outchar;}
          printf("\nDone\n");
       }



      else if (strcmp(argv[1], "-stdout") == 0 || strcmp(argv[1], "-STDOUT") == 0 || strcmp(argv[1], "-Stdout") == 0)
       {
          cout<<"Enter the Number of characters to Generate: ";
          cin>>num;
          color = 10;
          SetConsoleTextAttribute(Console, color);
          for(int alpha = 0; alpha < num; alpha++)
          {cout << outchar;}
          color = 7;
          SetConsoleTextAttribute(Console, color);
       }



     else if (strcmp(argv[1], "-exec") == 0 || strcmp(argv[1], "-EXEC") == 0 || strcmp(argv[1], "-Exec") == 0)
       {
         char exec[1024];
         cout << "Enter the Filename that you want to execute: ";
         cin >> exec;
         cout<<"Enter the Number of characters to Generate: ";
         cin>>num;
         char gen[999999];
         for(int alpha = 0; alpha < num; alpha++)
         {
         strcat(gen, outchar);
         }
         strcat(exec, " ");
         strcat(exec, gen);
         color = 10;
         SetConsoleTextAttribute(Console, color);
         cout << exec << endl;
         color = 7;
         SetConsoleTextAttribute(Console, color);
         system(exec);
         }




      else
      // This will occur if the user used a command that is not recognized!
       {
          color = 12;
          SetConsoleTextAttribute(Console, color);
          cout << "Bad Command!!!";
          color = 7;
          SetConsoleTextAttribute(Console, color);
          printf(
          "\nEnter:  -Help            To show this Text\n"
          "        -File            To Select the output File\n"
          "        -Stdout          To print the Characters on the screen itself\n"
          "        -Exec            To execute a program with the specified number of Generated characters\n");
       }
}
else
//This will occur if the user didn't enter any command!
{
color = 12;
SetConsoleTextAttribute(Console, color);
cout << "Please Enter A Command!!!!!";
color = 7;
SetConsoleTextAttribute(Console, color);
printf(
          "\nEnter:  -Help            To show this Text\n"
          "        -File            To Select the output File\n"
          "        -Stdout          To print the Characters on the screen itself\n"
          "        -Exec            To execute a program with the specified number of Generated characters\n");
}

   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.