我觉得我放入结构中的输入代码可以压缩,关于如何在保持代码简短的同时保持数据个性化的任何建议?
I feel like the input code I put into my struct can be condensed, any suggestions on how to keep the data individual while keeping the code short?
我一直在试图弄清楚如何将信息输入到我的结构中,但我想压缩代码。
#include <iostream>
using namespace std;
struct Employee //The whole program is going to revolve around this struct
{
char first[10], last[10];
float Hours_Worked, Hourly_Rate, Fed_Tax_Rate, St_Tax_Rate;
}Kobe, Lebron, Larry, Michael; //Struct declarations
这里的代码就是我所说的代码。我的首选设计是使用循环 4 次的 for 循环,但我需要个人信息。
int main()
{
Employee Kobe;
cout << "First name: ";
cin >> Kobe.first;
cout << "Last name: ";
cin >> Kobe.last;
cout << "Hours worked: ";
cin >> Kobe.Hours_Worked;
cout << "Federal Tax Rate: ";
cin >> Kobe.Fed_Tax_Rate;
cout << "State Tax Rate: ";
cin >> Kobe.St_Tax_Rate;
Employee Lebron;
cout << "First name: ";
cin >> Lebron.first;
cout << "Last name: ";
cin >> Lebron.last;
cout << "Hours worked: ";
cin >> Lebron.Hours_Worked;
cout << "Federal Tax Rate: ";
cin >> Lebron.Fed_Tax_Rate;
cout << "State Tax Rate: ";
cin >> Lebron.St_Tax_Rate;
Employee Larry;
cout << "First name: ";
cin >> Larry.first;
cout << "Last name: ";
cin >> Larry.last;
cout << "Hours worked: ";
cin >> Larry.Hours_Worked;
cout << "Federal Tax Rate: ";
cin >> Larry.Fed_Tax_Rate;
cout << "State Tax Rate: ";
cin >> Larry.St_Tax_Rate;
Employee Michael;
cout << "First name: ";
cin >> Michael.first;
cout << "Last name: ";
cin >> Michael.last;
cout << "Hours worked: ";
cin >> Michael.Hours_Worked;
cout << "Federal Tax Rate: ";
cin >> Michael.Fed_Tax_Rate;
cout << "State Tax Rate: ";
cin >> Michael.St_Tax_Rate;
return 0;
}
使用输入方法定义一个Employee
以获取输入
struct Employee
{
char first[10], last[10];
float Hours_Worked, Hourly_Rate, Fed_Tax_Rate, St_Tax_Rate;
bool getinput(std::istream & in,
std::ostream & out);
};
然后实现此方法
bool Employee::getinput(std::istream & in,
std::ostream & out);
{
out << "First name: ";
in >> first;
out << "Last name: ";
in >> last;
out << "Hours worked: ";
in >> Hours_Worked;
out << "Federal Tax Rate: ";
in >> Fed_Tax_Rate;
out << "State Tax Rate: ";
in >> St_Tax_Rate;
return in.good(); //always good to know if the input succeeded
}
然后调用该方法
Employee Kobe;
Kobe.getinput(cin, cout);
Employee Lebron;
Lebron.getinput(cin, cout);
Employee Larry;
Larry.getinput(cin, cout);
Employee Michael;
Michael.getinput(cin, cout);
cin
和cout
以抽象形式传入,以便您可以在不同的输入流(例如网络套接字(上调用getinput
。
使用Employee
数组和for
循环来避免编写重复的代码:
#include <iostream>
using namespace std;
struct Employee
{
char first[10];
char last[10];
float Hours_Worked;
float Hourly_Rate;
float Fed_Tax_Rate;
float St_Tax_Rate;
};
int main()
{
Employee employees[4];
for (auto &e : employees) {
cout << "First name: ";
cin >> e.first;
cout << "Last name: ";
cin >> e.last;
cout << "Hours worked: ";
cin >> e.Hours_Worked;
cout << "Hourly rate: ";
cin >> e.Hourly_Rate;
cout << "Federal Tax Rate: ";
cin >> e.Fed_Tax_Rate;
cout << "State Tax Rate: ";
cin >> e.St_Tax_Rate;
}
for (auto const &e : employees)
cout << e.first << ' ' << e.last << 'n' << e.Hours_Worked << " / "
<< e.Hourly_Rate << " / " << e.Fed_Tax_Rate << " / " << e.St_Tax_Rate << "nn";
}
如果您(还(不熟悉基于范围的for
循环,您当然可以使用传统的循环,例如
for(size_t i{}; i < sizeof(employees)/sizeof(*employees); ++i)
// and access eployees[i] in the loops body.
相关文章:
- 代码编译没有任何输出,入门程序
- 可能我知道为什么这段代码没有给出任何输出吗?
- 执行此代码时,它不显示任何输出.为什么?
- 是给定代码中的任何更改,以便我可以为问题提供正确的输出
- 我的代码中是否有任何类型的错误,因为它没有给出正确的输出
- 我觉得我放入结构中的输入代码可以压缩,关于如何在保持代码简短的同时保持数据个性化的任何建议?
- 我最近更改了编译器路径以运行 c++ 代码,但现在我无法运行任何 python 代码。我该如何解决这个问题?
- 为什么 C++ 中的分号在插入代码开头时不显示任何错误?
- 为什么我的代码在终端中没有输出任何内容.开始新行没有错误
- 为什么这段代码非常慢?任何与缓存行为有关的事情?
- 如果代码中没有连接任何插槽,是否有理由发出Qt信号?
- 这会对代码产生任何影响吗?
- 为什么当从面向任何 CPU 的 C# 项目调用此代码时,此代码会引发 System.AccessViolationExc
- 为什么<<低;和 cout<<v.begin;单独给出错误,但 cout <<(lower.begin());没有给出任何错误,并使代码易于编译
- 是否有任何编译器标志可以在下面的代码中用于报告有关 UB 的警告?
- 为什么我在链表中插入的代码没有得到任何输出?
- 代码不会处理任何异常
- 如何创建我稍后在代码中定义大小的任何数组?
- 我的递归可以吗?是否有任何破坏代码的示例?
- 我是否需要处理以下代码中的任何错误情况?