Sign in to follow this  
raptorstrike

stupid chars

Recommended Posts

raptorstrike    181
ok i have something like this char* Master_Enemy_Files[] = {"Enemy_One.txt"}; but i want that whole thing to be one element i could do a multidemntional array but thats a PAIN im using c++ and i just want a way to make a const char* {"blabla"}; to be one SINGE entity is there any way to do this

Share this post


Link to post
Share on other sites
JohnBolton    1372
You mean like this?
std::string Master_Enemy_Files[10];

Master_Enemy_Files[0] = "Enemy_One.txt";
Master_Enemy_Files[1] = "Enemy_Two.txt";
Master_Enemy_Files[2] = "Enemy_Three.txt";
...

Share this post


Link to post
Share on other sites
mxrss    133
Quote:

yes thats exactly what i mean but unless you have a really good way to transfer those strings to chars i cant use em


just use the c_str() method in the string class to convert to chars.

- Mike

Share this post


Link to post
Share on other sites
gcs584    280
Hi,

I think it's because people usually get taught char arrays first, so they stick with what they've originally learned. Some people feel more comfortable using already defined data-types instead of user-defined classes.

I have seen some people post threads around the internet talking about how inefficient the string class is because it wastes memory (like when you concatenate). But when you look at the big picture the difference is negligible. It would obviously depend on it's application though.

Just my 2 cents,

Later,

GCS584

Share this post


Link to post
Share on other sites
raptorstrike    181
what files must i include in order to use the c_str method because as of right now it says it dont exist here are the files i have so far

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <vector>
#include <stdlib.h> // Include this to use system()
#include <iostream> // Include our standard header
#include <string> // Include this to use strings
#include <fstream>
#include <libiberty.h>
#include "Ship.h"
#include "Enemy.h"
#include "Level.h"
#include "SDL.h"
#include "Input.h"
using namespace std;

or is this a member function like str1.c_str(char1)?

never mind figured it out its the later

Share this post


Link to post
Share on other sites
raptorstrike    181
ok im having a little trouble here shouldnt this work?

Enemy_List[One->Enemy_Vector[i]->ID].c_str(Enemy_List_C);

string Enemy_List[]= "Enemy_1.bmp";
char* Enemy_List_C= "";

i get this error

Main.cpp:251: error: no matching function for call to `std::basic_string<char,
std::char_traits<char>, std::allocator<char> >::c_str(char*&)'
C:/MinGW/include/c++/3.3.1/bits/basic_string.h:717: error: candidates are:
const _CharT* std::basic_string<_CharT, _Traits, _Alloc>::c_str() const
[with _CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>]

make.exe: *** [Main.o] Error 1
Execution terminated

Share this post


Link to post
Share on other sites
kaysik    382
c_str() returns a const char pointer - it does not take any params. It works like so:


// your std string
std::string myStr = "blah";

// Assing your const char pointer
const char *strPtr = myStr.c_str();



then you can use strPtr as you see fit - although it is const so you can't edit it. If you want to edit it just use the myStr var and all the handy overloaded operators!

Share this post


Link to post
Share on other sites
gcs584    280
Hi,

This is all you do:

string name = "Hello World!";

Then to convert it to char would be name.c_str();

Also include <string> (not string.h)

Later,

GCS584

[EDIT:] Someone beat me too it! Don't pass any parameters to the function.

Share this post


Link to post
Share on other sites
GotenRulezU    100
I love strings :) I can use chars but strings have everything built in right at your fingertips. Like mentioned above the problems that I see are when a function from a library is set to take in a char as an argument and it denies your string lol [smile] Althought there are probably methods in the string class to get around this I'm lazy [smile]

Share this post


Link to post
Share on other sites
technomancer    199
I personnally prefer using STL std::string and std::stringstream over char arrays as it prevents one of the biggest kickers in software development buffer over/underrun. I don't mind M$ CString classes either for the same reasons.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this