C++/gettext equivalents
Expert: Eddie - 9/21/2009
QuestionQUESTION: Hi, I'm a c++ beginner using Dev-C++.
I'm trying to read text (single character) from a specific x,y point on the console, and searching the web, the only thing I can find to do it is gettext in conio.
I've included conio, but many of it's functions seem unavailable. I've found a bit of code that's an alternative to gotoxy, but can't find anything that will do the equivalent of gettext. Can you recommend anything?
Thanks,
John
ANSWER: Hi John, thank you for the question.
I'm not familiar with the gettext method myself, but after doing some searching on the internet, I found a link that appears to do what you are looking to do.
http://www.dreamincode.net/forums/showtopic17720.htm
It uses the gettext and gotoxy functions in the example, and translates to a string value. I think this could be a good read for you.
I hope this information was helpful.
- Eddie
---------- FOLLOW-UP ----------
QUESTION: Hi Eddie, sorry to keep bothering you...
I installed conio2, and it's working fine. Except...
The gettext function seems to work differently to the normal conio version, in terms of where it actually stores the info.
I've run the following code:
int main(int argc, char *argv[])
{
struct char_info buffer[2];
gotoxy (1,1);
cout << "*" <<endl;
gettext(1,1,1,1, buffer);
cout << buffer;
puttext (1,3,1,3, buffer);
gotoxy (1,4);
system("PAUSE");
return EXIT_SUCCESS;
}
and got this output:
*
0x22ff70
*
Press any key to continue . . .
which my very limited understanding leads me to think buffer is some sort of pointer, so printing it just gives me a memory address. However, I don't know how to access what's in there (apart from using puttext). If I try cout << *buffer; I get the following compiler error: "no match for 'operator<<' in 'std::cout << *(char_info*)(&buffer)' "
I think I'm out of my depth! All I want to do is say gettext (1,1,1,1,variable) and then do an if (variable='x').
The documentation explains char_info thusly:
Data Fields
char letter
character value
unsigned short attr
attribute value
Do you have any advice?
Thanks again for your time,
John
AnswerHi John, thank you for the question.
You're right that you are printing out a memory address. You created an array of 2 char_info's. The name of an array variable also serves as a pointer to the first element in the array, which is what you're seeing printed out.
Here's what it looks like you should do. Assuming you've already printed out to where you want to read in from.
char_info variable;
gettext(1,1,1,1, &variable); // Pass in the address of the variable so it can be modified
cout << "The letter is: " << variable.letter << endl;
The structure char_info has a field called letter, which has a char variable letter, so my guess is that is where it stores the letter of what it just read in. I'd assume if you wanted to read a line of text, you could do it in a for loop:
// Assume you've already placed your characters in the correct position
for(int i = 1; i < 6; i++) // read 5 characters
{
char_info variable;
gettext(1,1,i,1, &variable); // Pass in the address of the variable so it can be modified
cout << "The letter is: " << variable.letter << endl;
}
After looking at the definition, you might just be able to put a 5 as the third parameter, but I don't see how that would work, since the structure only has a single char letter variable. If you could pass in a number, it would have to store that many char's as opposed to a single one.
Let me know if this helps.
- Eddie