cin.getline isn't Working and I Can't Figure Out Why

This is a discussion on cin.getline isn't Working and I Can't Figure Out Why within the C++ Programming forums, part of the General Programming Boards category; Okay, so in my continuing quest to make a text adventure and learn C++ in the process, I found in ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    cin.getline isn't Working and I Can't Figure Out Why

    Okay, so in my continuing quest to make a text adventure and learn C++ in the process, I found in the tutorial a nifty way for the user to enter long strings without having the program flip out if there's a space in the string. I wanted to let players enter their name, or have the option of a few pre-set names, so I defined variables and built a switch case like so:

    variable/array
    Code:
    int playername;
    char enteredplayername[256];
    switch case
    Code:
     switch (playername)
        {
            case 1:
            cout << "The earth pony nods. \"Okay then Heavy Hooves, we need to get you sorted out, \n we're all grateful for what you did, but even with that kind of sacrifice, it's going to take us a while to trust you.\" \n Sacrifice? And then you remember... Oh no. No, no, no...\n 1: Your Horn \n 2: Your Wings \n 3: Your Leg (Earth Pony)\n\n";
            break;
            case 2:
            cout << "The earth pony nods. \"Okay then Far Sighted, we need to get you sorted out,  \n we're all grateful for what you did, but even with that kind of sacrifice, it's going to take us a while to trust you.\" \n Sacrifice? And then you remember... Oh no. No, no, no...\n 1: Your Horn \n 2: Your Wings \n 3: Your Leg (Earth Pony)\n\n";
            break;
            case 3:
            cout << "The earth pony nods. \"Okay then Swift Tailed, we need to get you sorted out, \n we're all grateful for what you did, but even with that kind of sacrifice, it's going to take us a while to trust you.\" \n Sacrifice? And then you remember... Oh no. No, no, no...\n 1: Your Horn \n 2: Your Wings \n 3: Your Leg (Earth Pony)\n\n";
            case 4:
            //cout << "Type your character's first name: ";
            //cin >> enteredfirstname;
            //cout << "Type your character's last name: ";
            //cin >> enteredlastname;
            cout<<"Please enter your character's name: ";
            cin.getline ( enteredplayername, 256, '\n' );              
            cout << "The earth pony nods Okay then " << enteredplayername << ", we need to get you sorted out, \n we're all grateful for what you did, but even with that kind of sacrifice, it's going to take us a while to trust you.\" \n Sacrifice? And then you remember... Oh no. No, no, no...\n 1: Your Horn \n 2: Your Wings \n 3: Your Leg (Earth Pony)\n\n";
            break;
        }
    What I used is very close to the example program provided in the tutorial for entering long strings, and that worked fine when I ran it through the compiler, but when I try to run the program it doesn't let you enter anything for case 4 and skips right to printing out the next cout statement with a blank space where enteredplayername should be. I'm having trouble figuring out why and would be really grateful for the advice of more seasoned programmers.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Hint: when you use formatted input with the overloaded operator>> for a null terminated string, the newline sequence from the "enter" is left in the input buffer. You need to ignore that before calling getline.
    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

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,243
    Just a minor, unrelated observation - it appears you left the "break" statement out for "case 3".

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    I strongly suggest you use std::string and std::getline instead of char arrays and std::cin.getline.
    Example:

    std::string s;
    std::getline(s, std::cin);

    This is not prone to programming errors or buffer overflows and allows for easier string manipulation that is safe and easy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    Thank you for your help everyone, I added a cin.ignore() statement before and the problem cleared right up.

    Quote Originally Posted by Elysia View Post
    I strongly suggest you use std::string and std::getline instead of char arrays and std::cin.getline.
    Example:

    std::string s;
    std::getline(s, std::cin);

    This is not prone to programming errors or buffer overflows and allows for easier string manipulation that is safe and easy.
    One question this raises, and I know this makes me a total newbie for asking, but it just hasn't been described in detail in the parts of the tutorial I've read so far, is what exactly is a buffer overflow? I've been seeing the term (or something like it) as an error every time I try to use certain functions without a '()' at the end, but other than that it has not been simply described to me.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    A buffer overflow happens when one writes more bytes to a buffer than there is room.
    Consider having a buffer (ie some local array) with n bytes.
    Then if you write n + 1 bytes or more to that variable, you get a buffer overflow.

    Buffer overflow - Wikipedia, the free encyclopedia
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    Quote Originally Posted by Elysia View Post
    A buffer overflow happens when one writes more bytes to a buffer than there is room.
    Consider having a buffer (ie some local array) with n bytes.
    Then if you write n + 1 bytes or more to that variable, you get a buffer overflow.

    Buffer overflow - Wikipedia, the free encyclopedia
    Oh, now I get it. Thank you for clearing things up!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline function not working
    By juice in forum C++ Programming
    Replies: 5
    Last Post: 05-09-2012, 03:10 PM
  2. Replies: 9
    Last Post: 04-14-2012, 01:59 PM
  3. Replies: 3
    Last Post: 12-13-2011, 06:32 AM
  4. my getline funtion is not working?
    By steals10304 in forum C++ Programming
    Replies: 1
    Last Post: 08-31-2009, 03:26 PM
  5. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 10:46 PM

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