【问题标题】:C++ array structuresC++ 数组结构
【发布时间】:2017-03-10 06:52:16
【问题描述】:

我正在阅读我书中关于结构的章节,它让我重新修改了我已经制作的程序,但这次使用了我以前从未使用过的结构;但是,完成程序后,有一个问题我不明白。程序的输出只显示一次。它在一个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 如果你不得不重新修改这个程序,你会用什么来简化它。要求我可以继续重新修改,并学习更高级的东西。向量,指针?提前致谢。

【问题讨论】:

  • 你能提供失败的输入吗?由于您未验证您看到的输入问题,因此很可能是由于您的解析行为不当造成的。
  • 我刚刚在我的机器上测试了代码,它打印了两次输出 - 因为这里的大小设置为 2 const int size = 2; 而不是你说的 3。打电话给system("pause")后你也按下按钮了吗?否则程序将在仅输出第一行后暂停。
  • 不要使用数组,你应该几乎总是在 c++ 中使用 std::vector。
  • 如果您的数组大小为 3,您应该运行循环 i &lt; size(因为 3 个元素使用索引 0 1 2).. 无论如何,如果您按下一个键,您的代码将输出超过 1 行因为system("pause");
  • @Android400 所以你建议总是使用向量?我将开始阅读有关向量的内容。概念和数组类似吗?

标签: c++ arrays data-structures


【解决方案1】:

您有一个包含 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

【讨论】:

  • 对不起,编辑的程序,我错误地把我试图做的事情放到了修复它。该程序现在正是我所拥有的,仍然只显示一次。
  • 否则您可以将大小增加到 +1,而不是在 for 参数中将条件操作更改为邪恶的 &lt;=
  • @ReMaKe 它对我来说工作正常。我使用了相同的代码。检查此链接。 ideone.com/uGeQEQ
  • @Ayush 是的,它现在正在工作。我评论了另一个人的评论,但我会在这里说同样的话。这是最愚蠢的事情。我不得不去建造,然后按干净的解决方案,它就开始工作了。我猜它从未更新程序。有谁知道为什么它在视觉工作室中这样做?当我过去在 xcode 上编码时,这从未发生在我身上。它让您认为您的代码完全错误,而实际上它是正确的。只是按运行,有时不会更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多