making it portable.....?

This is a discussion on making it portable.....? within the C Programming forums, part of the General Programming Boards category; I have here a simple program... Code: #include<stdio.h> #include<stdlib.h> #include<dos.h> #include<conio.h> int main() { system("cls"); welcome_scr(); authentication_func(); exit_scr(); return 0; ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool making it portable.....?

    I have here a simple program...

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<dos.h>
    #include<conio.h>
    
    int main()
    {
    	system("cls");
    
    	welcome_scr();
    	authentication_func();
    	exit_scr();
    
    	return 0;
    }
    
    int welcome_scr()
    {
    	int x;
    	char spinstr[] = "|/-\\|";
    
    	system("cls");
    
    	do {
    
    	x = 0;
    	while(x < 5)
    	{
    		gotoxy(9,10); printf("%c", spinstr[x]);
    		gotoxy(18,10); printf("%c", spinstr[x]);
    		gotoxy(27,10); printf("%c", spinstr[x]);
    		gotoxy(36,10); printf("%c", spinstr[x]);
    		gotoxy(45,10); printf("%c", spinstr[x]);
    		gotoxy(54,10); printf("%c", spinstr[x]);
    		gotoxy(63,10); printf("%c", spinstr[x]);
    		gotoxy(72,10); printf("%c", spinstr[x]);
    
    		gotoxy(10,10); printf("========");
    		gotoxy(19,10); printf("========");
    		gotoxy(28,10); printf("========");
    		gotoxy(37,10); printf("========");
    		gotoxy(46,10); printf("========");
    		gotoxy(55,10); printf("========");
    		gotoxy(64,10); printf("========");
    
    
    		gotoxy(9,12); printf("%c", spinstr[x]);
    		gotoxy(18,12); printf("%c", spinstr[x]);
    		gotoxy(27,12); printf("%c", spinstr[x]);
    		gotoxy(36,12); printf("%c", spinstr[x]);
    		gotoxy(45,12); printf("%c", spinstr[x]);
    		gotoxy(54,12); printf("%c", spinstr[x]);
    		gotoxy(63,12); printf("%c", spinstr[x]);
    		gotoxy(72,12); printf("%c", spinstr[x]);
    
    		gotoxy(10,12); printf("========");
    		gotoxy(19,12); printf("========");
    		gotoxy(28,12); printf("========");
    		gotoxy(37,12); printf("========");
    		gotoxy(46,12); printf("========");
    		gotoxy(55,12); printf("========");
    		gotoxy(64,12); printf("========");
    
    		gotoxy(7,11); printf("<");
    		gotoxy(74,11); printf(">");
    
    		gotoxy(31,11); printf("Welcome to ABC v1.0");
    
    		delay(2500);
    		x++;
    	}
    
    	}while(!kbhit());
    
    	getch();
    	return 0;
    }
    
    int authentication_func()
    {
    	char passcode[20];
    
    	do {
    
    	system("cls");
    
    	printf("\n\n\tEnter the passcode: ");
    	gets(passcode);
    
    	if(!strcmp(passcode, "123"))
    	{
    		system("cls");
    		printf("\n\n\tAuthenticated.");
    	}
    
    	if(strcmp(passcode, "123"))
    	{
    		system("cls");
    		printf("\n\n\tIncorrect!! Please try again.");
    		getch();
    	}
    
    	}while(strcmp(passcode, "123"));
    
    	getch();
    	return 0;
    }
    
    int exit_scr()
    {
    	int x;
    
    	system("cls");
    
    	x = 0;
    	while(x < 2)
    	{
    		gotoxy(9,10); printf("BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf(" BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("  BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("   BYE-BYE");
            delay(2500);
    		x++;
    		gotoxy(9,10); printf("     BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("      BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("       BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("        BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("         BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("          BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("           BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("            BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("             BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("              BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("               BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                 BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                  BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                   BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                    BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                     BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                      BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                       BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                        BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
                    gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
                    gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
                    gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
                    gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
                    gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
                    gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
                    gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
                    gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                                 ");
    		delay(2500);
    		x++;
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
    	}
    
    	sleep(2);
    	return 0;
    }
    My question is.... How can I make the program portable, at the same time, retaining the way it runs...? I mean, those gotoxy()'s there, the textcolor()'s, and getch()'s... I know that these belong to the conio.h... and people say that it's not portable if I use conio.h...

    ...please give alternatives... and if you can give some tips, like simplifying my code(especially on that "BYE-BYE" thing there).. I'd really appreciate it.. thanks in advance... -_+

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Well, portable, this isn't. I'm not sure what compiler you're using, but I think it might be old, especially if you are using "dos.h" and "conio.h". I don not believe that "conio.h" is even part of the "standard C/C++" . In order for your program to be compaticle, you need to follow the latest C99 (?) standard. You might want to Google that and see what the standard says. There are other folks on here who know more about that than I do.

    Also, you need to forward declare your functions before using them. And there is no reason to have them "return" anything. They can be declared as "void". So if you are new to C, you need to get a more recent book that follows the latest "standard" so you can program correctly.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by ShadeS_07
    I don not believe that "conio.h" is even part of the "standard C/C++" .
    Yes, but I believe that is needed for gotoxy(). I suspect that this is inherently platform dependent since as far as I can tell the standard library functions are not enough to accomplish what ShadeS_07 wants to do.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There must be a better way to do this:

    Code:
    	x = 0;
    	while(x < 2)
    	{
    		gotoxy(9,10); printf("BYE-BYE");
    		delay(2500);
    		x++;
    		/*snip*/
    		gotoxy(9,10); printf("                         BYE-BYE");
    		delay(2500);
    		x++;
    	}
    As to making it portable, you'll need to use a portable library for the OS-dependent stuff (ncurses?).

    Seeing that this program takes more than 2 minutes to say good-bye, wouldn't it be better to spare our Linux-using friends?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    44
    okay, fyi.. I'm using TurboC 2.01... But I am moving on to Code::Blocks...

    I want to make portable programs... but I don't know yet the alternatives for these... (gotoxy()'s, textcolor()'s, etc..) hmmmm... btw, thanks for the fast response... ^.^

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by ShadeS_07
    I want to make portable programs... but I don't know yet the alternatives for these... (gotoxy()'s, textcolor()'s, etc..) hmmmm... btw, thanks for the fast response... ^.^
    anon's advice was to shift the burden of making those functions portable by using a multi-platform library that does them.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    44
    okay, hmmmm... If conio.h and dos.h aren't portable, what alternatives can I use for textcolor(), gotoxy(), and getch()?

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    44
    uhmmmm, what's a "multi-platform library"?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ShadeS_07 View Post
    uhmmmm, what's a "multi-platform library"?
    A library that has functions that are normally non-portable implemented in several different flavours. pdcurses or ncurses is such a library.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by ShadeS_07 View Post
    I want to make portable programs... but I don't know yet the alternatives for these... (gotoxy()'s, textcolor()'s, etc..) hmmmm... btw, thanks for the fast response... ^.^
    One way to make your source code portable is to use only portable construct. Especially when the non-portable functions you are using aren't adding value to your application, i.e. when you can afford not using the non portable stuff.

    Of course, there's alternatives, but if you are a beginner, I would recommend sticking to the standard. Else, look for the libraries matsp mentioned.
    I hate real numbers.

  11. #11
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    There must be a better way to write that exit_ecr(), esp. since you repeat the lines
    Code:
    delay(2500);
    x++;
    At least 20 times. Mabye using another loop. As for this: printf(" BYE-BYE"); I not going to get started as I know that you could use a while to uniformily insert spaces in those messages...
    Also why so many delays... you realise that so many people are not going to wait that long.
    Last edited by P4R4N01D; 12-23-2008 at 05:41 PM. Reason: ...
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    9

    Smile

    Hi,

    A quick hack :

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<dos.h> 
    #include<conio.h>
    
    #define DELAY_TIME 1 /* note the time unit of various functions; it may be different if dos.h is used */
    
    #define MAX_PASS_LENGTH 20 /* take care of overflow */
    #define PASS_STRING "123"
    
    #define NO_hide_and_seek 16
    #define NO_steps 25
    
    #ifndef EXIT_SUCCESS
    #define EXIT_SUCCESS 0
    #endif
    // check sleep 
    #ifndef delay /* the delay function */
    #define delay sleep /* sleep may work for unix */
    #endif
    
    /* prototypes */
    int welcome_scr( void );
    int authentication_func( void );
    int exit_scr( void );
    
    int main( void )
    {
    	system("cls");
    
    	welcome_scr();
    	authentication_func();
    	exit_scr();
    
    	return EXIT_SUCCESS;
    }
    
    int welcome_scr( void )
    {
    	int x;
    	char c;
    	char spinstr[] = "|/-\\|";
    
    	system("cls");
    
    	do {
    
    		for ( x=9 ; x<= 72 ; x+= 9 )
    		{
    			gotoxy(x,10); 
    			printf( "|"  );   
    		}
    
    		for ( x=10 ; x<= 64 ; x+= 9 ) 
    		{
    			gotoxy(x,10); 
    			printf("========");
    		}
    
    		for ( x=9 ; x<= 72 ; x+= 9 )
    		{
    			gotoxy(x,12); 
    			printf( "|"  );  
    		}
    
    		for ( x=10 ; x<= 64 ; x+= 9 ) 
    		{
    			gotoxy(x,12); 
    			printf("========");
    		}
    
    		gotoxy(7,11); 
    		printf("<");
    		gotoxy(74,11); 
    		printf(">");
    
    		gotoxy(25,11); 
    		printf("Welcome to ABC v1.0 (press enter)"); /* press enter is added */
    
    	}while( scanf("%c",&c)  && c!='\n' ) ; /* needs error checking */
    
    	return EXIT_SUCCESS;
    }
    
    int authentication_func( void )
    {
    	char passcode[MAX_PASS_LENGTH+1];
    
    	do {
    
    	system("cls");
    
    	printf("\n\n\tEnter the passcode: ");
    
    	scanf( "%s", passcode);
    
    	if(!strcmp(passcode, PASS_STRING))
    	{
    		system("cls");
    		printf("\n\n\tAuthenticated.");
    	}
    
    	if(strcmp(passcode, PASS_STRING))
    	{
    		system("cls");
    		printf("\n\n\tIncorrect!! Please try again.");
    		getchar();
    	}
    
    	}while(strcmp(passcode, PASS_STRING));
    
    	getchar();
    	return EXIT_SUCCESS;
    }
    
    int exit_scr( void )
    {
    	int x;
    	char dumb[NO_steps+1] ;
    	system("cls");
    
    	for ( x=0 , dumb[0]='\0'; x<NO_steps ; dumb[x]=' ', dumb[x+1]='\0' ,x++)
    	{
    		gotoxy(9,10); 
    		printf("%s",dumb);
    		printf("BYE-BYE");
    		delay(DELAY_TIME);
    	}
    
    
    	for ( x=0; x<NO_hide_and_seek ; x++) 
    	{
    		gotoxy(9,10); 
    		printf("                                 ");
    		delay(DELAY_TIME);
    		gotoxy(9,10); 
    		printf("                         BYE-BYE");
    		delay(DELAY_TIME);
    	}
    
    	delay(2); /* this was sleep(2) */
    	return EXIT_SUCCESS;
    }
    Note:

    * I compiled this using pelles C with <dos.h> excluded and "Project options-> Compiler -> Define compatibility names" enabled.

    * I suggest you to use standard functions and multi platform libraries instead of OS/implementation specific functions or write your own ones. For example replace gotoxy, delay and so on.

    I hope it helps anyway.
    Last edited by Visitor; 12-24-2008 at 09:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making sprites
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 02-20-2010, 06:00 AM
  2. Making great graphics
    By MadCow257 in forum Game Programming
    Replies: 1
    Last Post: 02-20-2006, 10:59 PM
  3. Making control...
    By Finchie_88 in forum C++ Programming
    Replies: 2
    Last Post: 09-07-2004, 01:42 PM
  4. Replies: 2
    Last Post: 01-13-2003, 12:28 PM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21