Hello,
why when i inserted group name without space,the program suddenly quit.
here is my code
Code:
#include<iostream>
#include<string>
#include<fstream>
#include<conio.h>
using namespace std;
int main()
{
cout<<"How many groups?"<<endl;
int nGroup;
cin>>nGroup;
string delGroup;
string delTeams;
for(int i=0;i<nGroup;i++)
{
cout<<"What name for Group "<<i+1<<"?"<<endl;
char GrpName[300];
fflush(stdin);
cin.getline(GrpName,30);
string FileName=GrpName;
//create a string to delete the Group file
string copyFileName=FileName;
int indexGroup=copyFileName.find_first_of(' ');
copyFileName.erase(indexGroup);
delGroup="del "+copyFileName+"*";
fstream Group(FileName.c_str(), ios_base::in);// attempt open for read
if (!Group)
{
// file doesn't exist; create a new one
Group.open(FileName.c_str(), ios::out|ios::binary|ios::app);
cout<<"Lets input team names..."<<endl;
cout<<"How many teams?"<<endl;
int nTeam;
cin>>nTeam;
for(int j=0;j<nTeam;j++)
{
cout<<"What name for team "<<j+1<<"?"<<endl;
char TeamName[300];
fflush(stdin);
cin.getline(TeamName,30);
Group<<TeamName<<"\n";
string sTeam=TeamName;
//create a string to delete the team file and team extended files
string copysTeam=sTeam;
int indexTeam=copysTeam.find_first_of(' ');
copysTeam.erase(indexTeam);
delTeams="del "+copysTeam+"*";
string sTeamEx=sTeam + "ex";
fstream Team(sTeam.c_str(), ios_base::in);// attempt open for read
if (!Team)
{
// Team doesn't exist; create a new one
Team.open(sTeam.c_str(), ios::out|ios::binary|ios::app);
Team.close();
}
else //ok, Team exists; error message
{
cout<<"ERROR, the Team already exists"<<endl;
}
fstream TeamEx(sTeamEx.c_str(), ios_base::in);// attempt open for read
if (!TeamEx)
{
// Team Extended doesn't exist; create a new one
TeamEx.open(sTeamEx.c_str(), ios::out|ios::binary|ios::app);
TeamEx.close();
}
else //ok, Team Extended exists; error message
{
cout<<"ERROR, the Team Extended already exists"<<endl;
}
}
Group.close();
}
else //ok, group exists; show the error message
{
cout<<"ERROR, the group already exists"<<endl;
}
}
cout<<"Do you want to clean up all files created ?(y/n)"<<endl;
char YesNo[1];
cin>>YesNo[0];
if(YesNo[0]=='y'||YesNo[0]=='Y')
{
cout<<"deleting all files created"<<endl;
cout<<"deleting Group files..."<<endl;
system(delGroup.c_str());
cout<<"deleting Team files and Team Extended files..."<<endl;
system(delTeams.c_str());
}
getch();
return 0;
}
let say group name --> GroupA
result --> program suddenly quit
let say group name --> Group A
result --> the program continue
why is this happened ?