【发布时间】: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 < size(因为 3 个元素使用索引 0 1 2).. 无论如何,如果您按下一个键,您的代码将输出超过 1 行因为system("pause"); -
@Android400 所以你建议总是使用向量?我将开始阅读有关向量的内容。概念和数组类似吗?
标签: c++ arrays data-structures