LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I cant draw a ASCII table in c++ shell console (https://www.linuxquestions.org/questions/programming-9/i-cant-draw-a-ascii-table-in-c-shell-console-642317/)

DonnyYangR 05-15-2008 02:49 AM

I cant draw a ASCII table in c++ shell console
 
i cant seem to display my ASCII tables using a function which takes a RECT class as a argument using these codes:

the function is "void tables::sdraw(RECT tmp){/*do stuff*/}"

function.cpp:
Code:

#include "header.h"

// regular functions
int randint()
{
        return ((int)(rand()*rand()*time(NULL)));
}
BOOL gotoxy(int x,int y) // somebody plz tell me how this function works!
{
        HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );

    if ( INVALID_HANDLE_VALUE != hConsole )
        {
        COORD pos = {x,y};
        return SetConsoleCursorPosition(hConsole,pos);
        }
}
// end regular functions

// class coin functions
coin::coin()
{
        topface = HEAD;
}
face coin::getTopFace(void)
{
        return topface;
}

void coin::tossCoin(void)
{
        int tmp;
        tmp = 1+(randint()%2);
        if (tmp==1)
                topface = HEAD;
        else
                topface = TAIL;
}
// end class coin functions

// class player functions
player::player()
{
        wins = 0;
        points = 100;
        bet = 0;
        side = HEAD;
        loss = 0;
}
// end class player functions

// class tables functions
void tables::draw(player *tmp,int stage)
{
        if(stage==1)
        {
                gotoxy(0,0);
                cout << "+------------------------------------------------------------------------------+";
                gotoxy(0,1);
                cout << "|                Press ENTER to play or press ESCAPE to quit!                |";
                gotoxy(0,2);
                cout << "|            WARNING: Your points and your status will not be saved            |";
                gotoxy(0,3);
                cout << "+------------------+-----------------------------------------------------------+";
                gotoxy(0,4);
                cout << "| Status:          | Results:                                                  |";
                gotoxy(0,5);
                cout << "|                  |                                                          |";
                gotoxy(0,6);
                cout << "|                  |                                                          |";
                gotoxy(0,7);
                cout << "|                  |                                                          |";
                gotoxy(0,8);
                cout << "|                  |                                                          |";
                gotoxy(0,9);
                cout << "|                  |                                                          |";
                gotoxy(0,10);
                cout << "|                  |                                                          |";
                gotoxy(0,11);
                cout << "|                  |                                                          |";
                gotoxy(0,12);
                cout << "|                  |                                                          |";
                gotoxy(0,13);
                cout << "|                  |                                                          |";
                gotoxy(0,14);
                cout << "|                  |                                                          |";
                gotoxy(0,15);
                cout << "|                  |                                                          |";
                gotoxy(0,16);
                cout << "|                  |                                                          |";
                gotoxy(0,17);
                cout << "|                  |                                                          |";
                gotoxy(0,18);
                cout << "+------------------+                                                          |";
                gotoxy(0,19);
                cout << "| Credits:        +-----------------------------------------------------------+";
                gotoxy(0,20);
                cout << "|                  |            You currently have: "; gotoxy(62,20); cout << "Points          |";
                gotoxy(0,21);
                cout << "| Made By:        +-----------------------------------------------------------+";
                gotoxy(0,22);
                cout << "|  Donny Corp.    | Current Player: 1                        Total Players: 1 |";
                gotoxy(0,23);
                cout << "|                  |                                                  +--------+";
                gotoxy(0,24);
                cout << "+------------------+--------------------------------------------------+ DUMP:  ";
                showpoint(tmp->getPoints());
        }

        gotoxy(78,24);
}


inline void tables::showpoint(int tmp)
{
        gotoxy(52,20);
        printf("%9d",tmp);
}
void tables::showerr(string tmp)
{
        RECT tmp2;

        tmp2.bottom = 15;
        tmp2.top = 11;
        tmp2.left = 8;
        tmp2.right = 68;

        sdraw(tmp2);

        gotoxy(35,12);
        cout << "ERROR:";
        gotoxy(9,13);
        cout << tmp;
}
void tables::sdraw(RECT tmp)
{
        int x,y;
        for(x=0;!x>(tmp.right-tmp.left);x++)
        {
                if(x==0||x==(tmp.right-tmp.left))
                {
                        gotoxy(tmp.left+x,tmp.top);
                        cout << "+";
                }
                else
                {
                        gotoxy(tmp.left+x,tmp.top);
                        cout << "-";
                }
        }
        for(y=1;!y==(tmp.bottom-tmp.top);y++)
        {
                for(x=0;!x>(tmp.right-tmp.left);x++)
                {
                        if(x==0||x==(tmp.right-tmp.left))
                        {
                                gotoxy(tmp.left+x,tmp.top+y);
                                cout << "|";
                        }
                        else
                        {
                                gotoxy(tmp.left+x,tmp.top+y);
                                cout << " ";
                        }
                }
        }
        for(x=0;!x>(tmp.right-tmp.left);x++)
        {
                if(x==0||x==(tmp.right-tmp.left))
                {
                        gotoxy(tmp.left+x,tmp.bottom);
                        cout << "+";
                }
                else
                {
                        gotoxy(tmp.left+x,tmp.bottom);
                        cout << "-";
                }
        }
}
// end class tables functions

header.h:
Code:

#ifndef HEADER_H
#define HEADER_H

#include <windows.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <iostream>
using namespace std;

#define FALSE 0
#define TRUE !FALSE

#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define ESC 27
#define ENTER 13

typedef unsigned short int tinyint;

enum face {HEAD,TAIL};

int randint(void);
BOOL gotoxy(int x,int y);

class coin
{
        public:
                coin();
                face getTopFace(void);
                void tossCoin(void);
        private:
                face topface;
};

class player
{
        public:
                player();
                int getWins(void)
                {
                        return wins;
                }
                int getPoints(void)
                {
                        return points;
                }
                int getBet(void)
                {
                        return bet;
                }
                face getSide(void)
                {
                        return side;
                }
                int getLoss(void)
                {
                        return loss;
                }
                void setWins(int tmp)
                {
                        wins = tmp;
                }
                void setPoints(int tmp)
                {
                        points = tmp;
                }
                void setBet(int tmp)
                {
                        bet = tmp;
                }
                void setSide(face tmp)
                {
                        side = tmp;
                }
                void setLoss(int tmp)
                {
                        loss = tmp;
                }
        private:
                tinyint wins;
                unsigned int points;
                unsigned int bet;
                face side;
                tinyint loss;
};

class tables
{
        public:
                static void draw(player *tmp,int stage);
                static void showerr(string tmp);
                static void sdraw(RECT tmp);
                //static void showcre(int tmp);
                static void showpoint(int tmp);
};

#endif

test.cpp:
Code:

#include "header.h"

int main()
{
        player *player1;
        player1 = new player;

        tables::draw(player1,1);
        _getche();
        tables::showerr("TEST");

        delete player1;

        return 0;
}

im using VC++ 2008 express

b0uncer 05-18-2008 01:20 PM

Umm unless this is a homework, you could get it done easier if you used the ncurses library, for example. It's got some handy functions for drawing windows, panels and overall printing things to specific positions on the screen. But if this is a homework, then you probably can't go there.. (and should anyway mention if this is a homework).

DonnyYangR 05-20-2008 02:25 AM

:confused:ummm... im kinda a :newbie:

so how can i include it in? can you please show me a example code?

ntubski 05-20-2008 10:47 AM

First 2 links from googling ncurses tutorial:
http://web.cs.mun.ca/~rod/ncurses/ncurses.html
http://www.captain.at/howto-curses-example.php

DonnyYangR 05-22-2008 04:44 AM

ummm....

they look complicated...

lets just assume it was for homework...heh:D

ntubski 05-22-2008 12:21 PM

Sorry, I didn't really look closely at your code. I see you're working with the Windows console functions. I don't have access to a Windows dev environment at the moment, but I think you just have a precedence problem:
Code:

  for(x=0;!(x>(tmp.right-tmp.left));x++)
"!" has higher precedence than ">" so your loops weren't running at all. It might be better to rewrite the condition as
Code:

for (x=0; x <= (tmp.right-tmp.left); x++)
By the way, you really should try to give your variables more meaningful names than "tmp". And spaces between operators can also look a lot nicer.

DonnyYangR 05-23-2008 06:38 PM

Thank you very much!
What you said works!

Thank you!:)


All times are GMT -5. The time now is 06:27 AM.