私はこれで最高ではないことを知っていますが、私は学びたいと思っています。私は自分のプログラムをコンパイルしようとすると動作します。私はプログラムを実行しますが、最後になると、ダンプされたセグメンテーションフォールトが表示されます。私はそれが私がやろうとしている並べ替えを含むと思う。私がstealsPGで分類しようとする前にそれは働いていました。私はそれとスワップをした後、それは動作を停止しました。なぜどんなアイデア?セグメンテーションフォールトを持っているC++
//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;
}
ひどい書式設定が原因でクラッシュします。ただの冗談。真剣に:コードを正しくフォーマットする:P – user463035818
なぜあなたは 'player'と' pick'配列も作っていないのですか?同じコードを5回複製します。 – PaulMcKenzie
なぜわからないのですか?私はこれに対して初めてで、私が何をしているのか正確にはわからない。私のtxtファイルを整理しようとするまでうまくいきました。それは印刷され、働いていた。私は授業でやっていたプロジェクトと似たようにしようとしていましたが、私自身はやりました。私はおそらくそれが最もきれいではないことを知っていますが、私がそれをうまく働かせることができれば、この時点では幸せです。 – DaBears5423