Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
05-15-2008, 02:49 AM
#1
LQ Newbie
Registered: May 2008
Posts: 5
Rep:
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
05-18-2008, 01:20 PM
#2
Guru
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131
Rep:
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).
05-20-2008, 02:25 AM
#3
LQ Newbie
Registered: May 2008
Posts: 5
Original Poster
Rep:
ummm... im kinda a
so how can i include it in? can you please show me a example code?
05-20-2008, 10:47 AM
#4
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 1,421
05-22-2008, 04:44 AM
#5
LQ Newbie
Registered: May 2008
Posts: 5
Original Poster
Rep:
ummm....
they look complicated...
lets just assume it was for homework...heh
05-22-2008, 12:21 PM
#6
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 1,421
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.
05-23-2008, 06:38 PM
#7
LQ Newbie
Registered: May 2008
Posts: 5
Original Poster
Rep:
Thank you very much!
What you said works!
Thank you!
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 03:36 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News