C 阵列结构

C++ array structures

本文关键字:结构 阵列      更新时间:2023-10-16

我正在阅读书中的结构,这使我重新调整了我已经制定的程序,但是这次使用了我从未使用过的结构;但是,完成该程序后,我不了解一个问题。程序的输出仅显示一次。它是为了循环,但是即使它要求我三遍输入我的信息,但它仅输出第一个信息。

我可能只是不了解结构中的阵列如何工作。我问题的一个例子是以下内容。我在以下循环中的输出

for(int counter = 0; counter <size; counter++)

尺寸为3,这意味着我将打印三次输出;但是,我得到的答案就像我要以下内容一样。

Listofnames[0].F_name

当我真正想要的是

Listofnames[0].F_name Listofnames[1].F_name Listofnames[2].F_name

但是,我不想写三遍,我确实对它进行了测试,但实际上是有效的,但是那是唯一的方法吗?还是我错过了程序中的某些内容?

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
    struct Names
    {
        string F_name;          //Creating structure called Names.
        string L_name;
        char Mi;
    };
    struct Payrate
    {
        double rate;
    double hoursworked; //Creating structure called Payrate.
    double gross; 
    double net;
};
int main()
{
    double stateTax = 0, federalTax = 0, unionFees = 0, timeHalf = 1.5;         //Initializing variables.
    const int size = 2;         //Array size.
    Payrate employee[size];             //Structure variables
    Names Listofnames[size];
    for (int counter = 0; counter < size; counter++)            //Initializing for loop.
    {
        cout << "What's your first name?: " << endl;
        cin >> Listofnames[counter].F_name;
        cout << "What's your last name?: " << endl;                     //Displaying names, and hours worked, rate.
        cin >> Listofnames[counter].L_name;
        cout << "What is your middle initial?: " << endl;
        cin >> Listofnames[counter].Mi;
        cout << "How many hours did you work? Please enter a number between 1-50: " << endl;
        cin >> employee[counter].hoursworked;
        cout << "What is your hourly rate? Please enter a number between 1-50: " << endl;
        cin >> employee[counter].rate;
        if (employee[counter].hoursworked < 0 || employee[counter].hoursworked >50)                 //Initializing conditional statements.
        {
            cout << "Sorry you entered a erong entry. Pc shutting off " << endl;                        //Displays what happens is user inputs a number under 0 or over 50.
        }
        if (employee[counter].rate < 0 || employee[counter].rate > 50)                                              //Initializing conditional statements.
        {       
            cout << "Sorry you entered a erong entry. Pc shutting off " << endl;                                //Displays what happens is user inputs a number under 0 or over 50.         
        }
        if (employee[counter].hoursworked <= 40)                                                                                //Initializing conditional statements.
        {                                                                                                       
            employee[counter].gross = employee[counter].hoursworked * employee[counter].rate;               //Calculating gross.
        }
        else if (employee[counter].hoursworked > 40)                                                                                //Initializing conditional statements.
        {
            employee[counter].gross = employee[counter].hoursworked * (employee[counter].rate * timeHalf);  //Calculating gross.
        }
        stateTax = employee[counter].gross * 0.06;
        federalTax = employee[counter].gross * 0.12;                                                                            //Calculates all the tax fees, and net.
        unionFees = employee[counter].gross * 0.02;
        employee[counter].net = employee[counter].gross - (stateTax + federalTax + unionFees);
    }
    cout << "FirstN " << "MI " << "LastName " << "t" << "Rate " << "HoursWorked " << "TimeHalf " << "StateTax " << "FederalTax " << "UnionFees " << "Gross " << "  " << "Net " << endl;            //Displays header of output.
    cout << "==================================================================================================================" << endl;
    for (int counter = 0; counter <= size; counter++)
    {
        //Output.
        cout << Listofnames[counter].F_name << "t" << fixed << setprecision(2) << Listofnames[counter].Mi << " " << Listofnames[counter].L_name << "t" << employee[counter].rate << "t" << employee[counter].hoursworked << "t" << setw(7) << timeHalf << "t" << setw(8) << stateTax << setw(12) << federalTax << "t" << unionFees << "t" << employee[counter].gross << "t" << employee[counter].net << endl;
        system("pause");
    }
}

P.S如果您必须重新修改此程序,则将使用什么来简化它。问我可以继续重新修改,并学习更多高级内容。向量,指针?预先感谢。

您有一个带有3个索引的数组,但您的循环仅为2个索引。更改您的循环。

for (int counter = 0; counter <= size; counter++) 

现在,此循环将打印所有索引。

而不是使用静态值,您也可以使用此功能。

for (int counter = 0; counter < sizeof(Listofnames)/sizeof(Listofnames[0]); counter++)
sizeof(Listofnames)/sizeof(Listofnames[0]) This will give you the total size of your array.

IDEONE链接