Question

Simple C++: Getting an input file and doing calculations.

A company keeps the record of its employees in a text file. The text file is in the following format FirstName LastName ID Salary Write a program to read the text file and calculate the maximum salary. You then need to display the maximum salary and the FirstName, the LastName and the ID of the corresponding employee earning that maximum salary. You are permitted to use String data-type to handle the FirstName and the LastName. You DO NOT NEED to use arrays to solve this problem

Input file to be read from should be named "1030Prob1.dat" it will contain the following::

John Harris 11374 50000.00
Lisa Smith 11985 75000.00
Adam Johnson 12585 68500.00
Sheila Smith 11654 150000.00
Tristen Major 11274 75800.00
Yannic Lennart 15687 58000.00
Lorena Emil 17414 43000.00
Tereza Santeri 12597 48000.00
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include<fstream>
using namespace std;

int main()
{
ifstream inputFile;
inputFile.open("1030Prob1.dat");
char s[100];
int max = 0;
int count = 0;
string firstName, tempFirstName, lastName, tempLatName;
int id, tempId;
if (inputFile.is_open()) {
while (!inputFile.eof()) {

count++;
inputFile >> s;
if(count % 4 == 1){
tempFirstName = s;
}
else if(count % 4 == 2){
tempLatName = s;
}
else if(count % 4 == 3){
tempId = atoi(s);
}
else if(count % 4 == 0){
double salary = atoi(s);
if(max < salary){
max = salary;
lastName = firstName = tempFirstName;
tempLatName;
id = tempId;
}
}

}
}
cout<<"Maximum Salary: "<<max<<endl;
cout<<"First Name: "<<firstName<<" Last Name: "<<lastName<<" ID: "<<id<<endl;
inputFile.close();
return 0;

}

Output:

sh-4.3$ g++ -std-c++11-o main *.cpp sh-4.3$ main Maximum Salary: 150000 First Name: Sheila Last Name: Sheila ID: 11654

Add a comment
Know the answer?
Add Answer to:
Simple C++: Getting an input file and doing calculations. Input file to be read from should...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Simple C++. Sturctures- Please make sure it compiles. John Harris 1000 Lisa Smith 1002 Adam Johnson...

    Simple C++. Sturctures- Please make sure it compiles. John Harris 1000 Lisa Smith 1002 Adam Johnson 1007 Sheila Smith 1009 Tristen Major 1012 Yannic Lennart 1015 Lorena Emil 1018 Tereza Santeri 1020 Problem 1. Structures (10 points) A company keeps its employee databases in two text files. The first file contains the first name, the last name and ID in the following format First Name LastName ID This is provided to you as filel.txt The second file contains the ID,...

  • Write a Java program to read customer details from an input text file corresponding to problem...

    Write a Java program to read customer details from an input text file corresponding to problem under PA07/Q1, Movie Ticketing System The input text file i.e. customers.txt has customer details in the following format: A sample data line is provided below. “John Doe”, 1234, “Movie 1”, 12.99, 1.99, 2.15, 13.99 Customer Name Member ID Movie Name Ticket Cost Total Discount Sales Tax Net Movie Ticket Cost Note: if a customer is non-member, then the corresponding memberID field is empty in...

  • Description: This Java program will read in data from a file students.txt that keeps records of...

    Description: This Java program will read in data from a file students.txt that keeps records of some students. Each line in students.txt contains information about one student, including his/her firstname, lastname, gender, major, enrollmentyear and whether he/she is a full-time or part-time student in the following format. FIRSTNAME      LASTNAME        GENDER              MAJORENROLLMENTYEAR FULL/PART The above six fields are separated by a tab key. A user can input a keyword indicating what group of students he is searching for. For example, if...

  • Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.t...

    Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.txt) which you will create from the data provided below. The program will then prompt the user for a name to search for, when it finds the name it will output to a file (call it OFile.txt) the person’s id#, name,...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

  • Solve it for java Question Remember: You will need to read this assignment many times to...

    Solve it for java Question Remember: You will need to read this assignment many times to understand all the details of the you need to write. program Goal: The purp0se of this assignment is to write a Java program that models an elevator, where the elevator itself is a stack of people on the elevator and people wait in queues on each floor to get on the elevator. Scenario: A hospital in a block of old buildings has a nearly-antique...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT