cvandal
cvandal

Reputation: 794

C++ cin.getline appears to be skipped

I can't quite figure out why my program below is skipping over "cin.getline(staffMember, 100);". If I add a delimiter like 'q' for instance, it works as expected. I'm not sure why it's acting as if a new line is being entered automatically. Could somebody please explain to me why this is happening?

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream> // Allow use of the ifstream and ofstream statements
#include <cstdlib> // Allow use of the exit statement

using namespace std;

ifstream inStream;
ofstream outStream;

void showMenu();
void addStaffMember();

void showMenu()
{
    int choice;

    do
    {
        cout 
            << endl
            << "Press 1 to Add a New Staff Member.\n"
            << "Press 2 to Display a Staff Member.\n"
            << "Press 3 to Delete a Staff Member.\n"
            << "Press 4 to Display a Report of All Staff Members.\n"
            << "Press 5 to Exit.\n"
            << endl
            << "Please select an option between 1 and 5: ";

        cin >> choice;

        switch(choice)
        {
            case 1:
                addStaffMember();

                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
            default:
                cout << "You did not select an option between 1 and 5. Please try again.\n";
        }
    } while (choice != 5);
}

void addStaffMember()
{
    char staffMember[100];

    cout << "Full Name: ";

    cin.getline(staffMember, 100);

    outStream.open("staffMembers.txt", ios::app);
    if (outStream.fail())
    {
        cout << "Unable to open staffMembers.txt.\n";
        exit(1);
    }

    outStream << endl << staffMember;

    outStream.close();
}

int main()
{
    showMenu();

    return 0;
}

Upvotes: 1

Views: 246

Answers (4)

jhtong
jhtong

Reputation: 1589

cin is mixed with getline() - try not to mix both in the same code.

Try using this instead?

char aa[100];
// After using cin 
std::cin.ignore(1);
cin.getline(aa, 100);
//.... 

Upvotes: 0

Saurabh Mishra
Saurabh Mishra

Reputation: 433

Use

scanf("%d\n", &choice);

OR you can use a dummy getchar() after cin>>choice;

Right now, the \n is skipped, as explained in few answers.

Upvotes: 0

Vinayak Garg
Vinayak Garg

Reputation: 6606

When doing cin >> choice; the newline is left by cin. So when you do getline next, it reads up to this newline and returns empty (or whitespace) string.

Upvotes: 0

Joseph Mansfield
Joseph Mansfield

Reputation: 110648

When the user enters a choice, they type a number and then press enter. This puts that input including a \n character into the input stream. When you do cin >> choice, characters will be extracted until the \n is found and then those characters will be interpreted as an int. However, the \n is still in the stream.

Later, when you do cin.getline(staffMember, 100), it reads up to the \n and appears as though you entered a new line without actually typing anything.

To get around this, extract up to the next new line by using ignore:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

This will extract everything up until and including the next \n character and discard it. So in fact, this will even handle when the user inputs something like 1banana. The 1 will be extracted by cin >> choice and then the rest of the line will be ignored.

Upvotes: 4

Related Questions