   Char confusion!
  K, yesterday I attempted to make my first simple bot program.Here's the source code:   /*Name: PoppetBotDate: 2/16/04Last Modified:Author: Elben ShiraContact: wiseelben@wiseelben.comDescription: PoppetBot is my first attempt at making a bot.*/#include <iostream>#include <cstring>#include <cstdio>using namespace std;void talk();char reply(char toBot);int main(){char toBot[300];cout << "PoppetBot: How are you today, Poppet Master?";while(toBot != "quit"){talk();gets(toBot);cout << "PoppetBot: " << reply(toBot[300]);}return 0;}void talk(){cout << "\n>>:";}char reply(char toBot[300]){char replyFromBot[300];if(toBot == "good" || toBot == "i hate you" || toBot == "im dead" || toBot == "im poppet master")strcpy(replyFromBot, "Oh, really?");else if(toBot == "how are you?")strcpy(replyFromBot, "Good, you?");cout << endl;return replyFromBot[300];}  I got two fatal errors, saying something about unresolved external symbols in the reply(char). I'm guessing that's something wrong with the return type, right?I probally messed up with the chars. My first time using chars, actually. I don't know when to put the size of the chars (char1[200], for example), and when I don't need to (plain char1).Help!   Elben"Give a man a fish and he will eat for a day. Teach a man how to fish and he will eat for a life time."-Chinese ProverbWiseElben.com - Experience Wisdom[edited by - WiseElben on February 16, 2004 6:02:43 PM]
  1) You can''t use the != and == comparisons on char arrays. You should include  and use the strcmp method. Basically you pass it two null-terminated strings, and it will return 0 if they''re the same.2) reply(toBot[300]);  In that call, what you''re really doing is passing the 300th character in the toBot char array. What you need to do is pass the array I think, reply(toBot).3) return replyFromBot[300];   Here you''re doing much the same thing, you''re returning the 300th character in the array, return the whole array return replyFromBot;4) Considering the replyFromBot variable is created on the stack, it is destroyed when the function call ends. You should use something like ``char* replyFromBot = new char[300];`` instead of ``char replyFromBot[300];``Thats about all I can see off hand, if I was mistakened, someone correct me. ;  Oh, that''s simple enough. I was never told you couldn''t use operators on chars.Thanks.   Elben"Give a man a fish and he will eat for a day. Teach a man how to fish and he will eat for a life time."-Chinese ProverbWiseElben.com - Experience Wisdom;  You can use them on chars, but not on char arrays (aka strings). Well, you can use them on char arrays, but you don't get the type of comparison you want. You wont get a full string comparison in other words.In the STL, we have a lovely class called std::string, which makes this much easier [EDIT]Hacked just about everything I said  [edited by - porthios on February 16, 2004 6:30:46 PM];  Yea, I know about std::string, but you can''t use gets() with std::string, and therefore, I can only use cin, which only gets the first word (breaks after space).   Elben"Give a man a fish and he will eat for a day. Teach a man how to fish and he will eat for a life time."-Chinese ProverbWiseElben.com - Experience Wisdom;quote:Original post by WiseElben  Yea, I know about std::string, but you can''t use gets() with std::string, and therefore, I can only use cin, which only gets the first word (breaks after space).   Elben"Give a man a fish and he will eat for a day. Teach a man how to fish and he will eat for a life time."-Chinese ProverbWiseElben.com - Experience Wisdom yes you canjust use .c_str()std::string str;gets(str.c_str());    ;quote:Original post by DerAnged  quote:Original post by WiseElben   Yea, I know about std::string, but you can''t use gets() with std::string, and therefore, I can only use cin, which only gets the first word (breaks after space).   Elben"Give a man a fish and he will eat for a day. Teach a man how to fish and he will eat for a life time."-Chinese ProverbWiseElben.com - Experience Wisdom yes you canjust use .c_str()std::string str;gets(str.c_str());   Very bad idea. Never attempt to write to the space given by c_str(). There''s a reason it return const CharType*.What you''re after is:std::string str;std::getline(std::cin, str);   The following statement is true. The previous statement is false.Shameless promotion:FreePop: The GPL Populous II clone. ;  K, thanks.   Elben"Give a man a fish and he will eat for a day. Teach a man how to fish and he will eat for a life time."-Chinese ProverbWiseElben.com - Experience Wisdom