-
need help with functions
here is my code. i am trying to write a simple program in which when the user's input is "monkey", my function adds the word "bars" to it. here is the code.
#include <stdafx.h>
using namespace std;
int List(char word[250]);
int main()
{
char word[250];
cout << "Please enter the word 'Monkey': ";
cin >> word;
List(word);
cout << "Here's the added suffix: " << word << "\n";
return 0;
}
int List(char word[250] )
{
if (word == "monkey" || word == "Monkey")
{
word = strcat(word , "Bars");
}
return 0;
}
the program runs, but does not add "Bars". what is the problem
-
You can't compare character arrays using ==, nor can you assign them, using =. You either want to use the C++ string class, or use something like the strcmp to compare C style strings.
While you're at it, go back and read the Announcements, and then post code next time with [code] tags.
[edit]
Lucky you, you're the recipient of my 4000th post.
Well, 4000th for this incarnation of the C board anyway... ;)
[/edit]
Quzah.
-
im sorry if it strained your eyes because i didnt put [code]
-
It doesn't bother me. I'll either not bother taking the time to read your code, or I'll assume you write sloppy code. Either way, it only hurts you, not me.
Quzah.