LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 05-15-2008, 02:49 AM   #1
DonnyYangR
LQ Newbie
 
Registered: May 2008
Posts: 5

Rep: Reputation: 1
Question 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
 
Old 05-18-2008, 01:20 PM   #2
b0uncer
Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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).
 
Old 05-20-2008, 02:25 AM   #3
DonnyYangR
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 1
ummm... im kinda a

so how can i include it in? can you please show me a example code?
 
Old 05-20-2008, 10:47 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,489

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
First 2 links from googling ncurses tutorial:
http://web.cs.mun.ca/~rod/ncurses/ncurses.html
http://www.captain.at/howto-curses-example.php
 
Old 05-22-2008, 04:44 AM   #5
DonnyYangR
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 1
ummm....

they look complicated...

lets just assume it was for homework...heh
 
Old 05-22-2008, 12:21 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,489

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
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.
 
Old 05-23-2008, 06:38 PM   #7
DonnyYangR
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 1
Thank you very much!
What you said works!

Thank you!
 
  


Reply

Tags
c++, console, drawing, tables


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
console tty problem: huge fonts shown like ascii art TheHighwayman Linux - Software 11 11-14-2007 02:40 AM
Editing Ascii File Using Shell Script rohit2983 Programming 7 01-03-2007 06:27 AM
Extended ASCII on Busybox ASH shell kassle Linux - Software 0 12-19-2006 05:06 AM
function in shell to convert hexadecimal into ascii suchi_s Linux - Software 1 04-01-2005 02:07 PM
using extended ASCII at the console <keyboard> buhbuh Linux - Newbie 0 06-29-2004 06:09 PM


All times are GMT -5. The time now is 03:45 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration