Thread: My program "truck"

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    12

    My program "truck"

    Hello every one, I'm from Egypt and I'm trying to learn programming to be real programmer, I'm still beginner, I started with C and I'm planning to C++, and this is a program, a try of me to do something with C, I programmed it with Turbo C++ of DOS, there's no errors, I tested it, I'll be happy with any advice.


    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void fun_com(); //The commands processing function
    void fun_graph(); //The scene graph
    void fun_prompt(); //Receiving orders
    void fun_clearline(); //Clean lines for replies
    enum boolean
    {
     false,
     true
    };
    //The first variables of the scene
    enum boolean holding=false;
    int tboxes=10,sboxes=0;
    char *where="truck";
    char *commands;
    int you=1;
    void main()
    {
     fun_graph(); //Drawing the scene
     while(true)
     {
      fun_prompt(); //Receiving orders
      fun_com(); //Orders processing and making reactions
     }
    }
    //The commands processing function
    void fun_com()
    {
      //The truck direction order
      if(!stricmp(commands,"goto truck"))
         if(!stricmp(where,"truck"))
           {
     fun_clearline();
     printf("You're already beside the truck.");
           }
         else
           {
     where="truck";
     gotoxy(10,6);
     printf(" ");
     gotoxy(38,6);
     printf("%c",(char)you);
           }
      //The store direction order
      else if(!stricmp(commands,"goto store"))
       if(!stricmp(where,"store"))
         {
          fun_clearline();
          printf("You're already on the store.");
         }
       else
         {
          where="store";
          gotoxy(38,6);
          printf(" ");
          gotoxy(10,6);
          printf("%c",(char)you);
         }
      //Where you're order
      else if(!stricmp(commands,"where am I"))
       if(!stricmp(where,"truck"))
         {
          fun_clearline();
          printf("You're beside the truck.");
         }
       else
          {
           fun_clearline();
           printf("You're on the store.");
          }
      //The bringing up order
      else if(!stricmp(commands,"bring up"))
       if(holding==false && !stricmp(where,"truck") && tboxes>0)
         {
          tboxes--;
          holding=true;
         }
      else if(holding==false && !stricmp(where,"store") && sboxes>0)
      {
       sboxes--;
       holding=true;
      }
      else if(holding==false && !stricmp(where,"truck") && tboxes==0)
      {
       fun_clearline();
       printf("There's no boxes in the truck.");
      }
      else if(holding==false && !stricmp(where,"store") && sboxes==0)
      {
       fun_clearline();
       printf("There's no boxes on the store.");
      }
      else
      {
      fun_clearline();
      printf("You're already have a box.");
      }
      //The putting down order
      else if(!stricmp(commands,"put down"))
       if(holding==true && !stricmp(where,"truck"))
         {
          tboxes++;
          holding=false;
         }
       else if(holding==true && !stricmp(where,"store"))
       {
        sboxes++;
        holding=false;
       }
       else
      {
      fun_clearline();
      printf("You don't hold any box.");
      }
      //What I've order
      else if(!stricmp(commands,"what I've"))
       if(holding==true)
         {
          fun_clearline();
          printf("You're holding a box.");
         }
       else
         {
          fun_clearline();
          printf("You're not holding any box.");
         }
      //The how many order
      else if(!stricmp(commands,"how many"))
      {
       fun_clearline();
       printf("Truck boxes = %d, Store boxes = %d",tboxes,sboxes);
      }
      //The termination order
      else if(!stricmp(commands,"exit"))
       exit(0);
      else
     {
     fun_clearline();
     printf("You entered uncorrect order.");
     }
    }
    //The scene graph
    void fun_graph()
    {
     int u_left_cor=201;
     int u_right_cor=187;
     int d_left_cor=200;
     int d_right_cor=188;
     int horizontal=205;
     int vertical=186;
    
     int counter;
     clrscr();
     //The big box
     gotoxy(1,1);
     printf("%c",(char)u_left_cor);
     for(counter=2;counter<60;counter++)
        {
         gotoxy(counter,1);
         printf("%c",(char)horizontal);
        }
     gotoxy(60,1);
     printf("%c",(char)u_right_cor);
     for(counter=2;counter<12;counter++)
        {
         gotoxy(60,counter);
         printf("%c",(char)vertical);
        }
     gotoxy(60,12);
     printf("%c",(char)d_right_cor);
     for(counter=59;counter>1;counter--)
        {
         gotoxy(counter,12);
         printf("%c",(char)horizontal);
        }
     gotoxy(1,12);
     printf("%c",(char)d_left_cor);
     for(counter=11;counter>1;counter--)
        {
         gotoxy(1,counter);
         printf("%c",(char)vertical);
        }
     //The truck
       //The trunck
     gotoxy(40,4);
     printf("%c",(char)u_left_cor);
     for(counter=41;counter<50;counter++)
        {
         gotoxy(counter,4);
         printf("%c",(char)horizontal);
        }
     gotoxy(49,4);
     printf("%c",u_right_cor);
     gotoxy(49,5);
     printf("%c",(char)vertical);
     gotoxy(49,6);
     printf("%c",(char)d_right_cor);
     gotoxy(48,6);
     printf("%c",(char)horizontal);
     gotoxy(47,6);
     printf("*");
     for(counter=46;counter>42;counter--)
        {
         gotoxy(counter,6);
         printf("%c",(char)horizontal);
        }
     gotoxy(42,6);
     printf("*");
     gotoxy(41,6);
     printf("%c",(char)horizontal);
     gotoxy(40,6);
     printf("%c",(char)d_left_cor);
     gotoxy(40,5);
     printf("%c",(char)vertical);
       //The truck head
     gotoxy(51,4);
     printf("%c",(char)u_left_cor);
     for(counter=52;counter<55;counter++)
        {
         gotoxy(counter,4);
         printf("%c",(char)horizontal);
        }
     gotoxy(55,4);
     printf("%c",(char)u_right_cor);
     gotoxy(55,5);
     printf("%c",(char)vertical);
     gotoxy(55,6);
     printf("%c",(char)d_right_cor);
     gotoxy(54,6);
     printf("%c",(char)horizontal);
     gotoxy(53,6);
     printf("*");
     gotoxy(52,6);
     printf("%c",(char)horizontal);
     gotoxy(51,6);
     printf("%c",(char)d_left_cor);
     gotoxy(51,5);
     printf("%c",(char)vertical);
     gotoxy(50,6);
     printf("%c",(char)horizontal);
     //The store
     gotoxy(5,3);
     printf("%c",(char)u_left_cor);
     for(counter=6;counter<18;counter++)
        {
         gotoxy(counter,3);
         printf("%c",(char)horizontal);
        }
     gotoxy(18,3);
     printf("%c",(char)u_right_cor);
     for(counter=4;counter<6;counter++)
        {
         gotoxy(18,counter);
         printf("%c",(char)vertical);
        }
     for(counter=7;counter<9;counter++)
        {
         gotoxy(18,counter);
         printf("%c",(char)vertical);
        }
     gotoxy(18,9);
     printf("%c",(char)d_right_cor);
     for(counter=17;counter>5;counter--)
        {
         gotoxy(counter,9);
         printf("%c",(char)horizontal);
        }
     gotoxy(5,9);
     printf("%c",(char)d_left_cor);
     for(counter=8;counter>3;counter--)
        {
         gotoxy(5,counter);
         printf("%c",(char)vertical);
        }
     //You
     gotoxy(38,6);
     printf("%c",(char)you);
     //The command list
     gotoxy(62,1);
     printf("Commands:");
     gotoxy(62,3);
     printf("\"goto store\"");
     gotoxy(62,4);
     printf("\"goto truck\"");
     gotoxy(62,5);
     printf("\"where am I\"");
     gotoxy(62,6);
     printf("\"bring up\"");
     gotoxy(62,7);
     printf("\"put down\"");
     gotoxy(62,8);
     printf("\"how many\"");
     gotoxy(62,9);
     printf("\"what I've\"");
     gotoxy(62,10);
     printf("\"exit\"");
    }
    void fun_prompt()
    {
     int counter;
     for(counter=1;counter<81;counter++)
         {
          gotoxy(counter,15);
          printf(" ");
         }
      gotoxy(1,15);
      printf(">");
      gets(commands);
    }
    void fun_clearline()
    {
     int counter;
     for(counter=1;counter<81;counter++)
         {
          gotoxy(counter,16);
          printf(" ");
         }
     gotoxy(1,16);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I programmed it with Turbo C++ of DOS
    My first suggestion is that you get a modern C/C++ compiler that is designed to work on a modern operating system. Using a decades old tool for an operating system that was obsolete decades ago will not help you learn to program on current operating systems. And note in modern C or C++ main() is required to return an int, void main() is wrong.

    Next stop using all of those global variables, learn to pass the required variables to and from your functions as required.


    Jim

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > there's no errors, I tested it

    OK, how about
    > char *commands;
    ...
    > gets(commands);
    You don't initialise commands to point to any valid memory, so instead you overwrite something else instead.

    Never assume that "it works" is synonymous with "bug free".

    > I'm trying to learn programming to be real programmer,
    Unless your career ambition is museum curator, then you really need to be learning current technologies (not fossils like TurboC and DOS).
    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.

  4. #4
    Registered User
    Join Date
    Feb 2017
    Posts
    12
    Quote Originally Posted by jimblumberg View Post
    My first suggestion is that you get a modern C/C++ compiler that is designed to work on a modern operating system. Using a decades old tool for an operating system that was obsolete decades ago will not help you learn to program on current operating systems. And note in modern C or C++ main() is required to return an int, void main() is wrong.

    Next stop using all of those global variables, learn to pass the required variables to and from your functions as required.


    Jim
    1. "get a modern C/C++ compiler"
    I'm using this one in this link Turbo C++ or C for Windows 7, 8, 8.1 and 10 32/64-bit Full Screen Free Download - Home I think it's good for beginner

    2. "note in modern C or C++ main() is required to return an int"
    I'll do it next time, ok "int main()"

    3. "stop using all of those global variables"
    I've to use 'em, or I'll have a problem in "fun_com();" for example, if you have another way to make this program to work without general veriables I'll take it.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I'm using this one in this link Turbo C++ or C for Windows 7, 8, 8.1 and 10 32/64-bit Full Screen Free Download - Home I think it's good for beginner
    No that is a link to an outdated compiler and hacks to make it "run" in modern Windows. It is a horrible compiler for learning the current C or C++ languages. Either get Visual Studio (not great for C but better than Turbo C, but quite adequate for C++), or get a current version of Mingw system that includes the gcc tool chain.

    I've to use 'em, or I'll have a problem in "fun_com();" for example, if you have another way to make this program to work without general veriables I'll take it.
    You need to learn to properly pass variables to and from your functions, not use horrible global variables. Functions can take parameters and return values, use these features to pass the variables to and from your functions.


    Jim

  6. #6
    Registered User
    Join Date
    Feb 2017
    Posts
    12
    Quote Originally Posted by Salem View Post
    > there's no errors, I tested it

    OK, how about
    > char *commands;
    ...
    > gets(commands);
    You don't initialise commands to point to any valid memory, so instead you overwrite something else instead.

    Never assume that "it works" is synonymous with "bug free".

    > I'm trying to learn programming to be real programmer,
    Unless your career ambition is museum curator, then you really need to be learning current technologies (not fossils like TurboC and DOS).
    "OK, how about
    > char *commands;
    ...
    > gets(commands);", it's legal in Turbo C++ 3, but I tried it in Dev C++(modern compiler) and it refused to work, I think I understand you.

  7. #7
    Registered User
    Join Date
    Feb 2017
    Posts
    12
    Quote Originally Posted by jimblumberg View Post
    No that is a link to an outdated compiler and hacks to make it "run" in modern Windows. It is a horrible compiler for learning the current C or C++ languages. Either get Visual Studio (not great for C but better than Turbo C, but quite adequate for C++), or get a current version of Mingw system that includes the gcc tool chain.


    You need to learn to properly pass variables to and from your functions, not use horrible global variables. Functions can take parameters and return values, use these features to pass the variables to and from your functions.


    Jim
    I think I'll change my compiler, thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  2. Replies: 2
    Last Post: 08-19-2012, 06:15 AM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread