Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8811445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:22:01+00:00 2026-06-14T03:22:01+00:00

I’m writing a program in C++ for school, it’s coming along, but I’m having

  • 0

I’m writing a program in C++ for school, it’s coming along, but I’m having trouble in this specific area.

I need to call a function from a loop in another function. I need to read 5 lines and put each collection of numbers read into a double value. Right now I’m just trying to make sure that I can read through the file correctly. Currently, whenever I run the program it goes through the loop and prints the info five times, but it seems to only print the numbers from the last line five times.

What in my code is making it so that my program only works with the last line of my input file?

Here’s my info:

Input File that needs to be read:

1121  15.12  40                                                                                     

9876   9.50  47

3333  22.00  35

2121   5.45  43

9999  10.00  25

Code that I’m working with:

 double process_employee(double& employeeNumber, double& employeeRate, double& employeeHours)

 {

     ifstream employeeInputFile;

     employeeInputFile.open("employee input file.txt");

     if(employeeInputFile.fail())
     {
         cout << "Sorry, file could not be opened..." << endl;

        system("pause");
         exit(1);
     }

     //For some reason, this is only printing the data from the last line of my file 5 times
     while (!employeeInputFile.eof())  
     {
         employeeInputFile >> employeeNumber >> employeeRate >> employeeHours;
     }

}

void process_payroll()
{   
     double employeeNumber = 1.0;
     double employeeRate = 1.0;
     double employeeHours = 1.0;

     cout << "Employee Payroll" << endl << endl;
     cout << "Employee  Hours   Rate    Gross   Net Fed State   Soc Sec" 
          << endl;

     //caling process_employee 5 times because there are 5 lines in my input file
     for(int i = 1; i <= 5; i++)
     {
         process_employee(employeeNumber, employeeRate, employeeHours);

         cout << "Employee #: " << employeeNumber << " Rate: " << employeeRate << " Hours: " 
         << employeeHours << endl;
     }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:22:02+00:00Added an answer on June 14, 2026 at 3:22 am

    You keep overwriting your variables:

    while (!employeeInputFile.eof())  
    {
       employeeInputFile >> employeeNumber >> employeeRate >> employeeHours;
    }
    

    You need to save them in an intermediate like:

    std::vector<EmployeeStructure> someVector;
    
    while (!employeeInputFile.eof())  
    {
       employeeInputFile >> employeeNumber >> employeeRate >> employeeHours;
       someVector.push_back(EmpoyeeStructure(employeeNumber, employeeRate, employeeHours));
    }
    

    Then, pass that Vector around and print the information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I need to clean up various Word 'smart' characters in user input, including but
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.