C++ casting issue

Started by
7 comments, last by gumpy 18 years, 7 months ago
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?
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);
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 roomfor (counter=1; counter <= generator_crewnumber; counter++){    strcpy(crewmember[counter].name, "Crew ");    crewmember[counter].name[5] = (char)(counter + (int)'\0');}

HTH
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.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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.
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?
Vector is actually in the standard C++ libraries, it isn't just a Visual C++ thing.
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.
if you already have experience with c then these two free books from bruce eckel would be good for you.

thinking in c++ vol 1
thinking in c++ vol 2

and here's a link to sgi's online stl reference. this is the best reference i know of.

sgi's online stl reference

edit:
and this site has more info and tutorials.
http://www.cprogramming.com/
This space for rent.

This topic is closed to new replies.

Advertisement