Thread: quick question about getline

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    5

    quick question about getline

    hi, i have a little question about cin.getline and cin.get...

    Code:
    char seleccion(){
        char opcion[5];
        while(true){
            menu1();
            cin.getline(opcion,5);
            cin.clear();
            if(strcmp(opcion,"new") == 0) return 1;
            else if(strcmp(opcion,"show")==0) return 2;
            else if(strcmp(opcion,"mod")==0) return 3;
            else if(strcmp(opcion,"del")==0) return 4;
            else if(strcmp(opcion,"exit")==0) return 5;
    
        }
    }

    the problem is that after the first time the getline function doesn't allow me to enter anything, it just fills the string with a newline

    like if i do this

    Code:
    char something[5];
    cin.getline(something,5);
    cout << something << endl;
    cin.getline(something,5);
    cout << something << endl;
    cin.getline(something,5);
    cout << something << endl;
    what i get is

    Code:
    whatever_i_wrote
    newline
    newline
    can please anyone tell me why this happen and what can i do to solve it?


    (sorry if it's hard to understand, english isn't my mother language)

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I'm not sure why it's happening, though I suspect it's because there's not enough space in opcion to hold the newline and a terminating nul, so the newline is left in the stream.

    In any case, don't use C strings.
    Code:
    #include <string>
    int seleccion(){
        using namespace std;
        string opcion;
        while(true){
            menu1();
            getline(cin, opcion);
            cin.clear();
            if(opcion == "new") return 1;
            else if(opcion == "show") return 2;
            else if(opcion == "mod") return 3;
            else if(opcion == "del") return 4;
            else if(opcion == "exit") return 5;
    
        }
    }
    Note that I also changed the return type to int. There's no point in using a char there, and it's confusing.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    If you are writing in C++ you really are better to use the 'string' type instead of char array, unless you have a very good reason not to, and personally i would change while(true) to your own named boolean variable.
    Also with the string type you can avoid using the old c strcmp() function, and instead use the built in string.compare() or even the == operator which is available for the string class. You can see a very handy reference on the string class and all it can do for you here, y tu inglés esta bien...(taking a guess there!! :-> "opción??")
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Question about getline
    By Matty_Alan in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2010, 02:56 AM
  2. Question About Getline
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2009, 05:13 PM
  3. Quick std::string.getline() question.
    By A10 in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 07:33 PM
  4. getline question
    By Swaine777 in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2003, 06:17 AM
  5. STL question: getline
    By *ClownPimp* in forum C++ Programming
    Replies: 5
    Last Post: 09-29-2002, 12:20 PM