code problem

Started by
4 comments, last by kordova 21 years, 2 months ago
I downloaded this code
  
/*
 * Compiled under DJGPP
 */

#include <stdio.h>
#include <conio.h>
#include <time.h>

#define BLOCK 0xdb      //Solid block character
#define LEFT 10         //Left-most column for scanner_a
#define RIGHT 70        //Right-most column for scanner_a
#define LINER 10        //Line for scanner_a
#define TRUE 1
#define FALSE 0

/* Global variables are a good idea for programs that
   Fake multitasking. This way the variables not only are static
   but are also available to other routines  */

int block_x;            //Saves the block position for scanner_a()
int block_y;
int xpos;               //Saves cursor position for some_text()
int ypos;
int counter;            //Used in score()

/*
 * This routine displays the time in the upper left corner of the
 * screen. It's a standard, Unix-like time display routine
 */

void show_time(void)
{
    struct tm *rightnow;                //variables defined in TIME.H
    time_t rawtime;

    time(&rawtime);
    rightnow = localtime(&rawtime);
    gotoxy(1,1);
    cprintf("%s",asctime(rightnow));    //asctime() displays the time string
}

/*
 * This routine displays a block character that bounces back and forth
 * across the text screen. I called it scanner_a() because at one time
 * I also wrote a scanner_b() function. But they run too fast for anyone
 * to notice any difference.
 */

void scanner_a(void)
{
    static int direction;               //static variables are preserved
                                        //so this value is remembered
    gotoxy(block_x,block_y);            //set the cursor!
    cprintf(" ");                       //Erase the old block

    if(block_x==LEFT) direction=1;      //Check bounds and change direction
    if(block_x==RIGHT) direction=-1;    //if the block is at the end

    block_x = block_x + direction;      //move the block's location
    gotoxy(block_x,block_y);            //set the cursor
    cprintf("%c",BLOCK);                //Display the block
}

/*
 * Here is the function that processes text. First kbhit() is checked
 * to see if any keys have been pressed. If not, the function doesn't
 * run and the program doesn't sit and wait for a key to be pressed.
 */

int some_text()
{
    char c;

    if(kbhit())                 //TRUE if a key has been pressed
    {
        gotoxy(xpos,ypos);      //update cursor
        c=getch();              //read character
        cprintf("%c",c);        //dipslay character via console routine
        xpos = wherex();        //Grab cursor's new position and save it
        ypos = wherey();
        if(c == 0x0d)           //ENTER pressed?
            return(1);          //Return if so otherwise, continue
        else
            return(0);
    }
    else
        return(0);              //A normal return if no key was waiting
}

/*
 * Finally, something else to add: This is just a counter displayed
 * In the upper right corner of the screen. Consider it your "score"!!
 */

void score(void)
{
    counter++;                  //Counter is a global variable, an int.
                                //It's value will never be more than 32K
    gotoxy(70,1);               //Always display it at the same spot
    cprintf("%i",counter);      //Console output
}

/*
 * Ah! The main loop. Since the routines are listed above there's no
 * need for prototyping here. Also, notice the "int main()" format, which
 * is considered the standard for DJGPP.
 */

int main()
{
    int done;

    /* Initialize variables & such */

    clrscr();                   //Clear the screen

    block_x = LEFT;             //Preset the block's position
    block_y = LINER;

    gotoxy(1,15);               //Text prompt
    cprintf("Type some text:");
    xpos = wherex();            //Saving cursor location for text input
    ypos = wherey();

    counter = 0;                //Initialize the "score"

    /* Here is the loop that runs the show */

    done = FALSE;               //Prep for the almost-endless loop


    while(!done)                //The event loop

    {
        if(some_text())         //Check for text input

            done = TRUE;
        scanner_a();            //Move the block

        show_time();            //Display the time

        score();                //Display the score

    }

    gotoxy(1,23);               //Put cursor at the bottom of the screen

                                //for when the program ends

}
  
from http://www.c-for-dummies.com/lessons/bonus/11/ and the first error it gives me is 'gotoxy' undeclared identifier and proceeds to list others that I was rather sure were included in conio.h. I am using VisStud.net and created a new console app if that matters at all. Thanks ahead. [edited by - kordova on February 7, 2003 4:19:41 PM]
Advertisement
This code appears to be real old. It uses the DJGPP compiler (not visual studio .net) and is DOS based. Bottom line this code will not work in visual studio .net so don''t waste your time trying to get it to work. Find another tutorial that is more recent and uses visual C++.
Conio.h isn''t a standard header so it differs from implementation to implementation, if it''s present at all. You''ll have to use different functions depending on the platform you''re developing for - for example in Windows you would use WriteConsoleOutputCharacter which accepts console coordinates for text output.

The tags are source and /source.
Is there a more preferable header or such with which I might accomplish the same thing? Or just research more recent (and applicable to my system/ide) versions of conio.h?

edit: ?

[edited by - kordova on February 8, 2003 11:03:52 PM]
quote:Original post by kordova
Is there a more preferable header or such with which I might accomplish the same thing? Or just research more recent (and applicable to my system/ide) versions of conio.h?


Depends entirely on what you are trying to do and what you are trying to do with it. As was stated previously, the code snippet is old and was aimed at console (in this case, read "DOS"/"Un*x") based input and output. GUI''s use an entirely different approach, and are about as platform dependant as you can get.

In the old days, back in the time of the 8086 and DOS, 1M memory limits, etc, DOS based compilers (Turbo-C/MS-C) made use of conio.h to manage 80x24 text based screens. There were concepts such as cursors, character attributes, etc, to deal with. The function gotoxy() simply moved the cursor (the location the next printf() would display text) to the right location to help with screen formatting.

These days, GUI''s use status bars, toolbars, controls, graphics drawing and text rendering routines to do that. Un*x programs often make use of screen management libraries, such as Curses, to manage the logic/virtual screen, paging, etc.
I understand that much, but I''ve never really played around with gui''s etc outside of java and was told console was a good place to start, as well as a well written program''s gui handling should be such that it wouldnt be impossible to add a real windowed gui when I was comfortable with that aspect of it. Thanks again.

This topic is closed to new replies.

Advertisement