cin and char arrays

This is a discussion on cin and char arrays within the C++ Programming forums, part of the General Programming Boards category; Can you cin a char array? Code: char Buffer[81]; cin>>Buffer; Shouldn't this work? As long as there is no space ...

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Arrow cin and char arrays

    Can you cin a char array?
    Code:
     
    char Buffer[81];
    
    cin>>Buffer;
    Shouldn't this work? As long as there is no space shouldn't it read in the array as an array of chars?
    I was advised that we can do it with an integer array, but can I do it with a char array. I'm assuming that there shouldn't be any issue with it, but it doesn't work on my current compile.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    ya but make it this...


    Code:
    cin >> Buffer;
    Your mom is like a struct, she has no class

    How many C programmers does it take to screw in a light bulb? One to do it, 99 to tell him how to do it faster.

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    The >> operator isn't defined for arrays. That operator is defined for STL strings. Or you could use the getline function to read in character arrays.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Question cin.getline

    I'm trying to avoid using cin.getline().
    I'm trying to trouble shoot with only my pc.
    My books are unavailable for now.
    So I do appologize for remedial questions.
    (as if any of mine are not.)

    So cin>> doesn't work for char arrays?

    Am I required to use cin.getline for char arrays v.s. int arrays?
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Actually, this worked for me on VC++ 6.0:
    Code:
    #include <iostream>
    
    int main()
    {
    	char buffer[81];
    	std::cin >> buffer;
    	return 0;
    }
    although I think I read that better code to prevent overflow would be:
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
    	const int bufSize = 81;
    	char buffer[bufSize];
    	std::cin >> std::setw(bufSize) >> buffer;
    	return 0;
    }
    Not sure why it didn't work for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer working in scanf but not in cin.
    By sawer in forum C++ Programming
    Replies: 14
    Last Post: 06-15-2006, 02:15 AM
  2. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 03:38 PM
  3. (C++) two dimensional char arrays and cin
    By Dark Carnival in forum C++ Programming
    Replies: 2
    Last Post: 08-21-2004, 02:32 PM
  4. Char Arrays
    By Borommakot in forum C++ Programming
    Replies: 17
    Last Post: 09-28-2002, 01:58 PM
  5. simple question
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 07-04-2002, 09:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21