类和数组

发布于 2024-11-09 11:26:55 字数 1022 浏览 0 评论 0原文

我正在开发一个课程,该课程将获取员工 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

第几種人 2024-11-16 11:26:55

这段代码甚至不应该编译。问题是你的循环:

employee i2[3];
for (int i=0; i<3; i++)
{
    cout << "Enter Employee ID: ";
    cin >> i2[i].setid(num);             // Reading into a void return value.
    cout << "Enter Employee Pay: ";
    cin >> i2[i].setcomp(num);             // Reading into a void return value.
}

你至少需要将其更改为:

employee i2[3];
for (int i=0; i<3; i++)
{
    int num; float pay;
    cout << "Enter Employee ID: ";
    cin >> num;
    i2[i].setid(num);
    cout << "Enter Employee Pay: ";
    cin >> pay;
    i2[i].setcomp(pay);
}

注意:你的示例代码无法编译:

c:\users\nate\documents\visual studio 2010\projects\employeetest\employeetest\employeetest.cpp(33):错误 C2065:“num”:未声明的标识符
1>c:\users\nate\documents\visual studio 2010\projects\employeetest\employeetest\employeetest.cpp(35): 错误 C2065: 'num' : 未声明的标识符

第 33 行 & 35 是我在第一个代码块中指示的行。

编辑:
进行指示的更改后,我得到以下输出:

Id: 1111
Pay: 8.25
Enter Employee ID: 1
Enter Employee Pay: 1234.5
Enter Employee ID: 3
Enter Employee Pay: 5678.9
Enter Employee ID: 4
Enter Employee Pay: 123
Id: 1
Pay: 1234.5
Id: 3
Pay: 5678.9
Id: 4
Pay: 123
Press any key to continue . . .

另外,避免使用 system 函数。您可以通过执行以下操作来完成相同的任务,而无需生成另一个进程(system 会导致创建一个单独的进程):
计算<< “按 [ENTER] 继续...”<<结束;
cin.get();

This code shouldn't even compile. The problem is your loop:

employee i2[3];
for (int i=0; i<3; i++)
{
    cout << "Enter Employee ID: ";
    cin >> i2[i].setid(num);             // Reading into a void return value.
    cout << "Enter Employee Pay: ";
    cin >> i2[i].setcomp(num);             // Reading into a void return value.
}

You'd need to, at a minimum, change this to:

employee i2[3];
for (int i=0; i<3; i++)
{
    int num; float pay;
    cout << "Enter Employee ID: ";
    cin >> num;
    i2[i].setid(num);
    cout << "Enter Employee Pay: ";
    cin >> pay;
    i2[i].setcomp(pay);
}

Note: your sample code does not compile:

c:\users\nate\documents\visual studio 2010\projects\employeetest\employeetest\employeetest.cpp(33): error C2065: 'num' : undeclared identifier
1>c:\users\nate\documents\visual studio 2010\projects\employeetest\employeetest\employeetest.cpp(35): error C2065: 'num' : undeclared identifier

With lines 33 & 35 being the lines I indicated in the first code block.

Edit:
After making the indicated change, I get this output:

Id: 1111
Pay: 8.25
Enter Employee ID: 1
Enter Employee Pay: 1234.5
Enter Employee ID: 3
Enter Employee Pay: 5678.9
Enter Employee ID: 4
Enter Employee Pay: 123
Id: 1
Pay: 1234.5
Id: 3
Pay: 5678.9
Id: 4
Pay: 123
Press any key to continue . . .

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();

通知家属抬走 2024-11-16 11:26:55
cin >> i2[i].setid(num);

您正在编译的代码与您显示的不同。此代码将给出编译错误,因为setid返回void并且num尚未声明。

cin >> i2[i].setid(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文