Hi all. I was writing my program. And halfway through, i tried to compile and run. but got a blank output.

I tried doing a cout<<"hello"<<endl at the main() must still didn't get any display. could anyone please help me to see what went wrong?

#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <list>       
#include <algorithm> 

using namespace std;

class Game
{
      public : Game();
               int getGoalScored();
               int getGoalsLetIn();
	           void getName(string[]);
	           void setGoalScored(int);
	           void setGoalsLetIn(int);
	           void setName(string[]);
             
      private : int goalScored;
                int goalsLetIn;
                string name[15];
              
};//end game

Game::Game()
{
	goalScored = 0;
	goalsLetIn = 0;
	for (int i = 0; i < 15; i++)
	{	
      name[i] = "";          
    } 
}//end Game

int Game::getGoalScored()
{
 return goalScored;
}//end getGoalScored

int Game::getGoalsLetIn()
{
 return goalsLetIn;
}//end getGoalScored

void Game::getName(string scoreName[])
{
 for(int i=0;i<goalScored;i++)
 {
  scoreName[i]=name[i];
 }
}//end getName

void Game::setGoalScored(int score)
{
	goalScored = score;
}//end setGoalScored

void Game::setGoalsLetIn(int conceded)
{
	goalsLetIn = conceded;
}//end setGoalsLetIn

void Game::setName(string myName[])
{
	for(int i = 0; i < goalScored; i++)
	{
		name[i] = myName[i];
    }
}

class Player
{
     public : Player();
              string getName();
              int getAge();
              string getPosition();
              int getSalary();
              int getBonus();
              int getGoal();
              void setName(string);
              void setAge(int);
              void setPos(string);
              void setSalary(int);
              void setBonus(int);
              void setGoal(int);
              
            
     private : string name;
               int age;
               string position;
               int salary;
               int bonus;
               int goal;
};

Player::Player()
{
 	name = "";
	age = 0;
	position = "";
	salary = 0;
	bonus = 0;
	goal = 0;
}//end Player

string Player::getName()
{
 return name;
}//end getName

int Player::getAge()
{
 return age;
}//end getAge


string Player::getPosition()
{
 return position;
}

int Player::getSalary()
{
 return salary;
}

int Player::getBonus()
{
 return bonus;
}

int Player::getGoal()
{
 return goal;
}

void Player::setName(string PlayerName)
{
 name = PlayerName;
}
 
void Player::setAge(int myAge)
{
 age = myAge;
}

void Player::setPos(string myPosition)
{
 position = myPosition;
}

void Player::setSalary(int mySalary)
{
 salary = mySalary;
}
             
void Player::setBonus(int myBonus)
{
 bonus = myBonus;
}
              
void Player::setGoal(int myGoal)
{
 goal = myGoal;
}

//function declaration
bool loadPlayerFile(list <Player>& myPlayer);
bool loadGameFile(list <Game>& myGame);
bool checkInt(string & num);
bool checkPosition(string  myPosition);
void displayPlayers(list<Player>& myPlayer);
void displayGame(list <Game>& myGame);
int totalGames(list<Game>& myGame);
int gameResults(list<Game>& myGame, int &won,int &lost,int &draw);

//main
int main()
{
    list <Game> myGame;
    list <Player> myPlayer;
   	
    if (!loadGameFile(myGame))
	{
    	exit(0);
     }//end if
	if (!loadPlayerFile(myPlayer))
	{
       exit(0);
    }//end if
    
    displayGame(myGame);
    displayPlayers(myPlayer);
    
    system("pause");
	return 0;
}//end main

//function loadPlayerFile
bool loadPlayerFile(list <Player> &myPlayer)
{
    ifstream fin;
	string name;
	string age;
	string position;
	string salary;
	list<Player>::iterator p;
	Player A;
	
	p = myPlayer.begin();
	fin.open("players.txt");
    if (!fin.good())
    {
        cout << "File not found" << endl;
		return false;
    }
	
	while(!fin.eof())
	{
     fin >> name >> age >> position >> salary;
     
     //convert all position to lower case first.
     for (int i=0;i<position.size();i++)
      {
        position[i] = tolower(position[i]);
      }
     if(checkInt(age) && checkPosition(position) && checkInt(salary))
     {
      A.setName(name);
      A.setAge(atoi(age.c_str()));
      A.setPos(position);
	  A.setSalary(atoi(salary.c_str()));
	  myPlayer.insert(p, A);
	  p++;
     }
    }//end while
    return true;
}//emd loadPlayerFile

//function loadGameFile
bool loadGameFile(list <Game>& myGame)
{
     ifstream fin;
     string unwantedInfo1;
     int scored;
     string unwantedInfo2;
     int scoredAgainst;
     string scorer[15];
     list<Game>::iterator p;
     Game A;
     
     p = myGame.begin();
     
     fin.open("games.txt");
    
    if (!fin.good())
    {
        cout << "File not found" << endl;
		return false;
    }//end if     
    
    while(!fin.eof())
    {
     fin >> unwantedInfo1 >> scored >> unwantedInfo2 >> scoredAgainst;
     A.setGoalScored(scored);
     A.setGoalsLetIn(scoredAgainst);
     for(int i=0; i<scored;i++)
     {
      fin >> scorer[i];
     }
     A.setName(scorer);
     
     myGame.insert(p,A);
     p++;
    }//end while
    return true;
}//end loadGameFile

//function checkInt
bool checkInt(string & num)
{
 for(int i=0;i<num.size();i++)
 {
  if(!isdigit(num[i]))
  {
   return false;
  }
 }
 
 return true;
}//end check Int

//function checkPosition
bool checkPosition(string myPosition)
{
 return (myPosition == "goalkeeper" || myPosition == "defender" || myPosition == "striker" || myPosition == "midfielder");
}//end check Position

//function displayPlayers
void displayPlayers(list<Player>& myPlayer)
{
 list<Player>::iterator p;
 
 cout<<"Name\tAge\tPosition\tBasic Salary\tBonus\tTotal Income"<<endl;
 
 p = myPlayer.begin();
 
 while(p!=myPlayer.end())
 {
  cout << p -> getName() << "\t" << p-> getAge() << "\t" << p -> getPosition()<<"\t$" <<p-> getSalary()<<endl;
  p++;
 }//end while
}//end displayPlayers
Salem commented: unindented garbage with smilies -4

Recommended Answers

All 3 Replies

>> And halfway through, i tried to compile and run
You need to compile and fix errors a lot sooner than that!

>> void displayGame(list <Game>& myGame);

You have not coded that function.

oppss. realize i didn't quote the whole program. Anyway, before i wrote the displayGame(myGame) had tested the program and it works.
I have found where the error lies which is preventing the display from showing.

#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <list>       
#include <algorithm> 

using namespace std;

class Game
{
      public : Game();
               int getGoalScored();
               int getGoalsLetIn();
	           void getName(string[]);
	           void setGoalScored(int);
	           void setGoalsLetIn(int);
	           void setName(string[]);
             
      private : int goalScored;
                int goalsLetIn;
                string name[15];
              
};//end game

Game::Game()
{
	goalScored = 0;
	goalsLetIn = 0;
	for (int i = 0; i < 15; i++)
	{	
      name[i] = "";          
    } 
}//end Game

int Game::getGoalScored()
{
 return goalScored;
}//end getGoalScored

int Game::getGoalsLetIn()
{
 return goalsLetIn;
}//end getGoalScored

void Game::getName(string scoreName[])
{
 for(int i=0;i<goalScored;i++)
 {
  scoreName[i]=name[i];
 }
}//end getName

void Game::setGoalScored(int score)
{
	goalScored = score;
}//end setGoalScored

void Game::setGoalsLetIn(int conceded)
{
	goalsLetIn = conceded;
}//end setGoalsLetIn

void Game::setName(string myName[])
{
	for(int i = 0; i < goalScored; i++)
	{
		name[i] = myName[i];
    }
}

class Player
{
     public : Player();
              string getName();
              int getAge();
              string getPosition();
              int getSalary();
              int getBonus();
              int getGoal();
              void setName(string);
              void setAge(int);
              void setPos(string);
              void setSalary(int);
              void setBonus(int);
              void setGoal(int);
              
            
     private : string name;
               int age;
               string position;
               int salary;
               int bonus;
               int goal;
};

Player::Player()
{
 	name = "";
	age = 0;
	position = "";
	salary = 0;
	bonus = 0;
	goal = 0;
}//end Player

string Player::getName()
{
 return name;
}//end getName

int Player::getAge()
{
 return age;
}//end getAge


string Player::getPosition()
{
 return position;
}

int Player::getSalary()
{
 return salary;
}

int Player::getBonus()
{
 return bonus;
}

int Player::getGoal()
{
 return goal;
}

void Player::setName(string PlayerName)
{
 name = PlayerName;
}
 
void Player::setAge(int myAge)
{
 age = myAge;
}

void Player::setPos(string myPosition)
{
 position = myPosition;
}

void Player::setSalary(int mySalary)
{
 salary = mySalary;
}
             
void Player::setBonus(int myBonus)
{
 bonus = myBonus;
}
              
void Player::setGoal(int myGoal)
{
 goal = myGoal;
}

//function declaration
bool loadPlayerFile(list <Player>& myPlayer);
bool loadGameFile(list <Game>& myGame);
bool checkInt(string & num);
bool checkPosition(string  myPosition);
void displayPlayers(list<Player>& myPlayer);
void displayGame(list <Game>& myGame);
int totalGames(list<Game>& myGame);
int gameResults(list<Game>& myGame, int &won,int &lost,int &draw);

//main
int main()
{
    list <Game> myGame;
    list <Player> myPlayer;
   	
	if (!loadPlayerFile(myPlayer))
	{
       exit(0);
    }//end if
    
/*
    if (!loadGameFile(myGame))
	{
    	exit(0);
     }//end if
*/
    displayGame(myGame);
    displayPlayers(myPlayer);
    
    system("pause");
	return 0;
}//end main

//function loadPlayerFile
bool loadPlayerFile(list <Player> &myPlayer)
{
    ifstream fin;
	string name;
	string age;
	string position;
	string salary;
	list<Player>::iterator p;
	Player A;
	
	p = myPlayer.begin();
	fin.open("players.txt");
    if (!fin.good())
    {
        cout << "File not found" << endl;
		return false;
    }
	
	while(!fin.eof())
	{
     fin >> name >> age >> position >> salary;
     
     //convert all position to lower case first.
     for (int i=0;i<position.size();i++)
      {
        position[i] = tolower(position[i]);
      }
     if(checkInt(age) && checkPosition(position) && checkInt(salary))
     {
      A.setName(name);
      A.setAge(atoi(age.c_str()));
      A.setPos(position);
	  A.setSalary(atoi(salary.c_str()));
	  myPlayer.insert(p, A);
	  p++;
     }
    }//end while
    return true;
}//emd loadPlayerFile

//function loadGameFile
bool loadGameFile(list <Game>& myGame)
{
     ifstream fin;
     string unwantedInfo1;
     int scored;
     string unwantedInfo2;
     int scoredAgainst;
     string scorer[15];
     list<Game>::iterator p;
     Game A;
     
     p = myGame.begin();
     
     fin.open("games.txt");
    
    if (!fin.good())
    {
        cout << "File not found" << endl;
		return false;
    }//end if     
    
    
    while(!fin.eof())
    {
     fin >> unwantedInfo1 >> scored >> unwantedInfo2 >> scoredAgainst;
     A.setGoalScored(scored);
     A.setGoalsLetIn(scoredAgainst);
     for(int i=0; i<scored;i++)
     {
      fin >> scorer[i];
     }
     A.setName(scorer);
     
     myGame.insert(p,A);
     p++;
    }//end while
    return true;
}//end loadGameFile

//function checkInt
bool checkInt(string & num)
{
 for(int i=0;i<num.size();i++)
 {
  if(!isdigit(num[i]))
  {
   return false;
  }
 }
 
 return true;
}//end check Int

//function checkPosition
bool checkPosition(string myPosition)
{
 return (myPosition == "goalkeeper" || myPosition == "defender" || myPosition == "striker" || myPosition == "midfielder");
}//end check Position

//function displayPlayers
void displayPlayers(list<Player>& myPlayer)
{
 list<Player>::iterator p;
 
 cout<<"Name\tAge\tPosition\tBasic Salary\tBonus\tTotal Income"<<endl;
 
 p = myPlayer.begin();
 
 while(p!=myPlayer.end())
 {
  cout << p -> getName() << "\t" << p-> getAge() << "\t" << p -> getPosition()<<"\t$" <<p-> getSalary()<<endl;
  p++;
 }//end while
}//end displayPlayers


//function displayGame
void displayGame(list <Game>& myGame)
{
 int won,lost,draw;
 gameResults(myGame,won,lost,draw);
 cout<<"*** My Team's Performance ***"<<endl;
 cout<<"Number Of Games Played: "<<totalGames(myGame)<<endl;
 cout<<"Number Of Games won: "<<won<<endl;
 cout<<"Number Of Games drawn: "<<draw<<endl;
 cout<<"Numer Of Games lost: "<<lost<<endl;
 cout<<"Number of Goals scored: "<<endl;
 cout<<"Number of goals conceded: "<<endl;
 cout<<"Top scorer: "<<endl;
 cout<<"Number of points collected: "<<endl;
 
}//end function displayGame

//function totalGames
int totalGames(list<Game>& myGame)
{
	list<Game>::iterator p = myGame.begin();
	int num = 0;

	while (p != myGame.end())
	{
		num++;
		p++;
	}
	return num;
}//end function total Games

//function gameResults
int gameResults(list<Game>& myGame, int &won,int &lost,int &draw)
{
 list<Game>::iterator p = myGame.begin();
 won=0;
 lost=0;
 draw=0;
 
 while (p!=myGame.end())
 {
  if (p -> getGoalScored() > p -> getGoalsLetIn())
  {
   won++;
  }
  
  else if (p -> getGoalScored() == p -> getGoalsLetIn())
  {
   draw++;
  }
  
  else if (p -> getGoalScored() < p -> getGoalsLetIn())
  {
   lost++; 
  }
 }
}//end function gameResults

in the main function if i took out this :

if (!loadGameFile(myGame))
	{
    	exit(0);
     }//end if

and the display comes out. Any idea why?

Looks like loadGameFile() is failing to open the file games.txt

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.