Reading from files with ':' between each variable. C++ - c++

Hey so I have this text file:
1:Vegetarian Dishes:Lettuce:1.99
2:Vegetarian Dishes:Tomato:0.99
3:Drinks:Water:0.5
4:Meat Dishes:Burger:4.99
5:Fish Dishes:Fishes:5
6:Drinks:Coka cola:2.5
7:Drinks:Fanta:1.2
8:Drinks:Absolut:12.99
9:Drinks:Wiskey:9.99
10:Vegetarian Dishes:Lemon:0.99
11:Vegetarian Dishes:Green:2.99
12:Meat Dishes:Pizza:4.99
13:Fish Dishes:Fishes2:5.99
14:Drinks:Milk shake:4.99
15:Vegetarian Dishes:Vegan Sandwich:3.99
and i have objects of Dish class, so I want to read the file and add each dish to a Dish object.
Then i have an array of Dishes. Its supposed to store _itemNo:category:description:price.
Below is my code.
The problem is that it doesn't read the file correctly. Do you see any problems?
Code:
Dish::Dish() { }
Dish::Dish(int itemNo, string category, string description,
double price): _itemNo(itemNo), _category(category),
_description(description), _price(price) { }
void Dish::displayDish(void){
cout << setw(15) << left << _itemNo << setw(30) << left << _category
<< setw(45) << left << _description << _price << endl;
}
DishDb::DishDb(): _nElems(0) { }
void DishDb::addDish(int itemNo, string category, string description,
double price){
_menu[_nElems] = Dish(itemNo, category, description, price);
_nElems++;
}
void DishDb::display(){
int i;
cout <<setw(40) << "MENU" << endl << endl;
cout << "Item No" << setw(15) << "Category" << setw(30)
<< "Description" << setw(40)<< "Price" << endl << endl;
for(i = 0; i < _nElems; i++){
_menu[i].displayDish();
}
}
int main(){
/*Select file name for the bills to be stored in*/
string transFilename;
cout << "Enter today's transaction file name: ";
cin >> transFilename;
/*Load the menu*/
/*Adding each dish object into the _nenu array*/
/*Couldn't make this part object oriented*/
DishDb ddb;
string fileName;
int itemNo;
double price;
string description;
string category;
int i;
int numOfDishes = 0;
cout << "Enter file name: ";
cin >> fileName;
ifstream inFile(fileName);
while(i<15){
inFile >> itemNo;
getline(inFile, category, ':');
getline(inFile, description, ':');
inFile >> price;
numOfDishes++;
Dish(itemNo, category, description, price);
ddb.addDish(itemNo, category, description, price);
i++;
}
ddb.display();
inFile.close();
/* while(inFile >> itemNo && getline(inFile, category, ':') &&
getline(inFile, description, ':') && inFile >> price){
numOfDishes++;
Dish(itemNo, category, description, price);
ddb.addDish(itemNo, category, description, price);
}
*/
cout << endl << "Menu was loaded.";
return 0;
}
I've tried 2 ways of reading the file. With the first i was just checking if the file was beging read correctly but its not.

The following code should work for you:
int main() {
std::ifstream file("dishes.txt");
std::string temp, snum, category, description, sprice;
while (std::getline(file, snum, ':') &&
std::getline(file, category, ':') &&
std::getline(file, description, ':') &&
std::getline(file, sprice)) {
// Do whatever you want with:
// * snum (must convert to integer)
// * category
// * description
// * sprice (must convert to float)
}
}

Related

Trouble with arrays and structs from input file

I am working on this problem for a college course and I'm unsure why it isn't working. I thought I had everything setup correctly, but when I try running the program the file won't open on the first attempt at inputting the file name and when I input it again I get an "Exception Thrown" error in the "xmemory" part that I have no idea what any of it means.
The input file that the program is taking data from is just a text file with the following data:
201742 Sponge Bob 82.6
201701 Patrick Star 14.1
201753 Squidward Tentacles 85.43
201744 Sandy Squirrel 75.61
201700 Plankton Plank 100.0
The final output of the program should display the highest and lowest grade with the students first and last name, the average score, and the number of students tested. Any help would be much appreciated.
// Zane Richards
// Lab 10 Q2
// 4/6/2020
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <fstream>
using namespace std;
struct student {
int ID;
string firstName;
string lastName;
float grade;
};
void maxGradeFunc(struct student* s, int size)
{
float maxGrade = 0;
string firstName, lastName;
for (int i = 0; i < size; ++i)
{
if (s[i].grade > maxGrade)
{
maxGrade = s[i].grade;
firstName = s[i].firstName;
lastName = s[i].lastName;
}
}
cout << "Maximum grade is " << maxGrade << endl;
cout << "The name of the student with the grade is: " << firstName << " " << lastName << endl;
}
void minGradeFunc(struct student* s, int size)
{
float minGrade = 999;
string firstName, lastName;
for (int i = 0; i < size; ++i)
{
if (s[i].grade < minGrade)
{
minGrade = s[i].grade;
firstName = s[i].firstName;
lastName = s[i].lastName;
}
}
cout << "Maximum grade is " << minGrade << endl;
cout << "The name of the student with the grade is: " << firstName << " " << lastName << endl;
}
void avgGradeFunc(struct student* s, int size)
{
float sum = 0;
for (int i = 0; i < size; ++i)
{
sum += s[i].grade;
}
float avg = sum / size;
cout << "Avearge grade is " << avg << endl;
cout << "The total number of students are: " << sum << " students" << endl;
}
int main()
{
ifstream inFile;
string fileName = "";
struct student s[5];
int ID;
string firstName, lastName;
float grade;
int i = 0;
cout << "Please enter the input file name: "; //File is named "Student.txt"
cin >> fileName;
while (!inFile.is_open()) {
cout << "Sorry, the file did not open. \nTry again, or type \"cancel\" to quit: ";
cin >> fileName;
if (fileName == "cancel") {
cout << "Cancelling..." << endl;
break;
}
else {
inFile.open(fileName);
}
}
while (inFile.is_open()) {
inFile >> ID;
s[i].ID = ID;
inFile >> firstName;
s[i].firstName = firstName;
inFile >> lastName;
s[i].lastName = lastName;
inFile >> grade;
s[i].grade = grade;
i++;
}
maxGradeFunc(s, 5);
minGradeFunc(s, 5);
avgGradeFunc(s, 5);
return 0;
}
Here is the error code I'm getting: https://i.stack.imgur.com/i35Oq.png
Figured out the issue, deleted the code that allowed for multiple attempts, and changed it to just open the file and that fixed it. Thanks for the help everyone, much appreciated!

How can I read a document and write to it at the same time

struct employee
{
string empid;
string lastn;
string firstn;
float hours;
float payrate;
float taxrate;
};
const int SIZE = 100;
int main()
{
employee eCount[SIZE];
string empF;
int count, option1;
fstream inFile;
inFile.open("personnel1.dat", ios::in | ios::out);
count = 0;
cout << "Enter an Employee ID to Change Values (i.e Cv299): ";
cin >> empF;
cin.ignore();
while (!inFile.eof())
{
count += 1;
inFile >> eCount[count].empid >> eCount[count].lastn >> eCount[count].firstn >> eCount[count].hours >> eCount[count].payrate >> eCount[count].taxrate;
if (empF == eCount[count].empid)
{
do
{
cout << "1. Change Employee's ID (" << eCount[count].empid << ")\n";
cout << "2. Change Employee's Last Name (" << eCount[count].lastn << ")\n";
cout << "3. Change Employee's First Name (" << eCount[count].firstn << ")\n";
cout << "4. Change Employee's Hours Worked (" << eCount[count].hours << ")\n";
cout << "5. Change Employee's Pay Rate (" << eCount[count].payrate << ")\n";
cout << "6. Change Employee's Tax Rate (" << eCount[count].taxrate << ")\n";
cout << "7. Quit!\n\n";
cout << "Enter 1 - 7: ";
cin >> option1;
cin.ignore();
switch (option1)
{
case 1:
{
cout << "What would you like to change the Employee ID to?: ";
getline(cin, eCount[count].empid);
break;
}
case 2:
{
cout << "What would you like to change the Employee's Last Name to?: ";
getline(cin, eCount[count].lastn);
break;
}
case 3:
{
cout << "What would you like to change the Employee's First Name to?: ";
getline(cin, eCount[count].firstn);
break;
}
case 4:
{
cout << "What would you like to change the Employee's Hours Worked to?: ";
cin >> eCount[count].hours;
cin.ignore();
break;
}
case 5:
{
cout << "What would you like to change the Employee's Pay Rate to?: ";
cin >> eCount[count].payrate;
cin.ignore();
break;
}
case 6:
{
cout << "What would you like to change the Employee's Tax Rate to?: ";
cin >> eCount[count].taxrate;
cin.ignore();
break;
}
default:
break;
}
} while (option1 != 7);
}
}
inFile << eCount[count].empid << eCount[count].lastn << eCount[count].firstn << eCount[count].hours << eCount[count].payrate << eCount[count].taxrate;
inFile.close();
}
The program is supposed to read the file and also allow the user to write to it. After inputting a valid employee ID it pops up the menu allowing the user to enter what they want to change. After inputting those changes and closing the file, it doesn't update the file. It still shows the same values from before.
You can do some thing like,
This code copies Line by line from old file to a tempfile. When you find the line which you want to update, change it with your line and copy it to the tempfile. Then finally copy all data from tempfile to file and delete tempfile
#include<iostream>
#include<fstream>
#include<stdlib.h>
using std::cout;
using std::endl;
using std::cin;
using std::ios;
class Employee
{
public:
std::string empname;
std::string empid;
};
int main()
{
std::fstream file("emp"),tempfile("tempemp");
Employee emp;
std::string id;
if(!file.is_open()||!tempfile.is_open())
{
file.open("emp",ios::out); //create a file
tempfile.open("tempemp",ios::out); //create a file
if(!file.is_open()||!tempfile.is_open())
{
cout<<"File Error";
exit(1);
}
else
{
file.close();
tempfile.close();
file.open("emp"); // open file for both read and write
tempfile.open("emp"); // open file for both read and write
if(!file.is_open()||!tempfile.is_open())
{
cout<<"File Error";
exit(1);
}
}
}
for(int i=0;i<3;i++) // taking 3 employee name and id
{
cout<<"Enter Name and id:"<<endl;
cin>>emp.empname;
cin>>emp.empid;
file.write((char*)&emp,sizeof emp);
}
cout<<"Contents:"<<endl; //printing contents
file.seekg(0,ios::beg); //move get pointer to beginning
while(file.peek()!=EOF)
{
file.read((char*)&emp,sizeof emp);
cout<<emp.empname<<endl;
cout<<emp.empid<<endl;
}
file.clear(); //reset file
cout<<"Enter Id to Modify:"<<endl;
cin>>id; // which id to modify
file.seekg(0,ios::beg); //move get pointer to beg
tempfile.seekp(0,ios::beg); //move get pointer to beg
while(file.peek()!=EOF)
{
file.read((char*)&emp,sizeof emp);
if(emp.empid==id) //if emp id in file equal to id then
{ //save to tempfile the name and id of user entered
cout<<"Enter New name and id:"<<endl;
cin>>emp.empname;
cin>>emp.empid;
tempfile.write((char*)&emp,sizeof emp);
}
else // if not equal save the content in file to tempfile
{
tempfile.write((char*)&emp,sizeof emp);
}
}
file.clear(); //reset file
tempfile.seekg(0,ios::beg); //move get pointer pos to beg
file.seekp(0,ios::beg); //move put pointer pos to beg
while(tempfile.peek()!=EOF)
{
tempfile.read((char*)&emp,sizeof emp); //copy contents of tempfile to file
file.write((char*)&emp,sizeof emp);
}
file.clear(); //reset file
tempfile.close(); //close file
remove("tempemp"); //remove tempemp pointed by tempfile
cout<<"Modified Contents:"<<endl; //print modified contents
file.seekg(0,ios::beg); //move get pointer pos to beg
while(file.peek()!=EOF)
{
file.read((char*)&emp,sizeof emp);
cout<<emp.empname<<endl;
cout<<emp.empid<<endl;
}
file.close(); //close file
return 0;
}
i am used name and id only for simplicity
(OR)
you can use like this also,But this code did not use tempfile. it replaces the new content directly in the file.
#include<iostream>
#include<fstream>
#include<stdlib.h>
using std::cout;
using std::endl;
using std::cin;
using std::ios;
class Employee
{
public:
std::string empname;
std::string empid;
};
int main()
{
std::fstream file("emp");
Employee emp;
std::string id;
std::fstream::pos_type pos=0; //for position
if(!file.is_open())
{
file.open("emp",ios::out);
if(!file.is_open())
{
cout<<"File Error";
exit(1);
}
else
{
file.close();
file.open("emp");
if(!file.is_open())
{
cout<<"File Error";
exit(1);
}
}
}
for(int i=0;i<3;i++)
{
cout<<"Enter Name and id:"<<endl;
cin>>emp.empname;
cin>>emp.empid;
file.write((char*)&emp,sizeof emp);
}
cout<<"Contents:"<<endl;
file.seekg(0,ios::beg);
while(file.peek()!=EOF)
{
file.read((char*)&emp,sizeof emp);
cout<<emp.empname<<endl;
cout<<emp.empid<<endl;
}
file.clear();
cout<<"Enter Id to Modify:"<<endl;
cin>>id;
file.seekg(0,ios::beg);
file.seekp(0,ios::beg);
pos=file.tellp(); //get init pos
while(file.peek()!=EOF)
{
pos=file.tellp(); //update pos
file.read((char*)&emp,sizeof emp);
if(emp.empid==id)
{
cout<<"Enter New name and id:"<<endl;
cin>>emp.empname;
cin>>emp.empid;
file.seekp(pos); // change pos
file.write((char*)&emp,sizeof emp); //save modified content
}
}
file.clear();
cout<<"Modified Contents:"<<endl;
file.seekg(0,ios::beg);
while(file.peek()!=EOF)
{
file.read((char*)&emp,sizeof emp);
cout<<emp.empname<<endl;
cout<<emp.empid<<endl;
}
file.close();
return 0;
}
In file streams only tellg changes tellp but it not for other streams for more info see this
are "seekp" & "seekg" interchangeable?

Vector Function program issues

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
void check(ifstream &iFile)
{
if (!iFile.is_open())
{
cout << "Data file not found!" << endl;
system("pause");
exit(1); // exit the program if the file is not found.
}
}
void readIn(ifstream &iFile, vector<string> &fName, vector<string> &lName, vector<string> &jTitle, vector<string> &eID, vector<double> &hoursWorked, vector<double> &wage, vector<int> &deductions, vector<double> &sPay, string sTemp, double dTemp, int iTemp)
{
while (!iFile.eof())
{
iFile >> sTemp;
fName.push_back(sTemp);
iFile >> sTemp;
lName.push_back(sTemp);
iFile.ignore();
getline(iFile, sTemp);
jTitle.push_back(sTemp);
iFile >> sTemp;
eID.push_back(sTemp);
iFile >> dTemp;
hoursWorked.push_back(dTemp);
iFile >> dTemp;
wage.push_back(dTemp);
iFile >> iTemp;
deductions.push_back(iTemp);
iFile >> dTemp;
sPay.push_back(dTemp);
}
cout << "completed" << endl;
}
int main()
{
ifstream iFile;
iFile.open("data.txt");
check(iFile);
vector<string> fName, lName, eID, eStatus, jTitle;
vector<double> nPay, gPay, oPay, oHours;
vector<double> hoursWorked, wage, sPay;
vector<int> deductions;
// temporary names to pass to the vector
string sTemp; // string temp
double dTemp=0; // double temp
int iTemp=0; // integar temp
readIn(iFile, fName, lName, jTitle, eID, hoursWorked, wage, deductions, sPay, sTemp, dTemp, iTemp);
/* while (!iFile.eof())
{
iFile >> sTemp;
fName.push_back(sTemp);
iFile >> sTemp;
lName.push_back(sTemp);
iFile.ignore();
getline(iFile, sTemp);
jTitle.push_back(sTemp);
iFile >> sTemp;
eID.push_back(sTemp);
iFile >> dTemp;
hoursWorked.push_back(dTemp);
iFile >> dTemp;
wage.push_back(dTemp);
iFile >> iTemp;
deductions.push_back(iTemp);
iFile >> dTemp;
sPay.push_back(dTemp);
}*/
int sizeOf = fName.size();
for (int a = 0; a < sizeOf; a++)
{
cout << fName.size() << " FName " << fName[a] << " LName " << lName[a] << " JobTitle " << jTitle[a] << endl;
cout << "EmployeeID " << eID[a] << " Hours Worked " << hoursWorked[a] << " Hourly Wage " << wage[a] << endl;
cout << "Deductions " << deductions[a] << " Salary Pay " << sPay[a] << endl;
}
system("pause");
return 0;
}
I'm running into an issue where my function will not do anything. It will compile, but there is no output. The thing is when I take out the vector sPay from all the parts it works perfectly fine. Any suggestions as to why that one part isn't working? From my limited knowledge it should work perfectly fine, however I can't figure out what would be causing this.
My example text file is
Alan
WakeField
IT GUY
T2034
40
15
1
Hourly
0.00
Your input file does not match your reading code. There are 9 values in the file that you have shown, but your code is attempting to read only 8 values.
When readIn() gets to this code:
iFile >> dTemp;
sPay.push_back(dTemp);
It attempt to read a double but the file has Hourly instead, so the read fails.
So, either remove the Hourly line from the file, or else add a call to iFile >> sTemp to read that line.
Also, the parameters string sTemp, double dTemp, and int iTemp should be declared as local variables instead of as input parameters.
Also, readIn() is not doing any error handling. Your main() code makes an invalid assumption that the vector of first names exactly matches the size of the other vectors, but readIn() does not guarantee that.
And lastly, checking eof() before you have read anything is wrong. The stream's eofbit flag is not updated until a read operation attempts to read past EOF.
You should consider re-writing this code. For example, try something more like this:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
struct Employee
{
std::string fName;
std::string lName;
std::string title;
std::string eID;
double hoursWorked;
double wage;
int deductions;
std::string wageType;
double sPay;
Employee() :
hoursWorked(0), wage(0), deductions(0), sPay(0)
{
}
};
void check(std::ifstream &iFile)
{
if (!iFile.is_open())
{
std::cout << "Data file not found or unable to open!" << std::endl;
std::system("pause");
exit(1); // exit the program.
}
}
void readIn(std::ifstream &iFile, std::vector<Employee> &employees)
{
std::ios_base::iostate oldstate = iFile.exceptions();
iFile.exceptions(std::ifstream::badbit | std::ifstream::failbit);
try
{
do
{
Employee emp;
iFile >> emp.fName;
iFile >> emp.lName;
std::getline(iFile, emp.title);
iFile >> emp.eID;
iFile >> emp.hoursWorked;
iFile >> emp.wage;
iFile >> emp.deductions;
iFile >> emp.wageType;
iFile >> emp.sPay;
employees.push_back(emp);
}
while (!iFile.eof());
}
catch (const std::ios_base::failure &)
{
std::cout << "Data file corrupted!" << std::endl;
std::system("pause");
exit(1); // exit the program.
}
iFile.exceptions(oldstate);
std::cout << "completed" << std::endl;
}
int main()
{
std::ifstream iFile("data.txt");
check(iFile);
std::vector<Employee> employees;
readIn(iFile, employees);
int sizeOf = employees.size();
for (int a = 0; a < sizeOf; a++)
{
std::cout << "FName " << employees[a].fName
<< " LName " << employees[a].lName
<< " JobTitle " << employees[a].title
<< std::endl;
std::cout << "EmployeeID " << employees[a].eID
<< " Hours Worked " << employees[a].hoursWorked
<< " << employees[a].wageType << " Wage " << employees[a].wage
<< std::endl;
std::cout << "Deductions " << employees[a].deductions
<< " Salary Pay " << employees[a].sPay
<< std::endl;
std::cout << std::endl;
}
std::system("pause");
return 0;
}
Alternatively, since your data is line-based, you should use std::getline() to read each line, and then use std::istringstream to parse values:
void readIn(std::ifstream &iFile, std::vector<Employee> &employees)
{
std::string sTemp;
std::ios_base::iostate oldstate = iFile.exceptions();
iFile.exceptions(std::ifstream::badbit | std::ifstream::failbit);
try
{
do
{
Employee emp;
std::getline(iFile, emp.fName);
std::getline(iFile, emp.lName);
std::getline(iFile, emp.title);
std::getline(iFile, emp.eID);
std::getline(iFile, sTemp);
if (!(std::istringstream(sTemp) >> emp.hoursWorked))
iFile.setstate(std::ifstream::failbit);
std::getline(iFile, sTemp);
if (!(std::istringstream(sTemp) >> emp.wage))
iFile.setstate(std::ifstream::failbit);
std::getline(iFile, sTemp);
if (!(std::istringstream(sTemp) >> emp.deductions))
iFile.setstate(std::ifstream::failbit);
std::getline(iFile, emp.wageType);
std::getline(iFile, sTemp);
if (!(std::istringstream(sTemp) >> emp.sPay))
iFile.setstate(std::ifstream::failbit);
employees.push_back(emp);
}
while (!iFile.eof());
}
catch (const std::ios_base::failure &)
{
std::cout << "Data file corrupted!" << std::endl;
std::system("pause");
exit(1); // exit the program if the file is corrupted.
}
iFile.exceptions(oldstate);
std::cout << "completed" << std::endl;
}
I went derp for a moment. I forgot to read in the hourly or salaried before going to salary pay.

How to determine if name and lname is not in file

The program, is basically sussposed to iterate a file containing the elements
Harry Keeling (202)806-4830
Frank James (301)123-3459
Arthur Paul (202)865-9090
Todd Shurn (410)560-8909
Richard Okpala 202 388 410
what my current program is outputting the phonenumber if firstname and lastname is present in file but if its nothow do i output phone number isnt here my current code.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void lookup_name(ifstream&, string&, string&, string&); // prototype
int main()
{
ifstream myfile;
string name, lastname, phonenumber;
char choice;
do
{
myfile.open("infile.txt");
cout << "What is the First name? " << endl;
cin >> name;
cout << "what is your last name?" << endl;
cin >> lastname;
lookup_name(myfile, name, lastname, phonenumber);
cout << "Do you want to lookup another name<Y/N" << endl;
cin >> choice;
} while (choice == 'Y');
return 0;
}
void lookup_name(ifstream& myfile, string& name, string& lastname, string& phonenumber)
{
string name1, name2, fullname, secondname, dummy;
for (int i = 0; i < 5; i++) {
myfile >> name1 >> name2;
fullname = name1 + name2;
secondname = name + lastname;
if (fullname == secondname) {
myfile >> phonenumber;
cout << phonenumber << endl;
myfile.close();
break;
}
else if (fullname != secondname) {
myfile >> dummy;
phonenumber = dummy;
}
Just return whether you found it or not from your lookup function
int lookup_name(ifstream& myfile, string& name, string& lastname, string& phonenumber)
{
string name1, name2, fullname, secondname, dummy;
for (int i = 0; i < 5; i++) {
myfile >> name1 >> name2;
fullname = name1 + name2;
secondname = name + lastname;
if (fullname == secondname) {
myfile >> phonenumber;
cout << phonenumber << endl;
myfile.close();
return 1;
}
else if (fullname != secondname) {
myfile >> dummy;
phonenumber = dummy;
}
}
return 0;
}
Then use the return value
if ( ! lookup_name(myfile, name, lastname, phonenumber) ) {
cout << "Nope, didn't find it!" << endl;
}

Adding user input to a method and putting the results into a file (C++)

Any help would be appreciated. All I'm trying to do is ask for user input, do some calculations and print the results in a file. I thought that my code was correct but when I run my program, I get nothing. Here is my code. Not looking for an answer, just for any tips to lead me in the right direction. Thanks.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Employee{
private:
int id;
int job_class;
int years_service;
int Ed;
float salary;
public:
void getData(ifstream&);
void computation(int job_class, int years_service, int Ed);
void printout(ofstream&);
};
void Employee::getData(ifstream& infile){
infile >> id >> job_class >> years_service >> Ed;
}
void Employee::computation(int job_class, int years_service, int Ed){
int basePay = 800;
float jobresult, Yearresult, Edresult;
if(job_class == 1){
jobresult = .05;
}
if(job_class == 2){
jobresult = .10;
}
if(job_class == 3){
jobresult = .15;
}
if(years_service <= 10){
Yearresult =.05;
}
if(years_service > 10){
Yearresult = .05;
}
if(Ed == 1){
Edresult = .00;
}
if(Ed == 2){
Edresult = .05;
}
if(Ed == 3){
Edresult = .12;
}
if(Ed == 4){
Edresult = .20;
}
salary = basePay + jobresult + Yearresult + Edresult;
//cout << salary;
}
void Employee::printout(ofstream& outfile){
outfile << "ID: " << "Salary " << endl;
outfile << id << salary;
}
int main(){
Employee emp; //created an Employee object
string input;
int id;
int job_class;
int years_service;
int Ed;
int basepay = 800;
cout << "Enter id" << endl;
cin >> id;
cout << "Enter job_class" << endl;
cin >> job_class;
cout << "Enter years of service" << endl;
cin >> years_service;
cout << "Enter education" << endl;
cin >> Ed;
ifstream inFile;
ofstream outFile;
//getline(cin, input);
inFile.open("example.txt");
outFile.open("examplee.txt");
//inFile.open(input);
std::string r = std::to_string(id); //converted id to string
inFile.open(r);
getline(cin, r);
std::string s = std::to_string(years_service);
inFile.open(s);
getline(cin, s);
std::string t = std::to_string(years_service);
inFile.open(t);
getline(cin, t);
std::string u = std::to_string(Ed);
inFile.open(u);
getline(cin, u);
if(inFile.is_open()){
emp.getData(inFile);
inFile.close();
}
outFile.open(r);
if(outFile.is_open()){
emp.computation(job_class, years_service, Ed);
float sal = basepay + job_class + years_service + Ed;
outFile << "ID " << "Salary " << endl;
outFile << id << sal;
outFile.close();
return 0;
}
}
What exactly are you trying to do with things like this?
std::string r = std::to_string(id); //converted id to string
inFile.open(r); /*Opens a file whose name is <id> ???*/
getline(cin, r); /*Overwrites the contents of r and does nothing??? */
Your entire program is fairly confusing. My best guess as to the (main) problem is that you aren't writing anything to inFile at all. Those 12 lines after outFile.open("examplee.txt") seem like they are trying to accomplish the following:
inFile << id << ' ' << job_class << ' ' << years_service << ' ' << ED << '\n';
Also, although I'm guessing this is for debugging purposes, but many of your methods do nothing or are unused. For example, you use emp.computation(job_class, years, ED) but after that you don't use emp at all in any way. And the three lines after that seem to mimic the behavior of Employee::computation and Employee::printout.
I suggest you carefully consider the specific steps you're trying to take, then think about the purpose of methods like getline and fstream::open, and ask yourself "Does this accomplish the task I had in mind?". Because I'm really struggling to understand what you are trying to do when I read this code.