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

Create a c program that reads a text file formatted such that each line contains

ID: 3677888 • Letter: C

Question

Create a c program that reads a text file formatted such that each line contains a first name, a last name, and a salary. Each is separated by a space. Print each line out to the screen. Get the average of the salaries and show it. Take the name of the file as a command line argument.

Exmple: Command Prompt$ ./a.out employees.txt

fname1 lname1 10000.000000

fname2 lname2 20000.000000

fname3 lname3 30000.000000

fname4 lname4 40000.000000

fname5 lname5 50000.000000

Average: $30000.00

Where employees.txt looks like:

fname1 lname1 10000.0

fname2 lname2 20000.0

fname3 lname3 30000.0

fname4 lname4 40000.0

fname5 lname5 50000.0

Explanation / Answer

#include<stdio>
#include<conio>
usinf namespace std;
void main(int argc, char *argv[])
{
   int i;
   float sum=0,av;
struct myStruct
{
char ch1[20];
char ch2[20];
int v1;
  
};

ifstream in(argv[1]);
while(in){
std::string line;
std::getline(in,line);
size_t lasttab=line.find_last_of(' ');
size_t firsttab=line.find_last_of(' ',lasttab-1);
mystruct data[5];
for(i=0;i<5;i++)
{
data[i].ch1=line.substr(0,firsttab).c_str();
data[i].ch2=line.substr(lasttab).c_str());
data[i].v1=atoi(line.substr(firsttab,lasttab).c_str());
printf(" %s %s %f",data[i].ch1,data[i].ch2, data[i].v1);
sum=sum+data[i].v1;


}
   av=sum/5;
   printf("Average: $%f",av);
}

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