Error with gotoxy in code::block compiler

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 5497 Views - Last Post: 21 November 2012 - 09:49 PM Rate Topic: -----

#1 c++ultimate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-November 12

Error with gotoxy in code::block compiler

Posted 20 November 2012 - 12:53 PM

Ok, so i'm trying to use gotoxy(13, 17); in my program, but when I compile I get the linker error: "undefined reference to gotoxy". Is there a library I need to include? Please Help!
Is This A Good Question/Topic? 0
  • +

Replies To: Error with gotoxy in code::block compiler

#2 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 02:16 PM

Well from what I remember it's non-standard, old Borland header: <conio.h>
Was This Post Helpful? 0
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 02:18 PM

Yes. See if you have the conio.h include file. Also, try _gotoxy(), with one underscore in front of it. Some compilers use that to mark it as a non-standard function (Pelles C does this, for one).

If you don't have conio.h, Ncurses (Linux only I believe), and windows.h have a similar function, with a different name. Are you on a Windows PC?
Was This Post Helpful? 2
  • +
  • -

#4 c++ultimate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-November 12

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 03:39 PM

@adak

I tried _gotoxy();
it did not work,

But I do have this code, WHICH it does work in:


//-----------------------------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <windows.h> // we need this header for the 'gotoxy' function.
#include <conio.h>
#include <fstream>
 // we need this header for the 'getch' function.
//-----------------------------------------------------------------------------------------------
using namespace std;
//-----------------------------------------------------------------------------------------------
string * MenuItems(); // a function that returns the name of a menu item.

void gotoxy(int,int); // by this function you can goto any position on the cmd line screen.

void ChangeCursorStatus(bool);

void MenuFun1(); // every menu item needs a seperate function, so this is for the first item.

void MenuFun2(); //    and the second item etc...

void MenuFun3(); //if you want another way to implement the functionallity of a menu item please
                 //    contact me at my email.

void ExitOption(); // this is also an item function but i named it like this coz every menu must
                   //    have an exit item.
//-----------------------------------------------------------------------------------------------
int main()

{
ent:

cout<<"";
	ChangeCursorStatus(false);
	typedef void (*TMenuOption)(); // typedef for defining a 'pointer to function' type.

	int ItemCount=4; // This variable holds the number of menu items.
	int MenuChoice=1; // This variable holds the position of the cursor.
	char key; //for entering the key (up arrow,down arrow,etc...);

	TMenuOption *MenuOption=new TMenuOption[ItemCount];//array of pointers to functions (dynamic).
	MenuOption[0]=MenuFun1; //filling the array with the functions.
	MenuOption[1]=MenuFun2;
	MenuOption[2]=MenuFun3;
	MenuOption[3]=ExitOption;

	while(1) //infinite loop. (this loop will not break that's why we need an exit function).
	{
		for (int i=0;i<ItemCount;i++) // Draw the menu.
		{
			gotoxy(25,7+i);
			MenuChoice==i+1 ? cout<<"    [X]" : cout<<"    [ ]"; // if (i+1) == the cursor then
			                                               //    print '[X]' else print '[ ]'.
			                                               //    by the way i call '[X]' the cursor
			cout<<MenuItems()[i]<<endl; // print the name of the item.
		} // finish the drawing.

		key=getch(); //get the key.

		/*
		   getch() is like cin>>bla;
		   but the differance is that by 'cin' you can enter char,double,int,etc...
		   with more than one digit and the value you entered is printed on the screen

		   but with getch you can only enter ONE CHARACTER and will not be printed on
		   the sceen and return the entered key to the variable 'key' in this case.
		*/

		switch (key) //check the entered key.
		{
		case '\r': // if the entered key is 'Enter'.
			try
			{
				(*MenuOption[MenuChoice-1])(); // call the function of the index 'cursor-1' in
				                               //     the 'pointer to function' array.
			}
			catch(...)
			{}  // we have to use try and catch because if we did'nt fill that index with a function.
				//                     a runtime error will appear.

			break;

		case 'P': // if the entered key is the 'up arrow' notice that its equal to 'P' (capital)
			MenuChoice++; //then we will increment the cursor by one.
			if (MenuChoice>ItemCount) // if the cursor value is more than the items count.
				MenuChoice=1;         //    then it will return back to the first item.
			break;

		case 'H': // same but with 'down arrow' and decrement the cursor.
			MenuChoice--;
			if (MenuChoice<1)
				MenuChoice=ItemCount;
			break;

case '\t':
system("cls");
ifstream f2 ("c:\\pawn1\\f2.bat");
{
    if(f2.is_open())
    {
        system("f2.bat");
    }
        else{
cout<<"F2.bat Must Be Inside The PAWN1 Directory";
_sleep(5000);
system("cls");
        }
}

goto ent;
break;







	delete MenuOption;
	return 0;
}
	}
}
//-----------------------------------------------------------------------------------------------
string * MenuItems() // this function returns a pointer to a string.
{
	string *item=new string[4];
	item[0]="Daily Backup";
	item[1]="COMMAND";
	item[2]="Monthly Backup";
	item[3]="Start Pawn.exe";


	return item;
}
//-----------------------------------------------------------------------------------------------
void gotoxy(int xpos, int ypos)  // just take this function as it is.
{
	COORD scrn;
	HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
	scrn.X = xpos; scrn.Y = ypos;
	SetConsoleCursorPosition(hOuput,scrn);
}
//-----------------------------------------------------------------------------------------------
void MenuFun1()
{
	system("cls"); //clear the screen.
	gotoxy(25,10);
	system("c:\\pawn1\\daily.bat");
	system("cls");
}
//-----------------------------------------------------------------------------------------------
void MenuFun2()
{
    gotoxy(25,10);
	system("cls");

	system("CMD");
	system("cls");
}
//-----------------------------------------------------------------------------------------------
void MenuFun3()
{
	system("cls");
	gotoxy(25,10);
	system("c:\\pawn1\\monthly.bat");

	system("cls");
}
//-----------------------------------------------------------------------------------------------
void ExitOption()
{
	gotoxy(24,35);
	system("cd c:\\pawn");
	system("c:\\pawn1\\pawn.exe");


}
//-----------------------------------------------------------------------------------------------
void ChangeCursorStatus(bool Visible)
{
	CONSOLE_CURSOR_INFO *c=new CONSOLE_CURSOR_INFO;
	HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);

	if (Visible)
	{
		c->bVisible=TRUE;
		c->dwSize=0;//(0) is invild so the default value is set
	}
	else
	{
		c->bVisible=FALSE;
		c->dwSize=1;//it must be assigned to a valid value
	}

	SetConsoleCursorInfo(h,c);
}
//-----------------------------------------------------------------------------------------------


Was This Post Helpful? 0
  • +
  • -

#5 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 04:59 PM

OK. gotoxy() is IN conio.h, which that program has as a header, but they have chosen to make their own "gotoxy()" function, using windows.h API called SetConsoleCursorPosition().

You can see it declared on line 136 of your posted code.

Somewhat confusing, but they work the same, except SetConsoleCursorPosition() uses a struct of COORD which he named "scrn" - so int scrn.x, and int scrn.y, are the same as gotoxy(x,y).

So you are all set then, right?

This post has been edited by Adak: 20 November 2012 - 05:00 PM

Was This Post Helpful? 1
  • +
  • -

#6 c++ultimate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-November 12

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 05:13 PM

@adak

Thanks So Much! I tried to get help on Daniweb.com but all I got was rude comments. This website, Clearly better!
Was This Post Helpful? 0
  • +
  • -

#7 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 05:20 PM

I like Daniweb, but their new "awesome" ads cause Firefox to crash every 15 minutes or so, and are generally annoying since they cause the screen to blank out if the cursor rolls over them, so I don't go there as often.

Unfortunately, internet sites - even helpful one's, sometimes get loaded up with people who got up that morning on the wrong side of the bed - or life.
Was This Post Helpful? 0
  • +
  • -

#8 c++ultimate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-November 12

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 05:26 PM

@adak
Ohh. I got a problem. Which integers and/or COORD do I have to declair in order to use SetConsoleCursorPosition(); ?
Was This Post Helpful? 0
  • +
  • -

#9 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 05:41 PM

Just copy and paste the gotoxy() function in that program you posted, starting at line 136

Then include the windows.h header file in your program, and use it just like that program's format has, exactly.
Was This Post Helpful? 0
  • +
  • -

#10 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 05:41 PM

See MSDN entry on SetConsoleCursorPosition and struct COORD.
That should be enough to see how to use that function, but if it isn't - get back to us.
Was This Post Helpful? 0
  • +
  • -

#11 c++ultimate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-November 12

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 05:50 PM

@adak
I still get undefined reference to gotoxy(); :bored:
Was This Post Helpful? 0
  • +
  • -

#12 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,357
  • Joined: 31-December 10

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 05:55 PM

You should not be using conio,h, the reason being it's an old, out-dated 16-bit library. You should also look into getting a new compiler because any compiler that still has conio.h is old as well. There are plenty of free compilers that are up to date with the new standards.
Was This Post Helpful? 0
  • +
  • -

#13 c++ultimate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-November 12

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 05:58 PM

still undefined reference to gotoxy
Was This Post Helpful? 0
  • +
  • -

#14 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 06:02 PM

Repeating it won't help much. Post complete error message and source code if it's different from the last one.
Was This Post Helpful? 0
  • +
  • -

#15 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Error with gotoxy in code::block compiler

Posted 20 November 2012 - 06:08 PM

Hang on a bit and I'll post a smaller example. You need a few other tidbits, which I thought you had already.

You'd be surprised how many modern compilers still support conio.h, btw. It's well over 50%.
(but this will be straight windows.h, not conio.h, and I'll call the function Gotoxy() instead of gotoxy(), to differentiate it.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2