Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 10th, 2005, 2:17 PM   #1
hbweb500
Newbie
 
Join Date: Jul 2005
Posts: 4
Rep Power: 0 hbweb500 is on a distinguished road
Return Strings in Function

In the Win32 program I am writing, I refer to different building names by integers so I can store information in arrays. Whenever I need to output this information, however, I need to associate a string with the integer so as the user will actually know what building I am talking about. I made a function to do this.

The first problem is using WSPRINTF().

LPTSTR string;
wsprintf(temp, "%hS","Research Lab");

The above code will compile but not run, as I get a "Cannot Write to Memory" error when I reach this section of the program.

To counter this, I follow MSDN's example. There they use a TCHAR. The problem is, I cant seem to find a way to get the function to return a TCHAR to the program, and get it to work.

Here is the function:
VOID NameBuilding(int bldng, TCHAR *bldngname)
{
     TCHAR temp[80];
     switch(bldng)
     {
                  case 0:
                  wsprintf(temp, "%S","Metal Mine");
                  break;
                  
                  case 1:
                  wsprintf(temp, "%S","Crystal Mine");
                  break;
                  
                  case 2:
                  wsprintf(temp, "%S","Deuterium Mine");
                  break;
                  
                  case 3:
                  wsprintf(temp, "%S","Solar Plant");
                  break;
                  
                  case 4:
                  wsprintf(temp, "%S","Robotic Plant");
                  break;
                  
                  case 5:
                  wsprintf(temp, "%S","Shipyard");
                  break;
                  
                  case 6:
                  wsprintf(temp, "%S","Metal Storage");
                  break;
                  
                  case 7:
                  wsprintf(temp, "%S","Crystal Storage");
                  break;
                  
                  case 8:
                  wsprintf(temp, "%S","Deuterium Tank");
                  break;
                  
                  case 9:
                  wsprintf(temp, "%S","Research Lab");
                  break;
                  
                  case 10:
                  wsprintf(temp, "%S","Rocket Silo");
                  break;
                  }

// Now here I have tried using the pointer I called for, bldngname, to write directly to the var, but no luck. I have also tried making the temp var a LPSTR and returning it, but it wont work with the WSPRINTF then.

}

And here is what I need to do witht that output:

TCHAR string[80];

wsprintf(string,"%hS",temp);
                   
//where temp is the returned string

I am thinking that the returned data must be a LPSTR or the like, but I just cant seem to figure this out.

Thanks,
Jeffrey
hbweb500 is offline   Reply With Quote
Old Jul 10th, 2005, 2:29 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I would suggest that you take a different tack. Either use a map (key => value, which is the building number => building name), or simply an array whose elements contain the building names. Element 0 would represent building 0, "Sears Hardware", and so forth.

One can't see all your code, but you have a fundamental problem when you say you can't properly return the string. I don't give a crap if the Pope anointed it, you can't return it from a function defined as returning a void. Your description is somewhat ambiguous regarding what you are actually trying to do, somewhat as if you're frustrated and have turned to trying Voodoo without first killing a good chicken. Consequently, perhaps I have misunderstood, somewhat.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 10th, 2005, 2:53 PM   #3
hbweb500
Newbie
 
Join Date: Jul 2005
Posts: 4
Rep Power: 0 hbweb500 is on a distinguished road
Quote:
I don't give a crap if the Pope anointed it, you can't return it from a function defined as returning a void.
I intially had the function classified as LPSTR and had the temp variable initialized as a LPSTR, but it didnt work with wsprintf so I removed it.

An overview: I am trying to make a function that will edit and return a TCHAR. I cant get this to work.

Alternatively: Why wont a LPTSTR work in wsprintf? As in this:

LPTSTR string;
wsprintf(string, "%hS","Research Lab");

That doesnt work for some reason.

Jeff
hbweb500 is offline   Reply With Quote
Old Jul 10th, 2005, 3:25 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I honestly don't mess with C# much, but I'll play with what you show here a little bit just for grins....
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 10th, 2005, 5:37 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I really don't know what you're doing here, as those things aren't a part of typical C# code. I realize that if one has to interface with old code, dll's, and things like that, one might have to resort to interfacing, unmanaged code, and unsafe objects. Maybe that's what you're doing. At any rate, it's too much work to mess with for an example.

Even if you're in the wrong forum, and meant for it to be C++ code, you have some problems. When you're dealing with wide characters, you can't just go dropping literals here and there. I've include here some source code. You can see that your approach to a functional cross reference will work (you can't return a pointer to a local variable and get away with it, of course). It's just far less effective than other ways to accomplish the same thing. I've shown the method of using an array with the building number as an index into the array. For that part, I didn't bother to make the strings wide characters. You can see from the example how to do that.
#include <tchar.h>
#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

void NameBuilding (int bNum, TCHAR *bName);

int _tmain (int argc, _TCHAR* argv[])
{
    TCHAR temp [80];
    char bldgText [][80] = {"Metal Mine", "Crystal Mine", "Deuterium Mine", "Solar Plant",
                     "Robotic Plant", "Shipyard", "Metal Storage", "Crystal Storage",
                     "Deuterium Tank", "Research Lab", "Rocket Silo"};
    NameBuilding (0, temp);
    wprintf (_T("%s\n"), temp);
    // Now do it the easy way
    cout << bldgText [5] << endl;
	return 0;
}
void NameBuilding (int bNum, TCHAR *bName)
{
    switch (bNum)
    {
        case 0:
        wsprintf (bName, _T("%S"), "Metal Mine");
        break;
    }
}
Again, if you're really going to use C#, and if you're NOT having to mate it with non-C# stuff, bite the bullet and learn it straight up, don't try to port other knowledge you have to it that isn't really applicable.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 10th, 2005, 6:23 PM   #6
hbweb500
Newbie
 
Join Date: Jul 2005
Posts: 4
Rep Power: 0 hbweb500 is on a distinguished road
Ok, thanks.

I am programming in Win32 C++ using Dev-C++, not any Microsoft C# tools. In this way I find it difficult to get support, as MSDN is mostly geared towards C# and .NET, and I need to make up most of my own knowledge.

If I may ask, what is the function of the cout in the first procedure? I have nly seen that used in C++ when it writes directly to the output buffer, not in Win32. What buffer is it writing to?

Thanks,
Jeff
hbweb500 is offline   Reply With Quote
Old Jul 10th, 2005, 7:12 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Cout is a C++ stream buffer. It's very similar to the C streams (like stdin, stdout) and can, actually, be tied to them, but it's not a really robust thing to do. Both those types of streams are language features; they only associate with a particular platform like Windows or Unix via dependent code provided by the compiler for a particular platform. Printf, for instance, is not a Win32 thing either. Those things, in Windows, get to the hardware through the Win32 API, the console portion of it. In many instances the C++ I/O thangys are classes which wrap the C-style I/O. The approach contributes to their robustness at the cost of additional code footprint. In some cases, though, the additional capabilities are nice, indeed, and can actually result in faster operation if investigated thoroughly and used properly. I recommend you move to C++ despite any costs in actual performance. Investigate the STL for vectors, deques, and maps. You'll have a much easier time doing things like you're trying to do. Your production will be dramatically greater than for C, alone. Of course, when one needs cutting edge, one drops to whatever level is necessary to provide it, but that is not a normal goal for most applications. The goal is to get there rapidly with code that works definitively and correctly.

You should post in the C++ forum, since that's what you're writing. The C# thing confused the hell out of me; I didn't know why in the world you were trying to use those items in a C# environment. There is still a LOT of C++ native code information available on MSDN, it's just harder to find since they've gone so NET-centric. As far as I can tell, all the old stuff for VC++ 6.0 is still there, although I have it all on CDs from ages ago. You might want to download VC++ 2005 beta, which is free, just to get the documentation. I actually like it as an IDE, although I have to work to avoid the NET/managed code it wants to push on me and go for native code. I sometimes use it in combination with VC 6.0, which I use to do the visual layout of dialog boxes and such. I have Borland Builder 6, but it's not free (except for a limited trial). I have Dev-C++ too, and it's very good for a freebie, but the debug capabilities aren't quite as good as MS's.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 10th, 2005, 8:11 PM   #8
hbweb500
Newbie
 
Join Date: Jul 2005
Posts: 4
Rep Power: 0 hbweb500 is on a distinguished road
I see. My bad, I thought I did post in the C++ forum. Ive been up for a while, I should get some sleep.

Im still not completely understanding the function of the COUT. Note that this is not a console app, im actually going to be outputting the data to an edit box.\

Now I tried this, and I get "?????????????????????????????????????
" output into my edit box.

This is my code:
TCHAR bldngname[80];
                   NameBuilding(1,bldngname);
                   wsprintf (tempc,"%S\n", bldngname);

SetDlgItemText(hwnd, IDC_OUTPUT, tempc);

Notcie I changed the first value of NameBuilding to 1 to make sure I wasnt passing a bad value. It should call for a Crystal Mine.

tempc is a TCHAR array.

NameBuilding() ties to this:

VOID NameBuilding(int bldng, TCHAR *bldngname)
{

     switch(bldng)
     {
                  case 0:
                  wsprintf(bldngname, "%S","Metal Mine");
                  break;
                  
                  case 1:
                  wsprintf(bldngname, "%S","Crystal Mine");
                  break;
                  
                  case 2:
                  wsprintf(bldngname, "%S","Deuterium Mine");
                  break;
                  
                  case 3:
                  wsprintf(bldngname, "%S","Solar Plant");
                  break;
                  
                  case 4:
                  wsprintf(bldngname, "%S","Robotic Plant");
                  break;
                  
                  case 5:
                  wsprintf(bldngname, "%S","Shipyard");
                  break;
                  
                  case 6:
                  wsprintf(bldngname, "%S","Metal Storage");
                  break;
                  
                  case 7:
                  wsprintf(bldngname, "%S","Crystal Storage");
                  break;
                  
                  case 8:
                  wsprintf(bldngname, "%S","Deuterium Tank");
                  break;
                  
                  case 9:
                  wsprintf(bldngname, "%S","Research Lab");
                  break;
                  
                  case 10:
                  wsprintf(bldngname, "%S","Rocket Silo");
                  break;
                  }

}

P.S. I know my code is shoddy, but Im not a professional. I only code maybe once a year for very specialized projects, and sometime I will have to study further. If it looks like I have no clue what Im doing, its true. Hey, at least I dont leetspeak.

Last edited by hbweb500; Jul 10th, 2005 at 8:35 PM.
hbweb500 is offline   Reply With Quote
Old Jul 10th, 2005, 9:44 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
No, you can't use "cout" for a text box, it's a console thangy, like "printf".

You need to look at the code I posted again or the response is wasted. Who wants to mess with that? You can't use that format string in wsprintf, you need to do it like I did. If that doesn't straighten it out, repost your newest code.

One presumes your text box is looking for wide characters? If not, why are you using what you're using?

If you were posting leet speak, you'd be looking to another for help !
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC