Я работаю над программой, которая читает текстовый файл, а затем ищет все, что вводит пользователь. Если его вход найден, он распечатывает остальные данные. По какой-то причине он работает только для первых двух имен или около того. Мне также интересно, есть ли у вас, ребята, какие-нибудь советы для начинающих о том, как манипулировать данными (например, попросите кого-нибудь найти игрока и изменить прохождение или приземления int) с помощью команд, которые я могу понять. Я хочу научиться делать эти вещи, поэтому я ищу совет больше, чем реальный код. Вот код поиска:
void nameinator()
{
string line;
string player;
players temp;
players pl[10];
int i = 0;
ifstream file("players.txt");
if (file.is_open())
{
cout << "Type in the players name: ";
cin >> player;
for ( i = 0; i < 10; i++)
{
file >> pl[i].name;
if (pl[i].name == player)
{
file >> pl[i].position >> pl[i].passing >> pl[i].recieve >> pl[i].rush >> pl[i].touchdowns >> pl[i].catches;
cout << pl[i].name << " " << pl[i].position << " " << pl[i].passing
<< " " << pl[i].recieve << " " << pl[i].rush << " " << pl[i].touchdowns << " " << pl[i].catches << " " << i << endl;
break;
}
}
if (i == 10)
cout << "Could not find player" << i;
i = 0;
}
else
cout << "ERROR, could not acces file";
file.close();
}
, а вот код в контексте:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
struct players
{
string name;
string position;
int passing;
int recieve;
int rush;
int touchdowns;
int catches;
};
void nameinator()
{
string line;
string player;
players temp;
players pl[10];
int i = 0;
ifstream file("players.txt");
if (file.is_open())
{
cout << "Type in the players name: ";
cin >> player;
for ( i = 0; i < 10; i++)
{
file >> pl[i].name;
if (pl[i].name == player)
{
file >> pl[i].position >> pl[i].passing >> pl[i].recieve >> pl[i].rush >> pl[i].touchdowns >> pl[i].catches;
cout << pl[i].name << " " << pl[i].position << " " << pl[i].passing
<< " " << pl[i].recieve << " " << pl[i].rush << " " << pl[i].touchdowns << " " << pl[i].catches << " " << i << endl;
break;
}
}
if (i == 10)
cout << "Could not find player" << i;
i = 0;
}
else
cout << "ERROR, could not acces file";
file.close();
}
void printinator()
{
ifstream file("players.txt");
players pl[10];
if (file.is_open())
{
cout << "\n";
for (int i = 0; i < 10; ++i)
{
file >> pl[i].name >> pl[i].position >> pl[i].passing >> pl[i].recieve >> pl[i].rush >> pl[i].touchdowns >> pl[i].catches;
cout << pl[i].name << " " << pl[i].position << " " << pl[i].passing
<< " " << pl[i].recieve << " " << pl[i].rush << " " << pl[i].touchdowns << " " << pl[i].catches << endl;
}
file.close();
}
else
cout << "ERROR, could not acces file";
}
int touchinator()
{
cout << "";
return 0;
}
int catchinator()
{
cout << "";
return 0;
}
int passinator()
{
cout << "";
return 0;
}
int recievinator()
{
cout << "";
return 0;
}
int rushinator()
{
cout << "";
return 0;
}
void menu()
{
cout << "\n\t\tWhat would you like to do to the file? : ";
cout << "\n\t\t\t1 to print single players data";
cout << "\n\t\t\t2 to print all data";
cout << "\n\t\t\t3 to update player touchdowns";
cout << "\n\t\t\t4 to update number of catches";
cout << "\n\t\t\t5 to update passing yards";
cout << "\n\t\t\t6 to update recieveing yards";
cout << "\n\t\t\t7 to update players rushing yards";
cout << "\n\t\t\t99 to exit: ";
}
int main()
{
int choice = 0;
while (choice != 99)
{
menu();
cin >> choice;
switch (choice)
{
case 1:
nameinator();
break;
case 2:
printinator();
break;
case 3:
touchinator();
break;
case 4:
catchinator();
break;
case 5:
passinator();
break;
case 6:
recievinator();
break;
case 7:
rushinator();
break;
case 99:
exit(1);
}
}
return 0;
}
, а также текстовый документ
Bill Quarter_Back 70 0 8754 0 573
Jackson Receiver 55 87 50 5490 574
Grahm Running_Back 45 30 0 50 2800
McCoy Full_Back 25 10 0 25 3762
Daryl Quarter_Back 50 2 7560 0 450
Santiago Left_Tackle 5 0 0 0 0
Hanks Receiver 35 37 0 3590 876
Johnson Running_Back 25 80 0 100 4000
Miller Receiver 110 250 150 7867 2100
Ruth Quarter_Back 85 0 12901 0 3249