Have the code, need comments. #include <iostream> #include <string> #include <fs
ID: 663293 • Letter: H
Question
Have the code, need comments.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct FootballPlayer
{
char name[30];
char pos[3];
int tds;
int ctch;
int pass;
int rec;
int rush;
};
void printData(FootballPlayer players[], int size);
void writeDataToFile(FootballPlayer players[], int size);
int searchNameStr(FootballPlayer players[], int size, const string& searchName);
int main( )
{
struct FootballPlayer players[10];
int i = 0;
ifstream infile;
//Reading data from file
infile.open("input.txt");
while (!infile.eof())
{
infile >> players[i].name;
infile >> players[i].pos;
infile >> players[i].tds;
infile >> players[i].ctch;
infile >> players[i].pass;
infile >> players[i].rec;
infile >> players[i].rush;
i++;
}
i--;
infile.close();
int ch;
string searchName;
bool found = false;
int index;
int j;
string pos;
while(true)
{
cout << " =====Inventory Management=====" << endl;
cout << "1. Print data" << endl;
cout << "2. Search record" << endl;
cout << "3. Update Record" << endl;
cout << "4. Exit" << endl;
cin >> ch;
switch(ch)
{
case 1:
printData(players, i);
break;
case 2:
cout << "Enter player name to Search: " << endl;
cin >> searchName;
index = searchNameStr(players, i, searchName);
if(index != -1)
{
cout <<"Name" << " "
<< "Pos." << " "
<< "TDs" << " "
<< "Ca." << " "
<< "Pas.Yds" << " "
<< "Rec.Yds" << " "
<< "Rus.Yds" << endl;
cout << players[index].name << " "
<< players[index].pos << " "
<< players[index].tds << " "
<< players[index].ctch << " "
<< players[index].pass << " "
<< players[index].rec << " "
<< players[index].rush << endl;
}
else
{
cout << "Record not found!" << endl;
}
break;
case 3:
cout << "Enter player name to upate date: " << endl;
cin >> searchName;
if(index != -1)
{
cout << "Enter position to update: " << endl;
cin >> pos;
players[index].pos == pos;
cout << "Player details updated successfully!" << endl;
}
else
{
cout << "Record not found!" << endl;
}
break;
case 4:
break;
default:
cout << "Invalid Choice" << endl;
}
if (ch == 4)
break;
}
writeDataToFile(players, i);
cout << "Thank You!" << endl;
return 0;
}
void printData(FootballPlayer players[], int size)
{
cout << "----------------Players data---------------------------------------------------" << endl;
cout <<"Name" << " " <<
"Pos." << " " <<
"TDs" << " " <<
"Ca." << " " <<
"Pas.Yds" << " " <<
"Rec.Yds" << " " <<
"Rus.Yds" << endl;
cout << "-------------------------------------------------------------------------------" << endl;
for(int j = 0; j < size; j++)
{
cout << players[j].name << " "
<< players[j].pos << " "
<< players[j].tds << " "
<< players[j].ctch << " "
<< players[j].pass << " "
<< players[j].rec << " "
<< players[j].rush << endl;
}
cout << "-------------------------------------------------------------------------------" << endl;
}
void writeDataToFile(FootballPlayer players[], int size)
{
//Writing data to file
ofstream myfile ("output.txt");
for(int j = 0; j < size; j++)
{
myfile << players[j].name << " " <<
players[j].pos << " " <<
players[j].tds << " " <<
players[j].ctch << " " <<
players[j].pass << " " <<
players[j].rec << " " <<
players[j].rush << " " << endl;
}
cout << "Writing data to output.txt is completed!" << endl;
myfile.close();
}
int searchNameStr(FootballPlayer players[], int size, const string& searchName)
{
bool found = false;
int index;
for(int j = 0; j < size; j++)
{
if(searchName == players[j].name)
{
found = true;
index = j;
break;
}
}
if(found == true)
return index;
else
return -1;
};
Explanation / Answer
the program is quite good.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.