C++ 数组结构

标签 c++ arrays data-structures

我正在阅读我书中关于结构的章节,这让我重新修改了一个我已经编写的程序,但这次使用了我以前从未使用过的结构;但是,完成程序后,有一个问题我不明白。程序的输出只显示一次。它在一个 for 循环中,但即使它要求我输入我的信息三次,它也只输出第一个信息。

我可能只是不了解结构中数组的工作原理。 我的问题的一个例子如下。 我在以下循环中有我的输出

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

大小为 3,这意味着我将输出打印 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 如果让你重新修改这个程序,你会用什么来简化它。询问以便我可以继续重新修改,并学习更高级的东西。 vector ,指针?提前致谢。

最佳答案

您有一个包含 3 个索引的数组,但您的循环最多只能使用 2 个索引。将您的 for 循环更改为此。

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 Link

关于C++ 数组结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42712085/

相关文章:

algorithm - 线段树中的数据映射和惰性传播

C++ LoadLibrary 不工作

android - 如何在 C++ 中通过 OpenGLES 在 android 中显示位图?

arrays - 静态表中的动态表名与数组列

c++ - 在 MIPS 上调用两次的子程序

python - 是否可以限制仅使用不同类中的 setter/modifier 方法?

c++ - boost 多边形序列化 : Ring

c++ - 嵌套 Boost.Assignment `map_list_of`

python - 在数组 Python 中存储多个值

javascript - 为前端组件实现树形数据结构