473,246 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

string to char array

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
7 8531
Thanks in advance for any help.

Jul 13 '06 #2
* 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
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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: J. Campbell | last post by:
I'm a novice with c/c++ and have been reading Eckel's book. I'd like some feedback on using this method. What I need to do is treat a string as numeric data. I know how to write functions to...
3
by: Mark Miller | last post by:
I have a char array and when I write it to a file using BinaryWriter the position of the pointer is the size of the array + 1. For example: writing char leaves the pointer at position 26 after...
27
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I...
6
by: A_StClaire_ | last post by:
hi, wrote the following program to learn a bit about auto pointers. weird thing is, the char array 'name' with size 'max_size' displays a bunch of messed up symbols after the "John Smith" if...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
2
by: Amrit Kohli | last post by:
Hello. I have the following code, to do a simple operation by copying the elements of a vector of strings into an array of char pointers. However, when I run this code, the first element in the...
15
by: Kueishiong Tu | last post by:
How do I copy the content of a string in one encoding (in my case big5) to a char array (unmanaged) of the same encoding? I try the following String line = S"123水泥"; char buffer; ...
13
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things...
2
by: nagesh0280 | last post by:
Hi experts, I'm from a Verilog HDL background and trying to learn C. There are a lot of similarities between Verilog and C but the concept of char arrays and strings has me confused. I'd...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.