Jump to content
  • Advertisement
Sign in to follow this  
gaminlegend38

Solved - String function problem...

This topic is 4887 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is my first post here, but I'd like to say I've learned a lot in the past few days I've been here. Anyway, in the tutorial I'm reading, the author is talking about strings. He listed a few string functions but I'm getting an error with the "strcat" function. Here's the exact code:
#include <iostream>
#include <cstdlib>

using namespace std;

int main(){
    char myArray[50];
    cout << "Whats the password? ";
    cin.getline( myArray, 50, '\n');
    if( !strcmp( myArray, "cheesecake")){
        strcat( myArray, " is correct! Access granted!\n");
    } else {
        strcpy( myArray, "Invalid password!\n");
    }
    cout << myArray;
    system("pause");
}


Here's the error I get: implicit declaration of function "int strcat(...)" I'm using Dev-C++ for the compiler by the way. Thanks for the help in advance. [Edited by - gaminlegend38 on July 29, 2005 8:16:18 PM]

Share this post


Link to post
Share on other sites
Advertisement
The functions strcmp(), strcpy(), and strcat() are C library functions declared in string.h. You need:

#include <string.h>

Share this post


Link to post
Share on other sites
Quote:
Original post by Adamb
Why not just use printf(...)?
Because it doesn't teach string handling.

For standard C++, you need <cstring>, not <string.h>.

Share this post


Link to post
Share on other sites
Thanks for the quick replies, guys. I probably should of mention that I'm a complete newbie when it comes to C or C++ in my first post but thanks anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
The functions strcmp(), strcpy(), and strcat() are C library functions declared in string.h. You need:

#include <string.h>


Er, that's spelled <cstring> these days. But we're not going to use it anyway, because:

Quote:
Original post by gaminlegend38
This is my first post here, but I'd like to say I've learned a lot in the past few days I've been here.

Anyway, in the tutorial I'm reading, the author is talking about strings.


No, he isn't. He's talking crazy dinosaur-era C heresy-speak. :)

C++ provides a built-in (okay, it's not in the language itself, but it's in the standard library) string (textual) type, called std::string (i.e. it's a class declared in the std namespace, the same way that iostream is a class in that namespace and cin and cout are instances of it).

Anyway, it lets you do things with text in much more obvious ways, while avoiding problems with buffer sizes, memory allocation issues (notably, the differences between pointers and arrays), accidentally writing slow code (ask MaulingMonkey to explain this part if it interests you ;) ), and a bunch of other gotchas.


#include <iostream>
#include <string>

using namespace std;

int main(){
string myArray; // a single instance represents text of any length as needed
cout << "Whats the password? ";
// To read into a std::string requires a different function.
// It's no longer a member function of the stream (confusingly enough), but
// the length parameter is no longer needed (since the string will expand
// automatically to hold the line however long it is).
getline(cin, myArray);
if (myArray == "cheesecake") { // Isn't that much more clear?
// std::strings can be compared to each other, and there is an implicit
// conversion from a char* to std::string so that you can do this
// comparison. However you cannot meaningfully compare two char*'s this way
// (that comparison is already defined to compare the pointer values, i.e.
// *where* the data happen to be), and there is no implicit conversion back
// the other way (although there is a member function to convert explicitly
// when you need to).
myArray += " is correct! Access granted!\n";
// and yes, + is defined in a corresponding way. However you can only
// append other strings this way (or char*'s via the implicit conversion);
// for appending text representations of numbers etc., look into
// std::stringstream.
} else {
myArray = "Invalid password!\n";
}
cout << myArray; // we can output it in exactly the same way.
system("pause");
}


Unfortunately, an example this simple only shows a small portion of the benefits provided by the std::string type :/

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!