我不太明白为什么下面的程序会跳过“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;
}