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