Jump to content
  • Advertisement
Sign in to follow this  
IN1L8

Need help figuring this out.

This topic is 4079 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

I'm a beginner to C++ and I've been learning pretty quickly due to this site I found that provides some great tutorials. Anyways, I got to a section on strings and the example provided tells me nothing. Can somebody please walk me through what this guy is talking about and what each section of the code means? Here's a link to the section: http://www.cppgameprogramming.com/cgi/nav.cgi?page=strings Thanks ahead of time!

Share this post


Link to post
Share on other sites
Advertisement
If they are teaching you that C++ "strings" are arrays of characters defined as "char myArray[somesize]", then they are NOT teaching C++. Also, the information provided in that tutorial is incorrect. For example, strcmp does NOT return "false" when the character arrays match. It returns zero when they match, 1 if the first character array is greater than the second, and -1 if the first character array is less than the second. The functions strcmp, strcpy, and strcat should NEVER be found in a C++ program. I would recommend you find a better set of tutorials.

That being said, beginning with the first line after main:

The first line declares a variable myArray that can hold up 50 characters.

The second line outputs a prompt to the console.

The third line allows the user to type in a string of up to 50 characters. Those characters will be stored in myArray. Input extraction terminates when it encounters a newline character (e.g. where the user hit the return key). That's the "'\n'" parameter.

The fourth line compares the string typed in by the user to the literal string "cheesecake". strcmp will return zero if the two strings are the same. Using the "!" operator is a shortcut for strcmp(myArray,"cheesecake") == 0.

The fifth line will be executed if the two strings are equal. It concatenates (appends) the literal string " is correct! Access granted!\n" to the string the user typed in.

The seventh line will be executed if the two strings are not equal. It copies the literal string "Invalid password!\n" to myArray, replacing the value the user typed in.

The ninth line outputs whatever got stored in myArray to the console.

Share this post


Link to post
Share on other sites
thanks a lot for the help! i just don't get the strcmp, strcat, and strcpy. these are things i've never seen before, so yea... is there any way to rewrite this program or any other program examples that can demonstrate how to use strings in C++?

Share this post


Link to post
Share on other sites
The preferred way to use strings in C++ is via the C++ Standard Library "string" class: std::string. It's a lot more natural feeling than the array-of-chars hacks that C programmers were forced to use:

#include <string> // required for std::string

int main()
{
std::string message = "Hello, world!";
int len = message.length();

//...etc...
}

Good references would be the appropriate chapters in the online books "Thinking in C++" or "C++: A Dialog."

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!