Получение -858993460 Ошибка от l oop, означающая копирование данных в текстовый файл - PullRequest
0 голосов
/ 01 февраля 2020

Из того, что я понимаю, -858993460 означает, что код использует неинициализированную память, я не знаю, как получить мой код, чтобы не деинициализировать данные, которые у меня уже есть. Любая помощь приветствуется

void touchinator()
{
    string line;
    string player;
    players temp;
    players pl[10];
    int i = 0, ttouch = 0;
    ifstream file("players.txt");
    ofstream ofile("templayers.txt");

    if (file.is_open())
    {
        cout << "Type in the players name: "; //Allows user to input a name 
        cin >> player; //Stores the name
        for (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;
            if (pl[i].name == player) //Compares the name to the names in the file
            {
                cout << "\n\tName" << "\t\tPosition" << "\t\tPassing Yards\t" << "Receiving Yards\t\t" << "Rush Yards\t" << "Touchdowns\t" << "Catches\n"; //Creates labels
                cout << "\n\t" << pl[i].name << "\t\t" << pl[i].position << "\t\t" << pl[i].passing
                     << "\t\t" << pl[i].recieve << "\t\t\t" << pl[i].rush << "\t\t" << pl[i].touchdowns << "\t\t" << pl[i].catches << "\n"; //prints out the data of searched for player

                cout << "\n\t\tThe selected player has " << pl[i].touchdowns << " Touchdowns, What would you like to change the value to? : ";
                cin >> ttouch;
                if (ofile.is_open())
                {
                    for (int k = 0; k < 10; k++)
                    {
                        if (pl[k].name != player)
                        {
                        ofile << " " << pl[k].name << " " << pl[k].position << " " << pl[k].passing << " " << pl[k].recieve
                            << " " << pl[k].rush << " " << pl[k].touchdowns << " " << pl[k].catches << "\n";
                        }
                        else
                        {
                            ofile << " " << pl[k].name << " " << pl[k].position << " " << pl[k].passing << " " << pl[k].recieve
                                << " " << pl[k].rush << " " << ttouch << " " << pl[k].catches << "\n";
                        }
                    }
                }
                else
                    cout << "Could not access temp file";
                break; //Breaks to prevent messy display
            }
        }
        if (i == 10)
            cout << "Could not find player"; //If the search turns out unsuccessful
        i = 0;
    }
    else
        cout << "ERROR, could not acces file"; //In case the file could not be accessed
    file.close(); //Closes file
}

Текстовый файл

Bill QuarterBack 70 0 8754 0 573
Jackson Receiver 55 87 50 5490 574
Grahm RunningBack 45 30 0 50 2800
McCoy FullBack 25 10 0 25 3762
Daryl QuarterBack 50 2 7560 0 450
Santia LeftTackle 5 0 0 0 0
Hanks Receiver 35 37 0 3590 876
Johnson RunningBack 25 80 0 100 4000
Miller Receiver 110 250 150 7867 2100
Ruth QuarterBack 85 0 12901 0 3249

Если, например, я попытался изменить значение приземления для Хэнкса (3590 приземлений на 30), то оно изменилось бы и все остальные останутся прежними, однако имена ниже всех изменятся на -858993460

 Bill QuarterBack 70 0 8754 0 573
 Jackson Receiver 55 87 50 5490 574
 Grahm RunningBack 45 30 0 50 2800
 McCoy FullBack 25 10 0 25 3762
 Daryl QuarterBack 50 2 7560 0 450
 Santia LeftTackle 5 0 0 0 0
 Hanks Receiver 35 37 0 30 876
   -858993460 -858993460 -858993460 -858993460 -858993460
   -858993460 -858993460 -858993460 -858993460 -858993460
   -858993460 -858993460 -858993460 -858993460 -858993460

код в полном контексте

#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: "; //Allows user to input a name 
        cin >> player; //Stores the name
        for ( 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;
            if (pl[i].name == player) //Compares the name to the names in the file
            {
                cout << "\n\tName" << "\t\tPosition" << "\t\tPassing Yards\t" << "Receiving Yards\t\t" << "Rush Yards\t" << "Touchdowns\t" << "Catches\n"; //Creates labels
                cout << "\n\t" << pl[i].name << "\t\t" << pl[i].position << "\t\t" << pl[i].passing 
                     << "\t\t" << pl[i].recieve << "\t\t\t" << pl[i].rush << "\t\t" << pl[i].touchdowns << "\t\t" << pl[i].catches << "\n"; //prints out the data of searched for player
                break; //Breaks to prevent messy display
            }
        }
        if (i == 10)
            cout << "Could not find player"; //If the search turns out unsuccessful
        i = 0;
    }
    else
        cout << "ERROR, could not acces file"; //In case the file could not be accessed
    file.close(); //Closes file
}

void printinator()
{
    ifstream file("players.txt"); 
    players pl[10]; //declaring array to store player values in 

    if (file.is_open()) //checks if file opened without error
    {
        cout << "\n\tName" << "\t\tPosition" << "\t\tPassing Yards\t" << "Receiving Yards\t\t" << "Rush Yards\t" << "Touchdowns\t" << "Catches\n"; //Labels printed info
        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; //Labels the file with the struct
            cout << "\n\t" << pl[i].name << "\t\t" << pl[i].position << "\t\t" << pl[i].passing
                 << "\t\t" << pl[i].recieve << "\t\t\t" << pl[i].rush << "\t\t" << pl[i].touchdowns << "\t\t" << pl[i].catches << "\n"; //Prints out the file in accordance with the labels created for ease of access
        }
        file.close(); //closes files to prevent errors
    }
    else
        cout << "ERROR, could not acces file"; //In case of an error notifies user
}

void touchinator()
{
    string line;
    string player;
    players temp;
    players pl[10];
    int i = 0, ttouch = 0;
    ifstream file("players.txt");
    ofstream ofile("templayers.txt");

    if (file.is_open())
    {
        cout << "Type in the players name: "; //Allows user to input a name 
        cin >> player; //Stores the name
        for (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;
            if (pl[i].name == player) //Compares the name to the names in the file
            {
                cout << "\n\tName" << "\t\tPosition" << "\t\tPassing Yards\t" << "Receiving Yards\t\t" << "Rush Yards\t" << "Touchdowns\t" << "Catches\n"; //Creates labels
                cout << "\n\t" << pl[i].name << "\t\t" << pl[i].position << "\t\t" << pl[i].passing
                     << "\t\t" << pl[i].recieve << "\t\t\t" << pl[i].rush << "\t\t" << pl[i].touchdowns << "\t\t" << pl[i].catches << "\n"; //prints out the data of searched for player

                cout << "\n\t\tThe selected player has " << pl[i].touchdowns << " Touchdowns, What would you like to change the value to? : ";
                cin >> ttouch;
                if (ofile.is_open())
                {
                    for (int k = 0; k < 10; k++)
                    {
                        if (pl[k].name != player)
                        {
                        ofile << " " << pl[k].name << " " << pl[k].position << " " << pl[k].passing << " " << pl[k].recieve
                            << " " << pl[k].rush << " " << pl[k].touchdowns << " " << pl[k].catches << "\n";
                        }
                        else
                        {
                            ofile << " " << pl[k].name << " " << pl[k].position << " " << pl[k].passing << " " << pl[k].recieve
                                << " " << pl[k].rush << " " << ttouch << " " << pl[k].catches << "\n";
                        }
                    }
                }
                else
                    cout << "Could not access temp file";
                break; //Breaks to prevent messy display
            }
        }
        if (i == 10)
            cout << "Could not find player"; //If the search turns out unsuccessful
        i = 0;
    }
    else
        cout << "ERROR, could not acces file"; //In case the file could not be accessed
    file.close(); //Closes file
}

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;
}
...