以下のプログラムが「cin.getline(staffMember, 100);」をスキップする理由がよくわかりません。たとえば、「q」のような区切り記号を追加すると、期待どおりに機能します。改行が自動的に入力されているかのように動作する理由がわかりません。誰かが私になぜこれが起こっているのか説明してもらえますか?
#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;
}