Jump to content
  • Advertisement
Sign in to follow this  
Dreamshadow

C++ casting issue

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I need some help. I've been out of programming for about 10 years, and I'm used to C that was three years old then. I need some help with some code. I'm using Dev C++, and I'm either using MINIGCW or G++ as the compiler (not sure). I'm trying to write a program to generate NPCs for a pencil and paper boardgame. Here's the code: char[9] = crewmember[].name int counter int generator_crewmember for (counter=1; counter <= generator_crewnumber; counter++) { strcpy(crewmember[counter].name, "Crew "); strcat(crewmember[counter].name, counter); } This is the main loop of my app. I'm trying to create the NPC's name here, setting it to 'Crew #' where # is the iteration of the loop. Then I'll finish assigning stats. I keep getting this error: 108 C:\Dev-Cpp\main.cpp invalid conversion from `int' to `const char*' I don't understand why this is occuring. From when I studied C (and I was only studying the basics) you could assign an int to a string. I tried using casting to assign 'counter' to the name as a char, but it isn't working. I have some ideas why this might be going wrong..but I need some feedback! Help?

Share this post


Link to post
Share on other sites
Advertisement
Nope, you can't directly strcat an int into a string. If you want to do it C-style, you'll need to use either sprintf or itoa for the conversion from number to string.

Something like:
sprintf(crewmember[counter].name, "%s %d", "Crew ", counter);

Share this post


Link to post
Share on other sites
The error you're recieving is basically saying the compiler doesn't know how to convert an int to a string (char *).

Now, the C++ way to do this would be to use a istringstream and also, more importanly, use std::string instead of the nasty char *

So, here's how I would write your code (using a std::vector:

#include <string>
#include <sstream>
#include <vector>

typedef std::vector<std::string> CrewContainer;

CrewContainer GenerateCrew()
{
const int DEFAULT_CREW_NUMBER = 9;
CrewContainer crew(DEFAULT_CREW_NUMBER); //Allocate default storage for DEFAULT_CREW_NUMBER of elements

for (int i = 0; i < DEFAULT_CREW_NUMBER; ++i)
{
std::istringstream converter; //Used to format our string
converter << "Crew " << i;

crew.push_back(converter.str()); //Convert the std::istringstream to a std::string and add it to the back of our container
}

return crew;
}




However, I think what you wanted to do was this: (Only works for numbers 0 through to 9)

//assuming crewmember[counter].name has already been allocated and has enough room
for (counter=1; counter <= generator_crewnumber; counter++)
{
strcpy(crewmember[counter].name, "Crew ");
crewmember[counter].name[5] = (char)(counter + (int)'\0');
}


HTH

Share this post


Link to post
Share on other sites
Something Jesse didn't mention is that the first three lines of what you posted isn't valid C or C++ either. The first one is meaningless, and none of them are terminated by semicolons as declarations should be. I'd highly recommend you "start over from scratch", by picking up a good beginner's C++ book and working through it. I definitely recommend C++ over C.

Share this post


Link to post
Share on other sites
Thanks for the advice. Tdragon, you're right. I was including the variable types to help people out when looking at the code. Thanks for noticing though.

Share this post


Link to post
Share on other sites
Quote:
Original post by desertcube
The error you're recieving is basically saying the compiler doesn't know how to convert an int to a string (char *).

Now, the C++ way to do this would be to use a istringstream and also, more importanly, use std::string instead of the nasty char *

So, here's how I would write your code (using a std::vector:
*** Source Snippet Removed ***

However, I think what you wanted to do was this: (Only works for numbers 0 through to 9)
*** Source Snippet Removed ***
HTH



Is a standard vector something that is in the Visual C++ libraries?

Share this post


Link to post
Share on other sites
Quote:
Original post by Dreamshadow
Is a standard vector something that is in the Visual C++ libraries?


Yes it is and is very usefull! Here's an example i found with google.

I think though you can't beat a good STL book - the STL (I think its proper name now is actually the Standard C++ Library IIRC) will make developing code so much easier and, more importantly, less error prone. Also worth checking out are the Boost libraries, all code is ANSI C++ and there are some really usefull utilities.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!