我使用指针显示用户输入的数据,但是当我尝试显示用户输入的数据时,字符串不会显示在案例3上。相反,空白会显示。我将字符串更改为字符并且有效。只是想知道为什么字符会显示,但字符串不会显示?
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct employeeRec
{
string employee_id;
string last_name;
string first_name;
float hrs_worked;
float pay_rate;
float tax_rate;
};
int main()
{
bool again = true;
int recorded = 0, remaining = 20;
while (again)
{
const int max = 20;
int written, option;
employeeRec *p, records[max];
p = &records[0];
cout << "\n1 - Enter new employee(s)\n"
<< "2 - Find one employee record\n"
<< "3 - Display all employee records\n"
<< "4 - Quit\n\n";
cout << "Enter an option: ";
cin >> option;
switch (option)
{
case 1:
{
cout << "\nHow many records do you wish to enter? (Max total of 20 records): ";
cin >> written;
cin.ignore();
if (written < 0)
{
written = written * -1;
}
for (int n = 0; n < written; n++)
{
cout << "\nRecord #" << n << "\n";
cout << "1 of 6 - Enter Employee ID: ";
getline(cin, p -> employee_id);
cout << "\n2 of 6 - Enter Employee Last Name: ";
getline(cin, p -> last_name);
cout << "\n3 of 6 - Enter Employee First Name: ";
getline(cin, p -> first_name);
cout << "\n4 of 6 - Enter Hours Worked: ";
cin >> p -> hrs_worked;
cout << "\n5 of 6 - Enter Pay Rate: ";
cin >> p -> pay_rate;
cout << "\n5 of 6 - Enter Pay Rate: ";
cin >>p -> tax_rate;
cin.ignore();
cout << "\nEmployee Recorded\n";
p++;
}
recorded = recorded + written;
remaining = remaining - recorded;
break;
}
case 2:
{
cout << "\nFind an employee option";
break;
}
case 3:
{
for (int n = 0; n < written; n++)
{
cout << "\n\nEmployee ID: " << p -> employee_id << "\n";
cout << "Last Name: " << p -> last_name << "\tFirst Name: " << p -> first_name << "\n";
cout << "Hours Worked: " << p -> hrs_worked << "\tPayrate: " << p -> pay_rate << "\tTax Rate: " << p -> tax_rate << "\n\n";
p++;
}
break;
}
case 4:
{
cout << "\nQuitting...";
again = false;
break;
}
default:
{
cout << "Not a valid option. Enter a valid option.\n";
break;
}
}
}
}