3

I'm trying to write a simple file input and output program called employee list. It creates the list and stores the names and the salaries of the employees entered in a list.

The first problem I have is, anytime I run the code, it just replaces the list instead of adding to it.

My second problem is printing from the file to the console, It just prints a negative integer.

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>

int main() {

  std::string First_Name;
  std::string Last_Name;
  std::string NAMES;
  int SALARY;
  int Employee_Salary;

  std::fstream Employee_List("employee list.txt",
                             std::ios::in | std::ios::out | std::ios::app);

  std::cout << std::left << std::endl;

  if (!Employee_List) {

    std::cout << "File not Found" << std::endl;
    return -1;
  } else {

    Employee_List << "*************Employee Names and Salary***************"
                  << std::endl;
    Employee_List << "Names" << std::setw(25) << "Salary" << std::endl;

    std::cout << "Please Enter First Name" << std::endl;
    std::cin >> First_Name;

    std::cout << "Please Enter Last Name" << std::endl;
    std::cin >> Last_Name;

    std::cout << "Please Enter Salary" << std::endl;
    std::cin >> SALARY;

    std::cout << std::endl << std::endl << std::endl;
    Employee_List << First_Name << " " << Last_Name << "," << std::setw(18)
                  << SALARY << std::endl;

    Employee_List.ignore(255, '\n');

    std::getline(Employee_List, NAMES, ',');
    Employee_List >> Employee_Salary;
    std::cout << std::setw(20) << NAMES << Employee_Salary << std::endl;

    while (!Employee_List.eof()) {

      std::cout << std::setw(20) << NAMES << Employee_Salary << std::endl;

      std::getline(Employee_List, NAMES, ',');
      Employee_List >> Employee_Salary;
    }
  }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    See [why `eof` in a loop condition is wrong](https://stackoverflow.com/q/5605125/9254539). – eesiraed Jul 16 '19 at 21:54
  • 6
    Why are you trying to read from the file and write to the file **at the same time**? Open the file for output first, write to the file, then when you have finished writing, close the file, reopen the file for input and only then start reading from it. – john Jul 16 '19 at 21:56
  • 2
    it is rare that you want to open a text file for reading and writing because text data is almost never the same size for each record making it extremely hard to edit a record without either leaving crap in the file or accidentally overwriting the next record. It is iorders of magnitude easier to read the file, edit in memory, then overwrite the file with the edited copy in memory. – user4581301 Jul 16 '19 at 21:57
  • You don't need the `else { }`. Remove it to save some indentation! – Mateen Ulhaq Jul 16 '19 at 22:00
  • You are already using the `string` container, so you might as well create a `struct` holding the data for each person (e.g. name, salary, etc..) and then create a `vector` of `struct` allowing you to open the file for reading, read all data into your vector of struct, close the input stream, manipulate the data with your program as needed, and then before you exit, open the file for writing (replacing the input file completely), and write the vector of struct out to your file. – David C. Rankin Jul 17 '19 at 00:17
  • besides, ALL CAPS are reserved for macros (and sometimes constants) – phuclv Jul 17 '19 at 01:55
  • Thanks, all of your insights were really helpful. – dkillingjoke Jul 18 '19 at 04:07

1 Answers1

0

I've had this issue. (1st Issue) What you are currently doing is replacing the file contents every time you write something to it. What you want to be doing is appending to the file. Check out this link:

Append to a File with fstream Instead of Overwriting

That_Coder
  • 56
  • 7