Question: When I try to use Case 3 to update player record the program removes some of my players information instead of updating the selected players
When I try to use Case 3 to update player record the program removes some of my players information instead of updating the selected players information..
Using Visual Studio for C++
Any Thoughts?
Here is the Code:
#include
#include
#include
using namespace std;
struct FootballPlayer
{
string name;
string position;
int touchdowns;
int catches;
int passingYards;
int receivingYards;
int runningYards;
};
void printData(struct FootballPlayer players[10], int size);
void writeDataToFile(struct FootballPlayer players[10], int size);
int searchNameStr(struct FootballPlayer players[10], int size, string searchName);
int main()
{
struct FootballPlayer players[10];
int i = 0;
ifstream infile;
//Reading data from file
infile.open("Ch9_Ex7data.txt");
while (!infile.eof())
{
infile >> players[i].name;
infile >> players[i].position;
infile >> players[i].touchdowns;
infile >> players[i].catches;
infile >> players[i].passingYards;
infile >> players[i].receivingYards;
infile >> players[i].runningYards;
i++;
}
i--;
infile.close();
int ch;
string searchName;
bool found = false;
int index;
string position;
int touchdowns;
int catches;
int passingYards;
int receivingYards;
int runningYards;
while (true)
{
cout << " =====Inventory Management===== ";
cout << "1. Print data ";
cout << "2. Search record ";
cout << "3. Update Record ";
cout << "4. Exit ";
cin >> ch;
switch (ch)
{
case 1:
printData(players, i);
break;
case 2:
cout << "Enter player name to Search: ";
cin >> searchName;
index = searchNameStr(players, i, searchName);
if (index != -1)
{
cout << players[index].name << "\t" << players[index].position << "\t" << players[index].touchdowns << "\t" << players[index].catches << "\t" << players[index].passingYards
<< players[index].receivingYards << "\t" << players[index].runningYards << " ";
}
else
{
cout << "Record not found! ";
}
break;
case 3:
cout << "Enter player name to update: ";
cin >> searchName;
if (index = -1)
{
cout << "Enter position to update: ";
cin >> position;
players[index].position = position;
cout << "Enter touchdowns";
cin >> touchdowns;
players[index].touchdowns = touchdowns;
cout << "Enter catches;";
cin >> catches;
players[index].catches = catches;
cout << "Enter passingYards";
cin >> passingYards;
players[index].passingYards = passingYards;
cout << "Enter receivingYards";
cin >> runningYards;
players[index].runningYards = runningYards;
cout << "Player details updated successfully! ";
}
else
{
cout << "Record not found! ";
}
break;
case 4:
break;
default:
cout << "Invalid Choice ";
}
if (ch == 4)
break;
}
writeDataToFile(players, i);
cout << "Thank You! ";
return 0;
}
void printData(struct FootballPlayer players[10], int size)
{
cout << "----------------Players data--------------------------- ";
for (int j = 0; j { cout << players[j].name << "\t" << players[j].position << "\t" << players[j].touchdowns << "\t" << players[j].catches << "\t" << players[j].passingYards << "\t"<< players[j].receivingYards << "\t" << players[j].runningYards << " "; } cout << "-------------------------------------------------------- "; } void writeDataToFile(struct FootballPlayer players[10], int size) { //Writing data to file ofstream myfile("output.txt"); for (int j = 0; j { myfile << players[j].name << " " << players[j].position << " " << players[j].touchdowns << " " << players[j].catches << " " << players[j].passingYards << players[j].receivingYards << " " << players[j].runningYards << " "; } cout << "Writing data to output.txt is completed! "; myfile.close(); } int searchNameStr(struct FootballPlayer players[10], int size, string searchName) { bool found = false; int index; for (int j = 0; j { if (searchName == players[j].name) { found = true; index = j; break; } } if (found == true) return index; else return -1; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
