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;
}
}
}