string to char array

F

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.

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<string> cmd;
};
vector<string> command::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.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
 
K

Kai-Uwe Bux

Felix85 said:
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<string> cmd;
};
vector<string> command::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.toLower();


Would this compile? oneWord 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
 
A

Alf P. Steinbach

* 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<string> cmd;
};

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<string> command::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::string> theCmd;
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.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').
 
F

Felix85

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.
 
D

Default User

Felix85 said:
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
 
S

Steve Hicks

Felix85 said:
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
 
H

Howard

Steve Hicks said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top