Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 具有分段错误c++;_C++ - Fatal编程技术网

C++ 具有分段错误c++;

C++ 具有分段错误c++;,c++,C++,我知道我在这方面不是最好的,但我正在努力学习。我试着编译我的程序,它工作了。我运行了这个程序,但当我运行到最后的时候,它显示了转储的分段错误。我想这涉及到我正在尝试的分类。在我尝试按偷盗次数分类之前,它起作用了。在我做了那个和交换之后,它停止了工作。你知道为什么吗 //main.cpp #include <string> #include "Player.h" #include <fstream> #include <iostream> #inclu

我知道我在这方面不是最好的,但我正在努力学习。我试着编译我的程序,它工作了。我运行了这个程序,但当我运行到最后的时候,它显示了转储的分段错误。我想这涉及到我正在尝试的分类。在我尝试按偷盗次数分类之前,它起作用了。在我做了那个和交换之后,它停止了工作。你知道为什么吗

    //main.cpp

#include <string>
#include "Player.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main()
{

string response;
Player* PlaArray = new Player[5];
cout << "Time to make your basktball team!" << endl;
cout << "Press 1 to go by position and 2 if you do not care!" << endl;
cin >> response;
ofstream outFile;
outFile.open("Team.txt");
ifstream inFile;
string pick1;
string pick2;
string pick3;
string pick4;
string pick5;
string player1;
string player2;
string player3;
string player4;
string player5;
string firstname;
string lastname;
string position;
string team;
string pointsPG;
string reboundsPG;
string assistsPG;
string stealsPG;
string fake;
if (response == "1")
{
    cout << "How would you like to sort your Point Guards?" << endl;
    cout << "Press 1 if you dont have a preferance, 2 for points per game(PPG), 3 for rebounds per game(RPG)," << endl;
    cout << "Press 4 for assists per game(APG), and 5 for steals per game(SPG)." << endl;
    cin >> pick1;
    if (pick1 == "1")
    {
        for(int x = 0; x<1; x++)
        {
            inFile.open("PG.txt");
            getline(inFile, position, ',');
            getline(inFile, firstname, ',');
            getline(inFile, lastname, ',');
            getline(inFile, team, ',');
            getline(inFile, pointsPG, ',');
            getline(inFile, reboundsPG, ',');
            getline(inFile, assistsPG, ',');
            getline(inFile, stealsPG, '\n');
            PlaArray[x].setFirstname(firstname);
            PlaArray[x].setLastname(lastname);
            PlaArray[x].setPosition(position);
            PlaArray[x].setTeam(team);
            PlaArray[x].setPointsPG(pointsPG);
            PlaArray[x].setReboundsPG(reboundsPG);
            PlaArray[x].setAssistsPG(assistsPG);
            PlaArray[x].setStealsPG(stealsPG);
            if (outFile.is_open())
            {
                player1 = PlaArray[x].Report();
            }
            else
            {
                cout << "Unable to Open File" << endl;
            }

        }
        inFile.close();
    }
    cout << "How would you like to sort your Shooting Guards?" << endl;
    cout << "Press 1 if you dont have a preferance, 2 for points per game(PPG), 3 for rebounds per game(RPG)," << endl;
    cout << "Press 4 for assists per game(APG), and 5 for steals per game(SPG)." << endl;
    cin >> pick2;
    if (pick2 == "1")
        {
        for(int x = 0; x<1; x++)
        {
            inFile.open("SG.txt");  
            getline(inFile, position, ',');
            getline(inFile, firstname, ',');
            getline(inFile, lastname, ',');
            getline(inFile, team, ',');
            getline(inFile, pointsPG, ',');
            getline(inFile, reboundsPG, ',');
            getline(inFile, assistsPG, ',');
            getline(inFile, stealsPG, '\n');
            PlaArray[x].setFirstname(firstname);
            PlaArray[x].setLastname(lastname);
            PlaArray[x].setPosition(position);
            PlaArray[x].setTeam(team);
            PlaArray[x].setPointsPG(pointsPG);
            PlaArray[x].setReboundsPG(reboundsPG);
            PlaArray[x].setAssistsPG(assistsPG);
            PlaArray[x].setStealsPG(stealsPG);
            if (outFile.is_open())
                {
                    player2 = PlaArray[x].Report();
                }
            else
            {
                cout << "Unable to Open File" << endl;
            }   

            }
            }
    inFile.close();
    cout << "How would you like to sort your Small Fowards?" << endl;
    cout << "Press 1 if you dont have a preferance, 2 for points per game(PPG), 3 for rebounds per game(RPG)," << endl;
    cout << "Press 4 for assists per game(APG), and 5 for steals per game(SPG)." << endl;
    cin >> pick3;
    if (pick3 == "1")
    {
        for(int x = 0; x<1; x++)
        {
        inFile.open("SF.txt");
        getline(inFile, position, ',');
        getline(inFile, firstname, ',');
        getline(inFile, lastname, ',');
        getline(inFile, team, ',');
        getline(inFile, pointsPG, ',');
        getline(inFile, reboundsPG, ',');
        getline(inFile, assistsPG, ',');
        getline(inFile, stealsPG, '\n');
        PlaArray[x].setFirstname(firstname);
        PlaArray[x].setLastname(lastname);
        PlaArray[x].setPosition(position);
        PlaArray[x].setTeam(team);
        PlaArray[x].setPointsPG(pointsPG);
        PlaArray[x].setReboundsPG(reboundsPG);
        PlaArray[x].setAssistsPG(assistsPG);
        PlaArray[x].setStealsPG(stealsPG);
        if (outFile.is_open())
        {
            player3 = PlaArray[x].Report();
        }
        else
        {
            cout << "Unable to Open File" << endl;
        }

        }
        inFile.close();
    }
    cout << "How would you like to sort your Power Fowards?" << endl;
    cout << "Press 1 if you dont have a preferance, 2 for points per game(PPG), 3 for rebounds per game(RPG)," << endl;
    cout << "Press 4 for assists per game(APG), and 5 for steals per game(SPG)." << endl;
    cin >> pick4;
    if (pick4 == "1")
    {
        for(int x = 0; x<1; x++)
        {
        inFile.open("PF.txt");
        getline(inFile, position, ',');
        getline(inFile, firstname, ',');
        getline(inFile, lastname, ',');
        getline(inFile, team, ',');
        getline(inFile, pointsPG, ',');
        getline(inFile, reboundsPG, ',');
        getline(inFile, assistsPG, ',');
        getline(inFile, stealsPG, '\n');
        PlaArray[x].setFirstname(firstname);
        PlaArray[x].setLastname(lastname);
        PlaArray[x].setPosition(position);
        PlaArray[x].setTeam(team);
        PlaArray[x].setPointsPG(pointsPG);
        PlaArray[x].setReboundsPG(reboundsPG);
        PlaArray[x].setAssistsPG(assistsPG);
        PlaArray[x].setStealsPG(stealsPG);
        if (outFile.is_open())
        {
            player4 = PlaArray[x].Report();
        }
        else
        {
            cout << "Unable to Open File" << endl;
        }

        }
        inFile.close();
    }
    cout << "How would you like to sort your Cetners Guards?" << endl;
    cout << "Press 1 if you dont have a preferance, 2 for points per game(PPG), 3 for rebounds per game(RPG)," << endl;
    cout << "Press 4 for assists per game(APG), and 5 for steals per game(SPG)." << endl;
    cin >> pick5;
    if (pick5 == "1")
    {
        for(int x = 0; x<1; x++)
        {
            inFile.open("C.txt");
            getline(inFile, position, ',');
            getline(inFile, firstname, ',');
            getline(inFile, lastname, ',');
            getline(inFile, team, ',');
            getline(inFile, pointsPG, ',');
            getline(inFile, reboundsPG, ',');
            getline(inFile, assistsPG, ',');
            getline(inFile, stealsPG, '\n');
            PlaArray[x].setFirstname(firstname);
            PlaArray[x].setLastname(lastname);
            PlaArray[x].setPosition(position);
            PlaArray[x].setTeam(team);
            PlaArray[x].setPointsPG(pointsPG);
            PlaArray[x].setReboundsPG(reboundsPG);
            PlaArray[x].setAssistsPG(assistsPG);
            PlaArray[x].setStealsPG(stealsPG);
    if (outFile.is_open())
    {
        player5 = PlaArray[x].Report();
    }
    else
    {
        cout << "Unable to Open File" << endl;
    }

    }
    inFile.close();
    }
if (pick5 == "5");
{
    for( int x = 0; x<10; x++)
    {
        inFile.open("C.txt");
        getline(inFile, position, ',');
            getline(inFile, firstname, ',');
            getline(inFile, lastname, ',');
            getline(inFile, team, ',');
            getline(inFile, pointsPG, ',');
            getline(inFile, reboundsPG, ',');
            getline(inFile, assistsPG, ',');
            getline(inFile, stealsPG, '\n');
            PlaArray[x].setFirstname(firstname);
            PlaArray[x].setLastname(lastname);
            PlaArray[x].setPosition(position);
            PlaArray[x].setTeam(team);
            PlaArray[x].setPointsPG(pointsPG);
            PlaArray[x].setReboundsPG(reboundsPG);
            PlaArray[x].setAssistsPG(assistsPG);
            PlaArray[x].setStealsPG(stealsPG);
    int i;
    int j;
    for (i = 0; i<9; i++)
    {
        for (j = 0; j<9; j++)
        {
            if  (PlaArray[j].getStealsPG().compare(PlaArray[j+1].getStealsPG()) >0)
            {
                swap(PlaArray[j], PlaArray[j+1]);
            }
        }
    }
    }
        for(int x = 0; x<1; x++)
        {
            getline(inFile, position, ',');
            getline(inFile, firstname, ',');
            getline(inFile, lastname, ',');
            getline(inFile, team, ',');
            getline(inFile, pointsPG, ',');
            getline(inFile, reboundsPG, ',');
            getline(inFile, assistsPG, ',');
            getline(inFile, stealsPG, '\n');
            PlaArray[x].setFirstname(firstname);
            PlaArray[x].setLastname(lastname);
            PlaArray[x].setPosition(position);
            PlaArray[x].setTeam(team);
            PlaArray[x].setPointsPG(pointsPG);
            PlaArray[x].setReboundsPG(reboundsPG);
            PlaArray[x].setAssistsPG(assistsPG);
            PlaArray[x].setStealsPG(stealsPG);
        if (outFile.is_open())
                {
                        player5 = PlaArray[x].Report();
                }
                else
                {
                        cout << "Unable to Open File" << endl;
                }
        }   
    inFile.close();


}
outFile.close();

    cout << player1 << endl;
    cout << player2 << endl;
    cout << player3 << endl;
    cout << player4 << endl;
    cout << player5 << endl;
}
}
void swap(int*i, int*j)
{
    int c;
    c = *i;
    *i = *j;
    *j = c;
}               
//main.cpp
#包括
#包括“Player.h”
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串响应;
玩家*PlaArray=新玩家[5];

cout我不确定这是唯一的问题,但这是一个(大!)问题

您可以使用

Player* PlaArray = new Player[5];
但是你试着用其中的10个

if (pick5 == "5");
{
    for( int x = 0; x<10; x++)
    {
        inFile.open("C.txt");
        getline(inFile, position, ',');
        getline(inFile, firstname, ',');
        getline(inFile, lastname, ',');
        getline(inFile, team, ',');
        getline(inFile, pointsPG, ',');
        getline(inFile, reboundsPG, ',');
        getline(inFile, assistsPG, ',');
        getline(inFile, stealsPG, '\n');
        PlaArray[x].setFirstname(firstname);
        PlaArray[x].setLastname(lastname);
        PlaArray[x].setPosition(position);
if(pick5==“5”);
{

对于(int x=0;xit崩溃了,因为它的格式很糟糕。只是开玩笑。严肃地说:正确地格式化代码:pw你为什么不让你的
播放器
拾取
数组呢?你把同一个代码重复了5次。我不知道为什么。我不熟悉这个,也不确定我在做什么。在我尝试排序之前,它工作得很好输出我的txt文件。它打印出来并正在工作。我试图做一个类似于我必须在课堂上做的项目,但我自己做了。我知道这可能不是最漂亮的,但在这一点上,如果我能让它工作,我会很高兴。你的
swap
函数用于交换整数。你用它来交换
播放器
对象,它们不是整数。你没有收到
交换(PlaArray[j],PlaArray[j+1])的警告吗;
line?@Barmar可能被调用了
std::swap
。这是使用命名空间std;
的天才。谢谢你排除了这个错误。我应该做些什么来更好地格式化呢?而且它仍然不会交换?关于如何修复这个问题有什么想法吗?要获得更好的格式,你应该使用一个可以支持自动缩进。我使用
vim
,但还有其他的。对于交换,请删除您的函数并使用标准交换(如果您使用的是C++98,请添加
#包含
,如果您使用的是C++11或C++14,请添加
#包含