Thread: C++ Cin Help!

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    16

    Unhappy C++ Cin Help!

    Hello, I have a problem while CIN into an array of Chars.
    Say If I declare an array of Chars of length 10 and prompt
    the user for a CIN. How to I get the language to pick up only
    say 5 letters?!?

    Right now I have the CIN in a loop but it makes sure there
    has to be 10 letters so 5 is not accepted!

    Thank You

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    show us your code, and try using getline function with the length parameter....

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    16
    In a nutshell


    forloop

    cin>>array[variable];


    Is there an API describing C++ functions?

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    2
    Could you not just do something like this
    Code:
    char variable[];
    I think this would make it so that it would go to whatever length the cin uses.

  5. #5
    root
    Join Date
    Sep 2003
    Posts
    232
    >I think this would make it so that it would go to whatever length the cin uses.
    Nope, that's an array without a size which is illegal unless it's an extern declaration defined elsewhere. You must specify a size for built-in arrays, that's one reason the vector class is so useful: You can have variable length vectors without having to worry about memory management. It's a win-win situation.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    16
    how do i find out more about vectors and the cin function?

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    vectors are part of the standard template library, just look that up in google

    If you are going to use vectors then you might as well use strings to not c style strings.

    Strings are part of the standard template library as well and safer especially for dynamicaly allocation.

    I have read in many post that learning about string objects is more important than the deprecated charachter arrays or c style strings. The main reason for learning about c style strings is for the massive amounts of legacy code still in use, but shouldn't generally be used in modern c++ programs.(This is probably gonna get me flamed.)

    cin.getline(array,SIZE);

    will extract charachters from the input stream up to a maximum number of chars-1 specified by SIZE. Note

    cin.get(array,SIZE,'\n');
    reads charachters from the input stream terminates at 1-SIZE or when the delimeter is entered in this case '\n'

    I believe input past the size as prompted by cin stays in the input stream and must be read or ignored.
    cin.ignore();
    Last edited by curlious; 09-15-2003 at 12:40 PM.

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I absolutely agree that using the string class is preferred over using C style char arrays. You still might need to only read in a certain number of characters if you use strings, though. Here is another way to do that that works with strings or char arrays:
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    int main()
    {
        int numLettersToRead = 5;
        char chArray[10];
        std::string stdString;
    
        // Use numLettersToRead+1 to allow for the null character
        // that is automatically tacked on.
        std::cin >> std::setw(numLettersToRead+1) >> chArray;
    
        // Use numLettersToRead since you don't have to worry about
        // the null terminator with the string class.
        std::cin >> std::setw(numLettersToRead) >> stdString;
    
        std::cout << chArray << std::endl;
        std::cout << stdString << std::endl;
    
        return 0;
    }
    Of course, if you don't like all the std:: things everywhere, you can remove them and put using namespace std; after the #include's.

  9. #9
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Originally posted by jlou
    I absolutely agree that using the string class is preferred over using C style char arrays.
    Yes,but it looks like this is an intro c++ course or book...so using cstrings makes the student understand more of what is actually going on. In my intro c++ course we didn't use strings literals until the very end...

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I 've seen/read many arguments for learning C++ strings from the beginning (as well as other STL stuff like vectors), including a paper by Stroustrup. Those arguments make sense to me. The only valid argument I've heard for learning C-style strings first is that eventually a good programmer should know how that stuff works. That makes sense, but doesn't seem as important in my mind as learning the new/preferred way to do things.

    Here is Stroustrup's comments:
    http://www.research.att.com/~bs/new_learning.pdf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 11:14 AM
  2. Check if a cin is null
    By HumbuckeR in forum C++ Programming
    Replies: 6
    Last Post: 04-16-2006, 08:16 AM
  3. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  4. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 02:57 PM
  5. multiple instances of cin
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2004, 04:51 PM