简体   繁体   English

C ++数组结构

[英]C++ array structures

I was reading the chapter on structures in my book, and it got me re-modifying a program I already made, but this time using structures which I have never used before; 我正在阅读本书中关于结构的章节,它让我重新修改了我已经制作的程序,但这次使用了我以前从未使用过的结构; however, after finishing the program, there's one issue I'm not understanding. 然而,在完成该计划后,有一个我不理解的问题。 The output of the program only displays once. 程序的输出只显示一次。 It's in a for loop, and yet even though it asks me to input my information three times, it only outputs the first information. 它处于for循环中,但即使它要求我输入三次信息,它也只输出第一个信息。

I'm probably just not understanding how arrays in structures work. 我可能只是不了解结构中的数组是如何工作的。 An example of my issue is the following. 我的问题的一个例子如下。 I have my output on the following loop 我在以下循环中输出了输出

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

The size is 3, which would mean I'll get the output printed three times; 大小为3,这意味着我将打印输出三次; however the answer I'm getting is the same as if I was asking for the following. 但是我得到的答案就像我要求的一样。

Listofnames[0].F_name

When what I actually want is 当我真正想要的是

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

However, I don't want to have to write it three times, I did to test it and it actually worked, but is that the only way to do it? 但是,我不想写三次,我做了测试它实际上有效,但这是唯一的方法吗? Or did I miss something in my program? 或者我在程序中遗漏了什么?

#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");
    }
}

Ps If you had to re modify this program again, what would you use to simplify it. Ps如果你不得不重新修改这个程序,你会用什么来简化它。 Asking so I can keep re-modifying, and learn more advanced stuff. 要求我可以继续重新修改,并学习更高级的东西。 Vectors, pointers? 矢量,指针? Thanks in advance. 提前致谢。

You have an array with 3 indexes but your loop is only going upto 2 indexes. 你有一个包含3个索引的数组,但你的循环只能达到2个索引。 Change your for loop to this. 将for循环更改为此。

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

Now, this loop will print the all the indexes. 现在,这个循环将打印所有索引。

Instead of using a static value you can also use this. 您也可以使用它来代替使用静态值。

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 Link Ideone Link

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM