Thread: Help with functions and cin

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't use >> to read something with spaces in it.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your current code is not using getline to read in the address. Any input that contains spaces should be read with getline. The suggestion to add the call to ignore() was not meant to replace the suggestion to use getline.

    The call to cin.ignore() should be after all normal calls to cin >>. It is only technically needed between a call to cin >> and a call to getline, but because it can be hard to tell in some programs where exactly that happens, and because it is harmless to put cin.ignore() between calls to cin >>, the easiest correct solution is to always put cin.ignore() directly after a call to cin >> (but never directly after a call to getline).

  3. #18
    Registered User
    Join Date
    Sep 2009
    Posts
    16
    i get an error:
    Code:
    	cout << "Address: ";
    	cin.ignore();
    	cin.getline(theAddress);
    	//cin >> theAddress;
    the error is:
    f:\c++2\addressClass\addressClass.cpp(115): error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    David

  4. #19
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    I believe what you want is std::getline, not cin.getline. The cin version takes an array of chars, IIRC, while the std::getline version takes an input stream and a string. It optionally takes a character delimiter (it defaults to newline).

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You used getline correctly in your own code earlier in the thread. Use that code as an example of how to use it.

  6. #21
    Registered User
    Join Date
    Sep 2009
    Posts
    16
    Got it working finally! YAY!
    Code:
    cout << "Address: ";
    	cin.ignore();
    	getline(std::cin, theAddress);
    Thanks for the help!

    David

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. Variables and Functions
    By Hitetsu in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2006, 08:01 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. functions breaking other functions
    By eth0 in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2004, 03:05 PM
  5. Newbie asking about overloading functions
    By GLR in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2001, 10:54 AM