Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I make this code cout << \" File not found\"; and terminate the program w

ID: 3854655 • Letter: H

Question

How do I make this code cout << " File not found"; and terminate the program when the file cannot be opened ?



//Header file declaration
#include <iostream>
#include <cstring>
#include <fstream>
#include <iomanip>
using namespace std;
// A structure declaration
struct employeeType
{
string name;
double hours;
double hourlyPay;
double amountEarned;
};
string format(string str);
//function prototypes
void readData(employeeType emp[], int& i, ifstream& fileContent);
void bubblesort(employeeType emp[], int count);
void weeklyPay(employeeType arr[], int MAX);
void printEmployeesAboveAvg(employeeType arr[], int MAX);
void printEmployeeData(employeeType arr[], int MAX);
string format(string str)
{
for (int i = 0; i < str.length(); i++)
{
if (i == 0)
str[i] = toupper(str[i]);
else
str[i] = tolower(str[i]);
}
return str;
}
void readData(employeeType emp[], int& i, ifstream& fileContent)
{
i = 0;
while (fileContent >> emp[i].name >> emp[i].hours >> emp[i].hourlyPay)
{
emp[i].name = format(emp[i].name);
i++;
}
}
//definition for bubblesort function
void bubblesort(employeeType arr[], int MAX)
{
int n = MAX;
// Sorting strings using bubble sort
bool didSwap;
employeeType temp;
do
{
didSwap = false; //assume there are no swaps
for (int i = 0; i < n - 1; i++)
{
if (arr[i].name > arr[i + 1].name)
{
//I am not cheating
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
didSwap = true; //there was a swap...
}
}
} while (didSwap);
}
//definition for weeklyPay function to calculate the amount earned
//by each employee
void weeklyPay(employeeType emp[], int MAX)
{
for (int i = 0; i < MAX; i++)
{
if (emp[i].hours > 40)
{
emp[i].amountEarned = emp[i].hourlyPay * 40 + (emp[i].hours - 40) * emp[i].hourlyPay * 1.5;
}
else
{
emp[i].amountEarned = emp[i].hourlyPay * emp[i].hours;
}
}
}
//defintion for printEmployeesAboveAvg function
void printEmployeesAboveAvg(employeeType emp[], int MAX)
{
double temp = 0;
for (int i = 0; i < MAX; i++)
{
if(emp[i].name != "")
{
temp = temp + emp[i].amountEarned;
}
}
double avarage = temp / MAX;
cout << " Employees whose payroll is larger than " << avarage << " (Ave)"<< endl;
for (int i = 0; i < MAX; i++)
{
if (emp[i].amountEarned > avarage)
{
if(emp[i].name != "")
cout << emp[i].name << endl;
}
}
}
//defintion for printEmployeesData function
void printEmployeeData(employeeType emp[], int MAX)
{
cout << " " << left << setw(30) << " Employee Name " << left << setw(30) << " Hours worked "<< left << setw(30) << " Hourly wage " << left << setw(30) << " Amount earned " << endl;
for (int i = 0; i < MAX; i++) {
if (emp[i].name != "")
//setw means set width between columns according to the their sizes
cout << "" << left << setw(36) << emp[i].name << right << setw(8) << emp[i].hours<< right << setw(29) << emp[i].hourlyPay << right << setw(32) << emp[i].amountEarned << endl;
}
}
//start of main function
int main()
{
ifstream inFile;
struct employeeType emp[200];
char fileName[250];
cout << "Enter inputfile: ";
cin >> fileName;
inFile.open(fileName);
int i;
readData(emp, i, inFile);
bubblesort(emp, i);
weeklyPay(emp, i);
cout << fixed << setprecision(2);
printEmployeeData(emp, i);
printEmployeesAboveAvg(emp, i);
} How do I make this code cout << " File not found"; and terminate the program when the file cannot be opened ?



//Header file declaration
#include <iostream>
#include <cstring>
#include <fstream>
#include <iomanip>
using namespace std;
// A structure declaration
struct employeeType
{
string name;
double hours;
double hourlyPay;
double amountEarned;
};
string format(string str);
//function prototypes
void readData(employeeType emp[], int& i, ifstream& fileContent);
void bubblesort(employeeType emp[], int count);
void weeklyPay(employeeType arr[], int MAX);
void printEmployeesAboveAvg(employeeType arr[], int MAX);
void printEmployeeData(employeeType arr[], int MAX);
string format(string str)
{
for (int i = 0; i < str.length(); i++)
{
if (i == 0)
str[i] = toupper(str[i]);
else
str[i] = tolower(str[i]);
}
return str;
}
void readData(employeeType emp[], int& i, ifstream& fileContent)
{
i = 0;
while (fileContent >> emp[i].name >> emp[i].hours >> emp[i].hourlyPay)
{
emp[i].name = format(emp[i].name);
i++;
}
}
//definition for bubblesort function
void bubblesort(employeeType arr[], int MAX)
{
int n = MAX;
// Sorting strings using bubble sort
bool didSwap;
employeeType temp;
do
{
didSwap = false; //assume there are no swaps
for (int i = 0; i < n - 1; i++)
{
if (arr[i].name > arr[i + 1].name)
{
//I am not cheating
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
didSwap = true; //there was a swap...
}
}
} while (didSwap);
}
//definition for weeklyPay function to calculate the amount earned
//by each employee
void weeklyPay(employeeType emp[], int MAX)
{
for (int i = 0; i < MAX; i++)
{
if (emp[i].hours > 40)
{
emp[i].amountEarned = emp[i].hourlyPay * 40 + (emp[i].hours - 40) * emp[i].hourlyPay * 1.5;
}
else
{
emp[i].amountEarned = emp[i].hourlyPay * emp[i].hours;
}
}
}
//defintion for printEmployeesAboveAvg function
void printEmployeesAboveAvg(employeeType emp[], int MAX)
{
double temp = 0;
for (int i = 0; i < MAX; i++)
{
if(emp[i].name != "")
{
temp = temp + emp[i].amountEarned;
}
}
double avarage = temp / MAX;
cout << " Employees whose payroll is larger than " << avarage << " (Ave)"<< endl;
for (int i = 0; i < MAX; i++)
{
if (emp[i].amountEarned > avarage)
{
if(emp[i].name != "")
cout << emp[i].name << endl;
}
}
}
//defintion for printEmployeesData function
void printEmployeeData(employeeType emp[], int MAX)
{
cout << " " << left << setw(30) << " Employee Name " << left << setw(30) << " Hours worked "<< left << setw(30) << " Hourly wage " << left << setw(30) << " Amount earned " << endl;
for (int i = 0; i < MAX; i++) {
if (emp[i].name != "")
//setw means set width between columns according to the their sizes
cout << "" << left << setw(36) << emp[i].name << right << setw(8) << emp[i].hours<< right << setw(29) << emp[i].hourlyPay << right << setw(32) << emp[i].amountEarned << endl;
}
}
//start of main function
int main()
{
ifstream inFile;
struct employeeType emp[200];
char fileName[250];
cout << "Enter inputfile: ";
cin >> fileName;
inFile.open(fileName);
int i;
readData(emp, i, inFile);
bubblesort(emp, i);
weeklyPay(emp, i);
cout << fixed << setprecision(2);
printEmployeeData(emp, i);
printEmployeesAboveAvg(emp, i);
} How do I make this code cout << " File not found"; and terminate the program when the file cannot be opened ?



//Header file declaration
#include <iostream>
#include <cstring>
#include <fstream>
#include <iomanip>
using namespace std;
// A structure declaration
struct employeeType
{
string name;
double hours;
double hourlyPay;
double amountEarned;
};
string format(string str);
//function prototypes
void readData(employeeType emp[], int& i, ifstream& fileContent);
void bubblesort(employeeType emp[], int count);
void weeklyPay(employeeType arr[], int MAX);
void printEmployeesAboveAvg(employeeType arr[], int MAX);
void printEmployeeData(employeeType arr[], int MAX);
string format(string str)
{
for (int i = 0; i < str.length(); i++)
{
if (i == 0)
str[i] = toupper(str[i]);
else
str[i] = tolower(str[i]);
}
return str;
}
void readData(employeeType emp[], int& i, ifstream& fileContent)
{
i = 0;
while (fileContent >> emp[i].name >> emp[i].hours >> emp[i].hourlyPay)
{
emp[i].name = format(emp[i].name);
i++;
}
}
//definition for bubblesort function
void bubblesort(employeeType arr[], int MAX)
{
int n = MAX;
// Sorting strings using bubble sort
bool didSwap;
employeeType temp;
do
{
didSwap = false; //assume there are no swaps
for (int i = 0; i < n - 1; i++)
{
if (arr[i].name > arr[i + 1].name)
{
//I am not cheating
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
didSwap = true; //there was a swap...
}
}
} while (didSwap);
}
//definition for weeklyPay function to calculate the amount earned
//by each employee
void weeklyPay(employeeType emp[], int MAX)
{
for (int i = 0; i < MAX; i++)
{
if (emp[i].hours > 40)
{
emp[i].amountEarned = emp[i].hourlyPay * 40 + (emp[i].hours - 40) * emp[i].hourlyPay * 1.5;
}
else
{
emp[i].amountEarned = emp[i].hourlyPay * emp[i].hours;
}
}
}
//defintion for printEmployeesAboveAvg function
void printEmployeesAboveAvg(employeeType emp[], int MAX)
{
double temp = 0;
for (int i = 0; i < MAX; i++)
{
if(emp[i].name != "")
{
temp = temp + emp[i].amountEarned;
}
}
double avarage = temp / MAX;
cout << " Employees whose payroll is larger than " << avarage << " (Ave)"<< endl;
for (int i = 0; i < MAX; i++)
{
if (emp[i].amountEarned > avarage)
{
if(emp[i].name != "")
cout << emp[i].name << endl;
}
}
}
//defintion for printEmployeesData function
void printEmployeeData(employeeType emp[], int MAX)
{
cout << " " << left << setw(30) << " Employee Name " << left << setw(30) << " Hours worked "<< left << setw(30) << " Hourly wage " << left << setw(30) << " Amount earned " << endl;
for (int i = 0; i < MAX; i++) {
if (emp[i].name != "")
//setw means set width between columns according to the their sizes
cout << "" << left << setw(36) << emp[i].name << right << setw(8) << emp[i].hours<< right << setw(29) << emp[i].hourlyPay << right << setw(32) << emp[i].amountEarned << endl;
}
}
//start of main function
int main()
{
ifstream inFile;
struct employeeType emp[200];
char fileName[250];
cout << "Enter inputfile: ";
cin >> fileName;
inFile.open(fileName);
int i;
readData(emp, i, inFile);
bubblesort(emp, i);
weeklyPay(emp, i);
cout << fixed << setprecision(2);
printEmployeeData(emp, i);
printEmployeesAboveAvg(emp, i);
}


//Header file declaration
#include <iostream>
#include <cstring>
#include <fstream>
#include <iomanip>
using namespace std;
// A structure declaration
struct employeeType
{
string name;
double hours;
double hourlyPay;
double amountEarned;
};
string format(string str);
//function prototypes
void readData(employeeType emp[], int& i, ifstream& fileContent);
void bubblesort(employeeType emp[], int count);
void weeklyPay(employeeType arr[], int MAX);
void printEmployeesAboveAvg(employeeType arr[], int MAX);
void printEmployeeData(employeeType arr[], int MAX);
string format(string str)
{
for (int i = 0; i < str.length(); i++)
{
if (i == 0)
str[i] = toupper(str[i]);
else
str[i] = tolower(str[i]);
}
return str;
}
void readData(employeeType emp[], int& i, ifstream& fileContent)
{
i = 0;
while (fileContent >> emp[i].name >> emp[i].hours >> emp[i].hourlyPay)
{
emp[i].name = format(emp[i].name);
i++;
}
}
//definition for bubblesort function
void bubblesort(employeeType arr[], int MAX)
{
int n = MAX;
// Sorting strings using bubble sort
bool didSwap;
employeeType temp;
do
{
didSwap = false; //assume there are no swaps
for (int i = 0; i < n - 1; i++)
{
if (arr[i].name > arr[i + 1].name)
{
//I am not cheating
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
didSwap = true; //there was a swap...
}
}
} while (didSwap);
}
//definition for weeklyPay function to calculate the amount earned
//by each employee
void weeklyPay(employeeType emp[], int MAX)
{
for (int i = 0; i < MAX; i++)
{
if (emp[i].hours > 40)
{
emp[i].amountEarned = emp[i].hourlyPay * 40 + (emp[i].hours - 40) * emp[i].hourlyPay * 1.5;
}
else
{
emp[i].amountEarned = emp[i].hourlyPay * emp[i].hours;
}
}
}
//defintion for printEmployeesAboveAvg function
void printEmployeesAboveAvg(employeeType emp[], int MAX)
{
double temp = 0;
for (int i = 0; i < MAX; i++)
{
if(emp[i].name != "")
{
temp = temp + emp[i].amountEarned;
}
}
double avarage = temp / MAX;
cout << " Employees whose payroll is larger than " << avarage << " (Ave)"<< endl;
for (int i = 0; i < MAX; i++)
{
if (emp[i].amountEarned > avarage)
{
if(emp[i].name != "")
cout << emp[i].name << endl;
}
}
}
//defintion for printEmployeesData function
void printEmployeeData(employeeType emp[], int MAX)
{
cout << " " << left << setw(30) << " Employee Name " << left << setw(30) << " Hours worked "<< left << setw(30) << " Hourly wage " << left << setw(30) << " Amount earned " << endl;
for (int i = 0; i < MAX; i++) {
if (emp[i].name != "")
//setw means set width between columns according to the their sizes
cout << "" << left << setw(36) << emp[i].name << right << setw(8) << emp[i].hours<< right << setw(29) << emp[i].hourlyPay << right << setw(32) << emp[i].amountEarned << endl;
}
}
//start of main function
int main()
{
ifstream inFile;
struct employeeType emp[200];
char fileName[250];
cout << "Enter inputfile: ";
cin >> fileName;
inFile.open(fileName);
int i;
readData(emp, i, inFile);
bubblesort(emp, i);
weeklyPay(emp, i);
cout << fixed << setprecision(2);
printEmployeeData(emp, i);
printEmployeesAboveAvg(emp, i);
}

Explanation / Answer

Replace your main function with this. I have highlighted the updated lines of code.

int main()

{

ifstream inFile;

struct employeeType emp[200];

char fileName[250];

cout << "Enter inputfile: ";

cin >> fileName;

inFile.open(fileName);

if(inFile.is_open())
{

   int i;

   readData(emp, i, inFile);

   bubblesort(emp, i);

   weeklyPay(emp, i);

   cout << fixed << setprecision(2);

   printEmployeeData(emp, i);

   printEmployeesAboveAvg(emp, i);

}
else
   cout << "File not found" << endl;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote