类和数组
我正在开发一个课程,该课程将获取员工 ID 并付款,然后显示它。 出现的问题是它没有标记任何错误,但它完全跳过了我应该输入数据的代码。
这就是我的代码的样子。
#include <iostream>
#include <iomanip>
using namespace std;
class employee
{
private:
int id;
float compensation;
public:
employee() : id(0), compensation(0)
{}
employee(int num, float pay) : id(num), compensation(pay)
{}
void setid(int i) { id=i; }
void setcomp(float comp) { compensation=comp; }
void displayinfo() { cout << "Id: " << id << endl << "Pay: " << compensation << endl; }
};
int main ( int argc, char* argv)
{
employee i1(1111, 8.25);
i1.displayinfo();
employee i2[3];
for (int i=0; i<3; i++)
{
cout << "Enter Employee ID: ";
cin >> i2[i].setid(num);
cout << "Enter Employee Pay: ";
cin >> i2[i].setcomp(num);
}
for(int i=0; i<3; i++)
{
i2[i].displayinfo();
}
//------------------------------------------------
system("Pause");
return 0;
}
I'm working on a class that will take an employee id and pay in and then display it.
The problem that comes up is it doesn't flag any errors, but it completely skips past code where i'm supposed to input the data.
This is what my code looks like.
#include <iostream>
#include <iomanip>
using namespace std;
class employee
{
private:
int id;
float compensation;
public:
employee() : id(0), compensation(0)
{}
employee(int num, float pay) : id(num), compensation(pay)
{}
void setid(int i) { id=i; }
void setcomp(float comp) { compensation=comp; }
void displayinfo() { cout << "Id: " << id << endl << "Pay: " << compensation << endl; }
};
int main ( int argc, char* argv)
{
employee i1(1111, 8.25);
i1.displayinfo();
employee i2[3];
for (int i=0; i<3; i++)
{
cout << "Enter Employee ID: ";
cin >> i2[i].setid(num);
cout << "Enter Employee Pay: ";
cin >> i2[i].setcomp(num);
}
for(int i=0; i<3; i++)
{
i2[i].displayinfo();
}
//------------------------------------------------
system("Pause");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这段代码甚至不应该编译。问题是你的循环:
你至少需要将其更改为:
注意:你的示例代码无法编译:
第 33 行 & 35 是我在第一个代码块中指示的行。
编辑:
进行指示的更改后,我得到以下输出:
另外,避免使用
system
函数。您可以通过执行以下操作来完成相同的任务,而无需生成另一个进程(system
会导致创建一个单独的进程):计算<< “按 [ENTER] 继续...”<<结束;
cin.get();
This code shouldn't even compile. The problem is your loop:
You'd need to, at a minimum, change this to:
Note: your sample code does not compile:
With lines 33 & 35 being the lines I indicated in the first code block.
Edit:
After making the indicated change, I get this output:
Also, avoid the
system
function. You can accomplish the same without spawning another process (system
results in a separate process being created) by doing:cout << "Press [ENTER] to continue..." << endl;
cin.get();
您正在编译的代码与您显示的不同。此代码将给出编译错误,因为setid返回void并且num尚未声明。
You are compiling different code than you show. This code will give a compile error, because setid returns void and num has not been declared.