Question

In: Computer Science

write a C++ program that permits users to enter the following information about your small company’s...

write a C++ program that permits users to enter the following information about your small company’s 5 employees and then writes the information to a file “employee.txt”:
ID No.   Sex (M/F) Hourly Wage Years with the Company
Make sure that you open the file for write using the two-step process of “trial read followed by write” so that an existing file with the same name does not get inadvertently clobbered.
Close all open files after completing read or write operations.
Test your program thoroughly. Using WordPad or Notepad, print out the content of the created file “employee.txt” residing in your project folder

Solutions

Expert Solution

CODE:

#include<iostream>
#include<fstream>

using namespace std;
//main method
int main(){
//two files
fstream fileTest, file;
//opening the TestFile to check whether there already exists such a file
fileTest.open("employee.txt",ios::in);
if(fileTest == NULL){
//if it doesn't
fileTest.close();
//new file is created
file.open("employee.txt",ios::out);
cout<<"File Created"<<endl;
//asks the use the data of 5 employees
for(int i=0;i<5;i++){
//asking the Id, sex, wager, years
cout<<"Enter the ID number: ";
string id;
cin>>id;
cout<<"Enter Sex(M/F): ";
string sex;
cin>>sex;
cout<<"Enter the hourly wage: ";
double wager;
cin>>wager;
cout<<"Enter the number of years worked with the company: ";
int years;
cin>>years;
//printing the details to the file
file<<"ID No: "<<id<<" Sex: "<<sex<<" Hourly wage: $"<<wager<<"/hour Years worked with the company: "<<years<<" years"<<endl;
}
file.close();
}else{
//if the fileTest is not null the code exits
cout<<"File already exists!"<<endl;
}
return 0;
}

_____________________________________

CODE IMAGES:

______________________________________________

OUTPUT:

employee.txt

___________________________________________

Feel free to ask any questions in the comments section
Thank You!


Related Solutions

Write a C++ program that will require users to enter an automobile VIN number and you...
Write a C++ program that will require users to enter an automobile VIN number and you will check certain aspects: The VIN number must be 17 characters exactly in length The VIN number may contain only letters and numbers The first character is the location of manufacture; we will check for this to be 1 to 5 (made in North America). The 10th character indicates the manufacture year. We will check for this being a letter between A and I...
Write a C program that prompts the user to enter some information about up to 20...
Write a C program that prompts the user to enter some information about up to 20 individuals (think of a way to welcome and prompt the user). It must be stored in a structure. Once data is entered, the program output it as shown in sample run below. Your program should include a structure with a tag name of: “information”. It should contain the following data as members: a struct to store employee's name, defined as: struct name fullname e.g....
Write a C++ console program that prompts a user to enter information for the college courses...
Write a C++ console program that prompts a user to enter information for the college courses you have completed, planned, or are in progress, and outputs it to a nicely-formatted table. Name the CPP as you wish (only use characters, underscores and number in your file name. DO NOT use blank). Its output should look something like this: Course Year Units Grade ---------- ---- ----- ----- comsc-110 2015 4 A comsc-165 2016 4 ? comsc-200 2016 4 ? comsc-155h 2014...
Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Write a C program that performs the following: Asks the user to enter his full name...
Write a C program that performs the following: Asks the user to enter his full name as one entry. Asks the user to enter his older brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have any older brother). Asks the user to enter his younger brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have...
IN C++: Write a program to display the following table. MUST use for-loop Enter the table...
IN C++: Write a program to display the following table. MUST use for-loop Enter the table size: 10 N N^2 Square root of N --------------------------------------------- 1 1 1.00 2 4 1.41 3 9 1.73 ….. 10 100 3.16
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value....
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value. The program should then output a message saying whether the number is positive, negative, or zero.
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT