Thread: Tutorial Help

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    20

    Tutorial Help

    Hi, I am on the tutorial and I need some help that is probably incredibly basic to u guys. In the tutorial, when explaining strings, it says:

    Code:
    #include <iostream> //For cout
    #include <cstring>  //For the string functions
    
    using namespace std;
    
    int main()
    {
      char name[50];
      char lastname[50];
      char fullname[100]; // Big enough to hold both name and lastname
      
      cout<<"Please enter your name: ";
      cin.getline ( name, 50 );
      if ( strcmp ( name, "Julienne" ) == 0 ) // Equal strings
        cout<<"That's my name too.\n";
      else                                    // Not equal
        cout<<"That's not my name.\n";
      // Find the length of your name
      cout<<"Your name is "<< strlen ( name ) <<" letters long\n";
      cout<<"Enter your last name: ";
      cin.getline ( lastname, 50 );
      fullname[0] = '\0';            // strcat searches for '\0' to cat after
      strcat ( fullname, name );     // Copy name into full name
      strcat ( fullname, " " );      // We want to separate the names by a space
      strcat ( fullname, lastname ); // Copy lastname onto the end of fullname
      cout<<"Your full name is "<< fullname <<"\n";
      cin.get();

    Could you please explain the line in bold above?

    -Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Which tutorial is that from? I'd recommend learning C++ strings, not the C style strings taught in that code.

    However, if you have some reason to continue to learn C style strings, here is an explanation:

    C style strings require a special character at the end of the string so that you know where it ends. The character that is used is called the null character, and is represented like this: '\0'. Notice that name is an array of 50 characters. But if your name is "Joe" then you don't need all 50 characters. The string is stored as 'J', 'o', 'e', '\0' with another 46 characters following it. You don't care what those characters are, they are probably random garbage from memory. The '\0' indicates the end of the name so that you can ignore those extra 46.

    Now, the strcat function combines two strings by adding one to another. In this case the first time it is run fullname is empty and name is getting added to the end of this empty string. The way strcat works is it finds the end of the first string. But fullname doesn't have a string in it, it still only has garbage characters. By setting the first character of fullname to '\0', the code is indicating to strcat that the fullname string ends at the first character. Otherwise it would look for a '\0' and wouldn't find one until the random garbage in memory accidentally had it in there.

    The null terminator is not terribly intuitive. Using arrays for strings is also more error-prone. I really do hope you skip this tutorial and start using C++ strings.
    Last edited by Daved; 09-11-2007 at 05:39 PM.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    20
    First of all, I found it on this sites C++ tutorials, in the tutorial "strings". If you look on the page http://www.cprogramming.com/tutorial.html you can clearly see it under c++ tutorials.

    As it explains in that tutorial, "strcat is short for string concatenate, which means to add to the end, or append.". The problem I saw is when you added name and lastname to the end, wouldn't you end up with something like "\0 name lastname"? Then the "\0" would be in the beggining causing it to end there. Am I wrong?

    -Thanks
    Last edited by Suudsu2200; 09-11-2007 at 05:50 PM. Reason: Typo's

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    20
    Now, at a closer look, I see it is called "C style strings". Why is it there? That would seem to just confuse people. Also, what is the point?

    -Thanks

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> wouldn't you end up with something like "\0 name lastname"?
    The '\0' marks the end of the string. strcat finds the '\0' from the first string, then removes it and adds the second string at that point. That's why there is no '\0' in the front final string. In the same way, there is a '\0' at the end of name. strcat finds that '\0', removes it and adds lastname at that point. There is a '\0' at the end of lastname as well. That '\0' is left in the final string to indicate the end of the final string.

    >> Why is it there? That would seem to just confuse people.
    C++ is based on C, and in C people use C style strings for their string needs. When C++ was created, many continued to use C style strings, since they still work. Others created string classes to encapsulate the concept of a string and make them easier and safer to use. The language itself was finally standardized in 1998, and at that point an official string interface was chosen and made a part of the standard library. However, at that point a lot of code had been written in C++ that still used C style strings.

    Because C style strings still work, people have been very slow to adopt the standard string class. These tutorials were written a long time ago, before that class became widely used. Now, the C style string tutorial is still available for those who want to learn about them. However, new learners of the language should start with and stick to C++ strings to avoid confusion.

    If you haven't found it already, I believe that http://www.cprogramming.com/tutorial/string.html is the tutorial for the C++ string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 07:38 PM
  2. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Problem with tutorial (Vector class)
    By OdyTHeBear in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2002, 02:49 PM
  5. My DirectInput tutorial....
    By jdinger in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-18-2002, 11:32 PM