Thread: learning to use arrays and C-strings together

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    learning to use arrays and C-strings together

    Hi

    1: I don't understand how one could enter the all the elements on a character array with one cin. This seeming works as is shown in the OUTPUT code at the bottom.

    One need to use cin.get() when there is a white space between the elements because white space is taken to be a null character which signals the end of the string.

    2: Why doesn't the for loop work? Why do I have to enter the array element in a column form?

    3: Is "C" in cin.get() always the size of the array?

    Code:
    // read_your_name.cpp
    // read and print your name using an array and using
    // cin, for loop, cin.get()
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    const int C = 20;
    
    int main()
    {
            int i;
    
            char name[C];
    
            cout << "enter your name: ";
            cin >> name;
    
            cout << "your name is: Mr. " << name << endl;
    
    
            cout << "enter your name: ";
    
            for (i=0; i<C; i++)
            {
                    cin >> name[i];
                    cin.ignore();
            }
    
            cout << "your name is: Mr. ";
    
            for (i=0; i<C; i++)
            {
                    cout << name[i];
            }
    
    
            cout << "\nenter your name: ";
            cin.get(name, C);
    
            cout << "your name is: Mr. " << name << endl;
    
            system("pause");
            return 0;
    }
    OUTPUT
    Code:
    enter your name: jackson
    your name is: Mr. jackson
    enter your name: j
    a
    c
    k
    s
    o
    n
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    your name is: Mr. jackson1111111111111enter your name: jackson heights
    your name is: Mr. jackson heights
    Press any key to continue . . .
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    2. because you're telling it to read a character. istream:perator>> - C++ Reference

    you're instructing it to read a character (by invoking the char& overload of >>)

    3. again, refer to the documentation: istream::get - C++ Reference


    the BEST thing you can do is to always read the docs first. i know as a beginner it's not very accessible, but to master the art of reading code documentation is to empower yourself to quickly learn new technologies. always always ALWAYS read the docs first, and ask questions about what you don't understand in them second. imo, this is the fast-track to becoming a proficient programmer.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jackson6612 View Post
    1: I don't understand how one could enter the all the elements on a character array with one cin. This seeming works as is shown in the OUTPUT code at the bottom.
    What are you confused about? You already input a name in one statement with the std::cin >> name.
    Btw, never ever do that. This is prone to buffer overflows and is as bad as gets.
    Instead, use std::cin.getline.

    One need to use cin.get() when there is a white space between the elements because white space is taken to be a null character which signals the end of the string.
    Where is the question?

    2: Why doesn't the for loop work? Why do I have to enter the array element in a column form?
    The loop works perfectly. Remember that std::cin is line buffered. That is, it only returns after you've pressed enter. So each std::cin statement in the loop wants a newline before it returns.

    3: Is "C" in cin.get() always the size of the array?
    No, it is the number of elements you want to read from the stream. Make sure this is equal to or less than the array size.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    Quote Originally Posted by jackson6612 View Post
    Hi

    1: I don't understand how one could enter the all the elements on a character array with one cin. This seeming works as is shown in the OUTPUT code at the bottom.

    One need to use cin.get() when there is a white space between the elements because white space is taken to be a null character which signals the end of the string.

    2: Why doesn't the for loop work? Why do I have to enter the array element in a column form?

    3: Is "C" in cin.get() always the size of the array?
    Include the string.h library and declare the array as a string. By doing this you will have a multi-character for each position of the array.
    "A Computer in every desk and in every home, running Microsoft software."

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, m37hod.

    Quote Originally Posted by Elysia View Post
    What are you confused about? You already input a name in one statement with the std::cin >> name.
    Btw, never ever do that. This is prone to buffer overflows and is as bad as gets.
    Instead, use std::cin.getline.
    Thanks a lot, Elysia. Yes, I'm confused because I don't really know what I'm doing.

    I'm surprised that I was able to input values for all the array elements with one cin. But it seems it would only work for arrays of char type. It wouldn't work for an array of int type. Why I think so? An element of array of char type could obvious hold only one digit (such as '1', '2', '3', etc.) or one character (such as 'a', 'A', 'c', etc.); in other words the 'size' of the element in this case is fixed. But for an element of array type int type the 'size' is not fixed. So, probably, when we input values for an array of char type, when we have entered one character with cin, we have already entered second element, and so on.


    Quote Originally Posted by Elysia View Post
    The loop works perfectly. Remember that std::cin is line buffered. That is, it only returns after you've pressed enter. So each std::cin statement in the loop wants a newline before it returns.
    Is there any way to make it so that I can input values in a row rather than a column?
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jackson6612 View Post
    I'm surprised that I was able to input values for all the array elements with one cin. But it seems it would only work for arrays of char type. It wouldn't work for an array of int type. Why I think so? An element of array of char type could obvious hold only one digit (such as '1', '2', '3', etc.) or one character (such as 'a', 'A', 'c', etc.); in other words the 'size' of the element in this case is fixed. But for an element of array type int type the 'size' is not fixed. So, probably, when we input values for an array of char type, when we have entered one character with cin, we have already entered second element, and so on.
    It has nothing to do with that.
    Strings are an exception, and that is all.

    Is there any way to make it so that I can input values in a row rather than a column?
    You use std::cin.getline or std::getline instead of std::cin >>.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, Elysia.

    What's the difference between cin.get() and cin.getline()? In the code below I could have used cin.get with the same effect.

    Code:
    // istream getline
    #include <iostream>
    using namespace std;
    
    int main () {
      char name[256], title[256];
    
      cout << "Enter your name: ";
      cin.getline (name,256);
    
      cout << "Enter your favourite movie: ";
      cin.getline (title,256);
    
      cout << name << "'s favourite movie is " << title;
    
      return 0;
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps you should read the documentation: istream - C++ Reference
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi Elysia

    I have been to that resource but just got more confused. So, if it's possible, let me have some information about the difference between the two. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by godly 20 View Post
    Include the string.h library and declare the array as a string. By doing this you will have a multi-character for each position of the array.
    including string.h will only get you c-style string functions. the thing to do (unless you're using an ancient compiler like borland or VC++ 4) is include <string>

    all of the standard C++ headers nowadays have no .h extension.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    i dont think cin.get will give you the same effect as cin.getline gives.
    because cin.get will not capture the \n entered in the first input to the name.

    have you tried?

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    The results are weird. Here is the code and the output.

    Code:
    // get_and_getline.cpp
    // difference between cin.get() and cin.getline()
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
            const int C = 10;
    
            char str[C];
    
            cout << "enter your name: ";
            cin.get(str, C);
    
            cout << "your name is: " << str << endl;
    
            cout << "enter your name again: ";
            cin.getline(str, C);
    
            cout << "your name is: " << str << endl;
    
            system("pause");
            return 0;
    
    }
    OUTPUT
    Code:
    enter your name: jackson
    your name is: jackson
    enter your name again: your name is:
    Press any key to continue . . .
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jackson6612 View Post
    Hi Elysia

    I have been to that resource but just got more confused. So, if it's possible, let me have some information about the difference between the two. Thanks.
    So what you're saying is that you can't read plain English, but you want us to write more English to tell you what the other English said?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jackson6612 View Post
    The results are weird. Here is the code and the output.
    The results are weird because you did not read the documentation.
    std::cin.get does not extract the newline from the buffer.
    std::cin.getline does extract the newline from the buffer and discard it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings in arrays
    By hellogamesmaste in forum C Programming
    Replies: 1
    Last Post: 08-13-2009, 09:15 AM
  2. Arrays/strings
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 10-22-2008, 07:31 PM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. Learning arrays
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-09-2002, 10:27 PM
  5. Strings & Arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-11-2001, 07:50 PM