Infinite Loop PLEASE HELP

I am getting an infinite loop when my program generates a new bracket

#include <iostream>
#include <string>
#include <stdlib.h>
#include <numeric>
#include <fstream>
#include <vector>
using namespace std;


const int MAXTEAMS = 20;
const char* c;

//Function Declarations
void delay(int i);
int menuOption();
bool fileExists(const char *fileName);
void generateBracket(string bracketName);

//Struct Declarations
struct team{
string name;
bool played;
};



int main()
{

srand(time(NULL));
int numTeams = 0;
string bracketName;
char createBracket;
int menuChoice=0;
fstream bracketStream;
//need to be a vector
vector<team> teams1;

//////////////////////////////
//Menu
//////////////////////////////
menuChoice = menuOption();

//menuChoice not to Quit
while(menuChoice != 3)
{
switch(menuChoice)
{
//////////////////////////////
//Getting team names
/////////////////////////////
case 1:
//openBracket();
cout<<"Which bracket would you like to open: ";
cin>>bracketName;
bracketName.append(".txt");
c = bracketName.c_str(); //string to char*
if(fileExists(c)==true)
{
bracketStream.open(c, fstream::ate);
cout<<"How many teams are you adding: ";
cin>>numTeams;
cout<<"Enter team Names"<<endl;
for(int i=0; i<numTeams; i++)
{
teams1.push_back(team());
cout<<"Team "<<i+1<<" : ";
delay(40000);
cin>>teams1[i].name;
bracketStream<<teams1[i].name<<endl;
}
bracketStream.close();
//ask if want to create new bracket
//if yes clear the vector of the teams and
//then fill the vector with all the teams from the file (name still stored in c)
}
else //bracket doesn't exist
{
cout<<"Would you like to create this bracket (y/n): ";
cin>>createBracket;
if(createBracket == 'y')
{
generateBracket(bracketName);
}
}
break;
//New Bracket
case 2:
cout<<"What is the brackets name: ";
cin>>bracketName;
generateBracket(bracketName);
break;
}
menuChoice = 0;
menuChoice = menuOption(); // Displays menu after options complete
}
return 0;
}


///////////////////////////////
//Functions
///////////////////////////////
void delay(int i){
for(int temp=0; temp<i; temp++)
{
NULL;
}
}

//Creates a menu GUI and returns an int
int menuOption(){
int menuChoice = 0;
while(menuChoice != 1 && menuChoice != 2 && menuChoice != 3)
{
cout<<"\t\t Menu"<<endl;
cout<<"\t 1. Existing bracket"<<endl;
cout<<"\t 2. New Bracket"<<endl;
cout<<"\t 3. Exit"<<endl;
cout<<"Choice a number: ";
cin>>menuChoice;
}
return menuChoice;
}

//Check to see if the file to be loaded exist
//returns true if the file exist
bool fileExists(const char *fileName)
{
fstream infile(fileName);
return infile.good();
}

//generates a new bracket
//Does not load from a file
void generateBracket(string bracketName)
{
int numTeams =0;
string name;
string temp;
const char* matchup;
ofstream outStream;
ofstream matchupStream;
temp = bracketName;
temp.append("matchup.txt");
bracketName.append(".txt");
c = bracketName.c_str(); //string to char*
matchup = temp.c_str();

outStream.open(c); //open bracketName.txt
cout<<"How many teams are playing: ";
cin>>numTeams;
vector<team> teams2; //need to change to vector

cout<<"Enter team Names"<<endl;
for(int i=0; i<numTeams; i++)
{
teams2.push_back(team());
cout<<"Team "<<i+1<<" : ";
delay(40000);
cin>>name;
teams2[i].name = name;
outStream<<teams2[i].name<<endl;
}
cout<<endl;
outStream.close();
//for picking matches
int home;
int away ;

//////////////////
//setting thats no one has been placed
//////////////////////////////////////
matchupStream.open(matchup);
for(int i=0; i<numTeams; i++)
{
teams2[i].played=false;
}
//Accounting for a bye
if(numTeams%2==1)
{
int bye = rand()%numTeams;
teams2[bye].played=true;
cout<<"Team: "<<teams2[bye].name<<" will have a bye."<<endl;
matchupStream<<"Team: "<<teams2[bye].name<<" will have a bye."<<endl;
}


///////////////////////////////////////////
//generating games

for(int i=0; i<teams2.size(); i++)
{
home = rand()%numTeams;
away = rand()%numTeams;
///////////////////s//////////////////////////
//while the random number is the same
//or those choosen have already been placed
/////////////////////////////////////////////
while((home==away || teams2[home].played || teams2[away].played))
{
home = rand()%numTeams;
away = rand()%numTeams;
}

teams2[home].played= true;
teams2[away].played= true;

//run into the loop right after this
cout<<"Match "<<i+1<<"\n"<<teams2[away].name<<" vs "<<teams2[home].name<<"\n"<<endl;
matchupStream<<"Match "<<i+1<<"\n"<<teams2[away].name<<" vs "<<teams2[home].name<<"\n"<<endl;

}
matchupStream.close();
return;
}


I don't really know where it is coming from but allot of this code is kinda black magic to me I have just somehow managed to get it to work
First, welcome to the Forum.
To paste code here, put the code in code-tags. It helps us see where a new function definition starts and where the current one ends (seriously that's all it does). So in the future press that <> button

You are not getting an infinite loop in the sense of how infinite loop works i.e. never ending. This is just a guess, but this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
///////////////////////////////////////////
//generating games 

for(int i=0; i<teams2.size(); i++)
{
    home = rand()%numTeams;
    away = rand()%numTeams;

    ///////////////////s//////////////////////////
    //while the random number is the same
    //or those choosen have already been placed
    /////////////////////////////////////////////
    while((home==away || teams2[home].played || teams2[away].played))
    {
        home = rand()%numTeams;
        away = rand()%numTeams;
    }

    teams2[home].played= true;
    teams2[away].played= true;

    //run into the loop right after this
    cout<<"Match "<<i+1<<"\n"<<teams2[away].name<<" vs
         <<teams2[home].name<<"\n"<<endl;
    matchupStream<<"Match "<<i+1<<"\n"<<teams2[away].name<<" vs
                         <<teams2[home].name<<"\n"<<endl;
}
matchupStream.close();


This looks ready to infinitely run provided atleast one of the conditions of the while loop remains true. And it is even more likely to be the case because of these lines:

1
2
teams2[home].played= true;
teams2[away].played= true;


Dude you have an infinite loop, only it is a nice one because it won't crash your program but it will still run infinitely provided numTeams is large enough
Last edited on
That was it! thank I forgot to put a stopping condition and that for() statement was wrong I needed to figure out the number of matches and use that in the for(), as well as the while() and it works now.
Topic archived. No new replies allowed.