By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,128 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,128 IT Pros & Developers. It's quick & easy.

string to char array

P: n/a
I am trying to make a command interpreter for a mud that i am working
on the problem i am having right now is that i cannot convert the
string into a char array.

This is the error I am getting now:

In file included from src/mud.cpp:3:
src/command.h: In static member function `static void
command::getCommand(std::string)':
src/command.h:38: error: incompatible types in assignment of `const
char*' to `char[256]'

the third line is just an include statement for command.h

this is the code for command.h

#ifndef COMMAND_H
#define COMMAND_H

#include <iostream>
#include <string>
#include <vector>
#include <cstring>

using namespace std;

class command {
public:
static void tokenizer(string str, vector<string>& words, string
delimiter);
static void getCommand(string c);
private:
static vector<stringcmd;
};
vector<stringcommand::cmd;
void command::tokenizer(string str, vector<string>& words, string
delimiter){
string::size_type pos1 = 0;
string::size_type pos2 = str.find_first_of(delimiter);
words.push_back(str.substr(pos1 ,pos2 - pos1));
pos1 = pos2 + 1;
while(pos2 <= str.length()){
pos2 = str.find_first_of(delimiter, pos1);
if(pos2 str.length()){
pos2 = str.length();
}
words.push_back(str.substr(pos1 ,pos2 - pos1));
pos1 = pos2 + 1;
pos2 += 1;
}
}
void command::getCommand(string c){
tokenizer(c, cmd, " ");
if(cmd.size() == 1){
char oneWord[256];
oneWord = cmd[0].c_str();
for(int i = 0; oneWord.length; i++){
oneWord[i].toLower();
}
cmd[0] = oneWord;
// Move North
if(cmd[0] = "n" || cmd[0] = "north"){
}
// Move South
if(cmd[0] = "s" || cmd[0] = "south"){
}
// Move East
if(cmd[0] = "e" || cmd[0] = "east"){
}
// Move West
if(cmd[0] = "w" || cmd[0] = "west"){
}
// Move Up
if(cmd[0] = "u" || cmd[0] = "up"){
}
// Move Down
if(cmd[0] = "d" || cmd[0] = "down"){
}

} else if(cmd.size() == 2){
} else {
cout << "invalid command!\n";
}
}

#endif

Jul 13 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Thanks in advance for any help.

Jul 13 '06 #2

P: n/a
* Felix85:
I am trying to make a command interpreter for a mud that i am working
on the problem i am having right now is that i cannot convert the
string into a char array.
Use std::string exclusively.

This is the error I am getting now:

In file included from src/mud.cpp:3:
src/command.h: In static member function `static void
command::getCommand(std::string)':
src/command.h:38: error: incompatible types in assignment of `const
char*' to `char[256]'

the third line is just an include statement for command.h

this is the code for command.h

#ifndef COMMAND_H
#define COMMAND_H

#include <iostream>
As a rule, don't include "nice to have" headers in headers.

#include <string>
#include <vector>
#include <cstring>

using namespace std;
See the FAQ item "Should I use using namespace std in my code?",
currently at <url:
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5>.

In addition to what's mentioned in that item, the most important:

DON'T EVER PUT THAT IN A HEADER.
class command {
public:
static void tokenizer(string str, vector<string>& words, string
delimiter);
static void getCommand(string c);
private:
static vector<stringcmd;
};
Seems OK except for visual layout: it's not visually clear what's public
and what's private. The names could have been better, e.g.

tokenizer - tokenize
getCommand - command
cmd - theCmd (or, for non-static, myCmd or cmd_).

vector<stringcommand::cmd;
No, you should not have this definition in a header file, because it
will be multiply defined (linker error) if the header is included in
more than one compilation unit. It belongs in an implementation file,
separately compiled. The easiest alternative, if you want to avoid a
separate implementation file, is to define inside a function, like

inline std::vector<std::string>& command::theCmdRef()
{
static std::vector<std::stringtheCmd;
return theCmd;
}

void command::tokenizer(string str, vector<string>& words, string
delimiter)
Needs to be declared 'inline' if you want to have it in the header file,
so as to avoid multiple definition error.

Also declare the string arguments as 'std::string const& someName'.
{
string::size_type pos1 = 0;
string::size_type pos2 = str.find_first_of(delimiter);
words.push_back(str.substr(pos1 ,pos2 - pos1));
pos1 = pos2 + 1;
while(pos2 <= str.length()){
pos2 = str.find_first_of(delimiter, pos1);
if(pos2 str.length()){
pos2 = str.length();
}
words.push_back(str.substr(pos1 ,pos2 - pos1));
pos1 = pos2 + 1;
pos2 += 1;
}
}
void command::getCommand(string c){
'inline', pass by ref.

tokenizer(c, cmd, " ");
if(cmd.size() == 1){
char oneWord[256];
Use a std::string (or reference to one).

oneWord = cmd[0].c_str();
Can't assign to array.

for(int i = 0; oneWord.length; i++){
oneWord[i].toLower();
}
cmd[0] = oneWord;
// Move North
if(cmd[0] = "n" || cmd[0] = "north"){
}
// Move South
if(cmd[0] = "s" || cmd[0] = "south"){
}
// Move East
if(cmd[0] = "e" || cmd[0] = "east"){
}
// Move West
if(cmd[0] = "w" || cmd[0] = "west"){
}
// Move Up
if(cmd[0] = "u" || cmd[0] = "up"){
}
// Move Down
if(cmd[0] = "d" || cmd[0] = "down"){
}

} else if(cmd.size() == 2){
} else {
cout << "invalid command!\n";
}
}

#endif
I suggest rethinking whether this really is a class: to me it looks like
a set of functions communicating via a global (the static 'cmd').
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 13 '06 #3

P: n/a
Felix85 wrote:
I am trying to make a command interpreter for a mud that i am working
on the problem i am having right now is that i cannot convert the
string into a char array.

This is the error I am getting now:

In file included from src/mud.cpp:3:
src/command.h: In static member function `static void
command::getCommand(std::string)':
src/command.h:38: error: incompatible types in assignment of `const
char*' to `char[256]'

the third line is just an include statement for command.h

this is the code for command.h

#ifndef COMMAND_H
#define COMMAND_H

#include <iostream>
#include <string>
#include <vector>
#include <cstring>

using namespace std;

class command {
public:
static void tokenizer(string str, vector<string>& words, string
delimiter);
static void getCommand(string c);
private:
static vector<stringcmd;
};
vector<stringcommand::cmd;
void command::tokenizer(string str, vector<string>& words, string
delimiter){
string::size_type pos1 = 0;
string::size_type pos2 = str.find_first_of(delimiter);
words.push_back(str.substr(pos1 ,pos2 - pos1));
pos1 = pos2 + 1;
while(pos2 <= str.length()){
pos2 = str.find_first_of(delimiter, pos1);
if(pos2 str.length()){
pos2 = str.length();
}
words.push_back(str.substr(pos1 ,pos2 - pos1));
pos1 = pos2 + 1;
pos2 += 1;
}
}
void command::getCommand(string c){
tokenizer(c, cmd, " ");
if(cmd.size() == 1){
char oneWord[256];
The use of a fixed length array is not so good. To hard code the length into
the code is not good either. Why don't you use a string instead?
oneWord = cmd[0].c_str();
Note that c_str() returns a char const *, not an array.
for(int i = 0; oneWord.length; i++){
oneWord[i].toLower();
Would this compile? oneWord[i] is a char. That does not have a member
function toLower().
}
cmd[0] = oneWord;
Now, you assign back: in other words, the whole detour was meant to turn
cmd[0] into all lower case. So, what about:

for ( std::string::iterator iter = cmd[0].begin();
iter != cmd[0].end(); ++iter ) {
*iter = std::tolower( *iter, std::locale() );
}

// Move North
if(cmd[0] = "n" || cmd[0] = "north"){
}
// Move South
if(cmd[0] = "s" || cmd[0] = "south"){
}
// Move East
if(cmd[0] = "e" || cmd[0] = "east"){
}
// Move West
if(cmd[0] = "w" || cmd[0] = "west"){
}
// Move Up
if(cmd[0] = "u" || cmd[0] = "up"){
}
// Move Down
if(cmd[0] = "d" || cmd[0] = "down"){
}

} else if(cmd.size() == 2){
} else {
cout << "invalid command!\n";
}
}

#endif

Best

Kai-Uwe Bux

Jul 13 '06 #4

P: n/a
I guess to make this more understandable, im new to C++. Im guessing
you already know this for all the mistakes that you have pointed out. I
am learning as i go. Thanks for the help you have given me.

Jul 13 '06 #5

P: n/a
Felix85 wrote:
I guess to make this more understandable, im new to C++. Im guessing
you already know this for all the mistakes that you have pointed out.
I am learning as i go. Thanks for the help you have given me.

Please quote enough of the previous message for context. I know Google
does that automatically now. See the way everybody else does it.


Brian
Jul 13 '06 #6

P: n/a
Felix85 wrote:
I guess to make this more understandable, im new to C++. Im guessing
you already know this for all the mistakes that you have pointed out. I
am learning as i go. Thanks for the help you have given me.
It may not be wise to suggest this, based on the rest of the thread,
but you might look into boost::spirit, which has already done the work
of implememnting routines to parse strings. You would then merely need
to write a grammar and deal with the parsed output. This becomes
important when you start to allow more structured commands, like "give
jewelled sword to orc", which you might imagine would be pretty
difficult to parse by just extending what you've already got.

steve

Jul 13 '06 #7

P: n/a

"Steve Hicks" <st**********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Felix85 wrote:
>I guess to make this more understandable, im new to C++. Im guessing
you already know this for all the mistakes that you have pointed out. I
am learning as i go. Thanks for the help you have given me.

It may not be wise to suggest this, based on the rest of the thread,
but you might look into boost::spirit, which has already done the work
of implememnting routines to parse strings. You would then merely need
to write a grammar and deal with the parsed output. This becomes
important when you start to allow more structured commands, like "give
jewelled sword to orc", which you might imagine would be pretty
difficult to parse by just extending what you've already got.

steve
Fortunately, nobody in their right mind would give a jewelled sword to an
orc, except perhaps pointy end first. :-)

-Howard
Jul 13 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.