Thread: Array question

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    Array question

    Hey guys I was just wondering when entering data into a char array for example using getline and someone input the following:

    "this is a test"

    and then in the same array using the getline "test" was entered. would the array then contain "test is a test" or is a null terminator automatically included when something is added to the array?

    Thanks.

    EDIT: my bad should have said getline
    Last edited by 182; 04-23-2006 at 02:04 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by 182
    and then in the same array using the geting "test" was entered. would the array then contain "test is a test" or is a null terminator automatically included when something is added to the array?
    Huh??? "using the geting"
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Try it! But I'm pretty sure it is added.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    First of all, cin.get() is used to fill empty character arrays. You have to use a C-string function to replace all or part of a string:
    Code:
    #include <cstring>
    #include <iostream>
    
    int main()
    {
        char buff[40], test[5] = "Test";
        std::cin.get(buff, 40);
        
        std::cout << strncpy(buff, test, 4);
           
        std::cin.ignore();
        std::cin.get(); // pause for user
        return 0;
    }
    Output:
    Code:
    This is a test.
    Test is a test.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    get() reads a set number of bytes so of course it won't append extra data. The OP mentioned getline which does add a NULL terminator (just tested this). Also you use strncpy which explicitly says in the docs doesn't add a NULL-terminator.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM