simple cin question

This is a discussion on simple cin question within the C++ Programming forums, part of the General Programming Boards category; I'm trying to read in a string that I'm throwing into a char buff[100].... but I need the spaces .... ...

  1. #1
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101

    simple cin question

    I'm trying to read in a string that I'm throwing into a char buff[100]....

    but I need the spaces .... so when I enter "this life"...it only takes "this"...

    I tried to use
    Code:
    cin.getline(buff, 100)
    but that didn't work

    I also tried to use
    Code:
     getline(cin, buff)
    with no third parameter for delimiters cause I don't need any...but that just throw errors all over the place so I'm not so sure what I should use and why cin sucks so bad!! Or maybe I just suck and cin is ok.....Any help would be appreciated

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Use a string, not a char array.

    Code:
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
    	//char buf[100]; /* Not this. */
    	std::string buf;
    	
    	getline(std::cin,buf);
    
    	...
    	
    	return 0;
    }

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    You could use:

    Code:
    getline(cin, VARIABLE, '\n');
    Use a string, not a char array.
    If he wants to use a char array, he can use a char array. (Of course it's easier to use a C++ string, but maybe he wants to experiment with C-strings?)
    Last edited by Ideswa; 05-09-2007 at 10:47 AM. Reason: addition
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Of course, he can use a char array, but getline() only works on strings I believe. Besides, this is a much easier way to read off an entire line. In C, reading an entire line can be more of a mess with memory management and pointers, at least for a beginner.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    There are two version of getline, one for strings, one for char arrays. Obviously I'd suggest using the string version only because I'd suggest using strings rather than char arrays.

    If either getline isn't working, post the full code. One common issue is if you use cin>> before using getline. This leaves a newline in the stream and getline reads that newline and stops. The solution is to add cin.ignore() after the cin >> call to ignore that newline.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,704
    It seems to me that:
    Code:
    cin.getline(buff, 100);
    should work. How does it not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Can we ban all posts that only describe problems as code that "doesn't work" or any variation of it? lol....

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    What laserlight just said, should work. Out of habit, if working with the operator I generally unset skipws when working with chars.

    in.unsetf(ios::skipws); // don't skip whitespaces

  9. #9
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    hmmmmmmmmmmmm neither one of those worked....the

    Code:
     getline(std::cin,buf)
    compiled but just skipped over the entire input question

    Code:
    getLine(cin, VARIABLE, '\n')
    threw a bunch of errors saying that getLine() only takes 2 arguments but 3 were provided.

    agghhhhh

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,704
    As far as I can tell, getLine() does not exist. C++ identifiers are case sensitive.

    Try:
    Code:
    #include <iostream>
    
    int main()
    {
        char buff[100];
        std::cin.getline(buff, 100);
        std::cout << buff << std::endl;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> getline(std::cin,buf) compiled but just skipped over the entire input question
    There are two common causes to this. One is if you are using Visual C++ 6.0. There is a bug in that compiler's standard library. If you are using that compiler, let us know and we can point you to the bug fix.

    The other reason is the one I mentioned above. Are you using cin >> for other data before you call getline?

  12. #12
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    laserlight your suggestion just skipped over the input again. I'm at a loss.

    here's the code I used:

    Code:
    cout << "Enter book title: " << endl;
    		std::cin.getline(buf, 100);
    with char buf[100]; declaration.....

    And I'm using Visual C++ Express 8.0
    Last edited by verbity; 05-09-2007 at 12:32 PM. Reason: forgot something

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,704
    Post the smallest and simplest compilable program that demonstrates the problem. As Daved pointed out, you could have other nearby (or not so nearby) code that is interfering.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Are you using cin >> for other data before you call getline?

  15. #15
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    here's my basic stuff...obviously not finished yet...

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <istream>
    #include <string>
    #include "Recording.h"
    #include "Book.h"
    #include "Holding.h"
    
    using namespace std;
    
    
    int main(void)
    {
    	//need to declare an array of five pointers to Holding
    
    	char holdingChoice, toLowerChoice;
    	char bookTitle[100] , bookAuthor[100], recordingTitle[100], recordingPerformer[100],recordingFormat[100];
    	int bookCallNum, recordingCallNum;
    	char buf[100];
    
    	cout << "Enter holdings to be stored in list: " << endl;
    
    	cout << "\nEnter B for book or R for recording: " << endl;
    	cin >> holdingChoice;
    
    	toLowerChoice = tolower(holdingChoice);
    	if(toLowerChoice == 'b')
    	{
    		cout << "Enter book title: " << endl;
    		std::cin.getline(buf, 100);
    
    		cout << "Enter author: " << endl;
    		cin >> bookAuthor;
    
    		cout << "Enter call number: " << endl;
    		cin >> bookCallNum;
    	}
    	else if(toLowerChoice = 'r')
    	{
    		cout << "Enter recording title: " << endl;
    		cin >> recordingTitle;
    
    		cout << "Enter performer: " << endl;
    		cin >> recordingPerformer;
    
    		cout << "Enter format: " << endl;
    		cin >> recordingFormat;
    
    		cout << "Enter call number: " << endl;
    		cin >> recordingCallNum;
    	}
    
    	
    
    
    	return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. cin question
    By Zico in forum C++ Programming
    Replies: 10
    Last Post: 04-12-2002, 05:25 AM
  4. cin question....
    By Alien_Freak in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2002, 10:29 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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