In C Firstname Lastname Salary Write a program to read the t

In C++

Firstname Lastname Salary

Write a program to read the text file, and the content on the screen with formatting. Allocate 20 spaces for first name and last name, and use right alignment. Allocate 10 spaces for salary and use right alignment. The salary must have two significant digits after the decimal point. Compute the average salary of the employees in the company and display the average on the screen.

John Harris 50000.00 Lisa Smith 75000.00 Adam Johnson 68500.00 Sheila Smith 150000.00 Tristen Major 75800.00 Yannic Lennart 58000.00 Lorena Emil 43000.00 Tereza Santeri 48000.00

Solution

PROGRAM CODE:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>

using namespace std;

void display(string array[][3], int lengthofArray) // function to display based on alignment
{
   for(int i=0; i<lengthofArray; i++)
   {
       string name = array[i][0] + \" \" + array[i][1];
      
       for(int j=0; j<20-name.length(); j++) // printing spaces first
       {
       cout<<\" \";
       }
       cout<<name; //printing name (first name and last name)
       double amount = stod(array[i][2]);
       for(int j=0; j<10-array[i][2].length(); j++)
       {
       cout<<\" \";
       }
       cout << fixed << setprecision(2); // setting precision to print the decimal values
       cout<<amount;
       cout<<endl;
   }
}

int main() {
   string array[10][3];
   ifstream infile(\"names.txt\");
   string line;
   int m=0;
   while (getline(infile, line))
{
   istringstream iss(line);
   if (!(iss >> array[m][0] >> array[m][1] >>array[m][2]))
   {
       break;
   }
m++;
}
   display(array, m);
   return 0;
}

OUTPUT:

John Harris 50000.00
Lisa Smith 75000.00
Adam Johnson 65800.00
Sheila Smith 150000.00
Tristen Major 75800.00
Yannic Lennart 58000.00
Lorena Emil 43000.00
Teresa Santeri 48000.00

In C++ Firstname Lastname Salary Write a program to read the text file, and the content on the screen with formatting. Allocate 20 spaces for first name and las
In C++ Firstname Lastname Salary Write a program to read the text file, and the content on the screen with formatting. Allocate 20 spaces for first name and las

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site