简体   繁体   English

构建成功但不会给出正确的输出

[英]Build Succeeding but won't give correct output

#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>

using namespace stdd;

struct em
{
   int hours;
   double payRate;
};

int main()
{
    vector<em> emp;
    int numEmployees;
    int index;
    cout << "How Many Employees Do You Have?" << endl;
    cin >> numEmployees;
    cout << "Enter the hours worked by " << numEmployees;
    cout << "employees and their hourly rates.\n";
    for (index = 0; index < numEmployees; index++)
    {
        int tempHours;
        double tempRate;
        em emp_pay;
        cout << "Hours worked by employee #" << (index + 1);
        cout << ": ";
        cin >> tempHours;
        emp_pay.payRate = tempRate;
        cout << "Hourly pay rate for employee #";
        cout << (index + 1) << ": ";
        cin >> tempRate;
        emp_pay.payRate = tempRate;
        emp.push_back(emp_pay);
    }
    return 0;
}

this is my code and my output looks as follows:这是我的代码,我的输出如下所示:

How Many Employees Do You Have?
3
Enter the hours worked by 3employees and their hourly rates.
Hours worked by employee #1: 1
Hourly pay rate for employee #1: 10
Hours worked by employee #2: 1
Hourly pay rate for employee #2: 10
Hours worked by employee #3: 1
Hourly pay rate for employee #3: 10
Program ended with exit code: 0.

how do i get it to display my results?我如何让它显示我的结果? First time working with structs btw顺便说一句,第一次使用结构

In your code, you never print out the contents of emp which, I assume is what you are trying to acheive.在您的代码中,您永远不会打印出emp的内容,我认为这是您想要实现的。

To print out the contents of emp , you must iterate through the vector with the following:要打印出emp的内容,您必须使用以下内容遍历vector

for (int i = 0; i < emp.size(); i++) {
  cout << "Employee #" << i << "\tHours: " << emp[i].hours
  << "\tPay rate: " << emp[i].payRate << endl;
}

This snippet would print out the data in the following format:此代码段将按以下格式打印数据:

Employee #{{ employeeNumber }}     Hours: {{ hours }}     Pay rate: {{ payRate }}

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM