C++ 使用指针显示结构中的字符串?

C++ 使用指针显示结构中的字符串?,c++,C++,我使用指针来显示用户输入的数据,但是当我试图显示用户输入的数据时,字符串不会显示在案例3中。相反,空格可以。我把字符串改成了chars,效果很好。只是想知道为什么会显示字符,而不会显示字符串 #include <iostream> #include <iomanip> #include <string> using namespace std; struct employeeRec { string employee_id; string last_nam

我使用指针来显示用户输入的数据,但是当我试图显示用户输入的数据时,字符串不会显示在案例3中。相反,空格可以。我把字符串改成了chars,效果很好。只是想知道为什么会显示字符,而不会显示字符串

#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;
        }
    }
}
}
#包括
#包括
#包括
使用名称空间std;
结构雇员
{
字符串employee_id;
字符串last_name;
字符串名;
你工作的时间;
浮动工资率;
浮动税率;
};
int main()
{
布尔再次=真;
记录的整数=0,剩余=20;
同时(再次)
{
常数int max=20;
int-writed,选项;
employeeRec*p,记录[max];
p=&记录[0];
关税->税率;
cin.ignore();

可能无法执行
cout.flush()
在输出行之后?也尝试过,但没有效果。将变量声明移出
while
循环。如前所述,从循环的一次迭代到下一次迭代,您不携带任何状态。例如,在选项3中,
writed
是一个未初始化的变量。我从循环中取出记录,现在显示字符串。而谢谢你的解释!也许做一个
cout.flush()
在输出行之后?也尝试过,但没有效果。将变量声明移出
while
循环。如前所述,从循环的一次迭代到下一次迭代,您不携带任何状态。例如,在选项3中,
writed
是一个未初始化的变量。我从循环中取出记录,现在显示字符串。而谢谢你的解释!