Thread: What does this code means?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    Malaysia
    Posts
    13

    What does this code means?

    I have no idea what does this code means even though the book explained it. I think maybe it didn't explain well enough. Can anyone tell me what does this code means?

    Code:
    //This programme shows an example of using an if statement to check for no input
    #include <iostream>
    #include <string>
    
    int main()
    {
        using namespace std;
    
        char response[256];
    
        cout<< "What is your name? ";
        cin.getline(response,256);
    
        if (strlen(response) == 0)
        cout<< "You must tell me your name...";
        else
        cout<< "It's nice to meet you, "<< response;
    
        return 0;
    }
    I also have no idea about the char 256 thing =,=

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you please specify more precisely what part you don't understand?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Location
    Malaysia
    Posts
    13
    Quote Originally Posted by matsp View Post
    Can you please specify more precisely what part you don't understand?

    --
    Mats
    Erm...lets see...

    First, the char response[256]. I only know that it's trying to declare a variable response. I have no idea about the [256].

    Second, the cin.getline(response,256).

    Third, if (strlen(response) == 0)

    That's about all I guess xD

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    this sample uses C-strings that are actually simple arrays of chars

    you better start with the sample that deals with the C++-strings that should look like

    Code:
    string response;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by junkeat90 View Post
    Erm...lets see...

    First, the char response[256]. I only know that it's trying to declare a variable response. I have no idea about the [256].
    An array of char, with 256 elements.

    Second, the cin.getline(response,256).
    Read a line of text, max size 256 chars.

    Third, if (strlen(response) == 0)
    Check the length of the input, if it's zero, then nothing was entered.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Location
    Malaysia
    Posts
    13
    lol..I have no idea about array...guess I'll just continue reading the book in order to explore C++ deeper. Anyway, thank you all for trying to explain. I appreciate it very much.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by junkeat90 View Post
    lol..I have no idea about array...guess I'll just continue reading the book in order to explore C++ deeper. Anyway, thank you all for trying to explain. I appreciate it very much.
    Yes, I expect that you will have to read more about arrays. It is a fairly important part of computer programming in general.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  2. What does this code means??
    By dianazheng in forum C Programming
    Replies: 13
    Last Post: 10-12-2004, 09:45 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM