Page 1 of 2

texts

Posted: October 13th, 2017, 6:46 am
by Empirean
Hello, how do i display texts? Like during runtime i wanna display debug messages on screen?

also whats the difference between

Code: Select all

 this; 
to

Code: Select all

 *this 
? Arent they just the same since this returns the class? Thanks.

Re: texts

Posted: October 13th, 2017, 7:11 am
by albinopapa
First, displaying text.
It depends...if you are using the chili framework and want to display text on screen like an overlay you can follow the Intermediate video on rendering text.
As for visual studio, you can send debug messages to the output window, though I don't remember the function name.

Second, this is a pointer pointing to the class it is apart of. *this dereferences the pointer. A pointer is just a variable to stores an address of some data. When you dereference the pointer, you are dealing with the data directly.

Syntax:
this->myData; the -> operator dereferences the pointer to access member data
(*this).myData; the * operator dereferences the pointer mostly used to pass a reference to an object, but using this syntax performs the same as the -> operator, dereference then access the data member.

Copying:
MyClass *pObject = this; this just copies the address stored at 'this'
MyClass &rObject = *this; this does the same except references can't be reassigned, so the 'rObject' can't point to anything else
MyClass vObject = *this; this actually makes a copy of the object that the this pointer points to.

EDIT: OutputDebugString( "This is a debut message sent to the output window" );
Found the function, here is the documentation if you want to look it over

Re: texts

Posted: October 15th, 2017, 12:44 pm
by Empirean
Hello albinopapa, thank you so much for the help. I don't understand pointers yet, I have got to that part of the tutorials.

Offtopic: My visual studio cannot run the framework anymore. I don't know what exactly happened but it's not running the chiliframework properly.

Re: texts

Posted: October 15th, 2017, 1:49 pm
by albinopapa
In what way will it not run properly?

Re: texts

Posted: October 15th, 2017, 1:59 pm
by Empirean
It runs normally in release but doesn't run on debug.

Edit: Nevermind bro, clean and build fixed the issue.

Re: texts

Posted: October 18th, 2017, 11:06 pm
by Empirean
Hello, I have further questions regarding pointers. It seem easy to understand and I know I used it at some point in school (but that was turboC). I cant grasp the idea of getting strings via user input. I tried making my own functions and it can only get the first character right and the rest are some special characters. I can post my code later when i get home. Also isnt there a String class in C++ that can maybe make things easier to handle? Thanks in advance for the response.

Offtopic: should have named this thread as "Pointer Help or something"

Re: texts

Posted: October 19th, 2017, 7:36 am
by albinopapa
How are you getting user input?

Yes, there is std::string and std::wstring if you #include <string>.

Code would help determine the issue.

Re: texts

Posted: October 19th, 2017, 11:54 am
by Empirean
Hello albinopapa, heres my code.

Code: Select all

void print(char* s, int size)
{
	for (char* p = s; p < s + size; p++)
	{
		_putch(*p);
	}
}

void scan(char* buf)
{
	for (char c = _getch(); _getch() != 13; buf++)
	{
		_putch(c);
	}
}

int main()
{
	
	print("Enter a number :", 18);
	char m[1000];
	char* msg = m;
	scan(msg);
	
	print("\nYou typed :", 15);
	print(msg, 100);


	while (!_kbhit())
	{
		;
	}

    return 0;
}
I understand that this part works because its a loop that will terminate when enter is pressed. What I dont get is how it saves the values that the user is typing. Well, i cant get it to work right just like in the video tutorial. I hope you can help me understand what is going on.

Code: Select all

void scan(char* buf)
{
	for (char c = _getch(); _getch() != 13; buf++)
	{
		_putch(c);
	}
}
I named it "scan" cause in turboC the function is called scanf.

I also have this other problem.
The code

Code: Select all

while (!_kbhit())
does not prevent the console from closing. Hence, im using the do_nothing inside the loop.

Thank you in advance for the help.

Re: texts

Posted: October 19th, 2017, 5:20 pm
by albinopapa

Code: Select all

void scan( char* buf )
{
	for( char c = _getch(); _getch() != 13; buf++ )
	{
		_putch( c );
	}
}
Take a look at this code from scan, where are you writing the input to the buffer?

Re: texts

Posted: October 19th, 2017, 5:30 pm
by albinopapa
Empirean wrote:I named it "scan" cause in turboC the function is called scanf.
Yeah, since C++ is compatible with C, functions like printf/fprintf/scanf/fscanf made their way into the C++ library also. With C++ you can use other utility functions and classes like std::string, std::stringstream, std::iostream and std::fstream. There are two global iostream objects for getting and setting user input to the console; std::cin and std::cout. Chili covers these a bit later in the tuts along with stringstream and fstream for strings and file access.