943,733 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 342
  • C++ RSS
Nov 29th, 2008
-1

Cant display anything from this program

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cctype>
  4. #include <string>
  5. #include <list>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. class Game
  11. {
  12. public : Game();
  13. int getGoalScored();
  14. int getGoalsLetIn();
  15. void getName(string[]);
  16. void setGoalScored(int);
  17. void setGoalsLetIn(int);
  18. void setName(string[]);
  19.  
  20. private : int goalScored;
  21. int goalsLetIn;
  22. string name[15];
  23.  
  24. };//end game
  25.  
  26. Game::Game()
  27. {
  28. goalScored = 0;
  29. goalsLetIn = 0;
  30. for (int i = 0; i < 15; i++)
  31. {
  32. name[i] = "";
  33. }
  34. }//end Game
  35.  
  36. int Game::getGoalScored()
  37. {
  38. return goalScored;
  39. }//end getGoalScored
  40.  
  41. int Game::getGoalsLetIn()
  42. {
  43. return goalsLetIn;
  44. }//end getGoalScored
  45.  
  46. void Game::getName(string scoreName[])
  47. {
  48. for(int i=0;i<goalScored;i++)
  49. {
  50. scoreName[i]=name[i];
  51. }
  52. }//end getName
  53.  
  54. void Game::setGoalScored(int score)
  55. {
  56. goalScored = score;
  57. }//end setGoalScored
  58.  
  59. void Game::setGoalsLetIn(int conceded)
  60. {
  61. goalsLetIn = conceded;
  62. }//end setGoalsLetIn
  63.  
  64. void Game::setName(string myName[])
  65. {
  66. for(int i = 0; i < goalScored; i++)
  67. {
  68. name[i] = myName[i];
  69. }
  70. }
  71.  
  72. class Player
  73. {
  74. public : Player();
  75. string getName();
  76. int getAge();
  77. string getPosition();
  78. int getSalary();
  79. int getBonus();
  80. int getGoal();
  81. void setName(string);
  82. void setAge(int);
  83. void setPos(string);
  84. void setSalary(int);
  85. void setBonus(int);
  86. void setGoal(int);
  87.  
  88.  
  89. private : string name;
  90. int age;
  91. string position;
  92. int salary;
  93. int bonus;
  94. int goal;
  95. };
  96.  
  97. Player::Player()
  98. {
  99. name = "";
  100. age = 0;
  101. position = "";
  102. salary = 0;
  103. bonus = 0;
  104. goal = 0;
  105. }//end Player
  106.  
  107. string Player::getName()
  108. {
  109. return name;
  110. }//end getName
  111.  
  112. int Player::getAge()
  113. {
  114. return age;
  115. }//end getAge
  116.  
  117.  
  118. string Player::getPosition()
  119. {
  120. return position;
  121. }
  122.  
  123. int Player::getSalary()
  124. {
  125. return salary;
  126. }
  127.  
  128. int Player::getBonus()
  129. {
  130. return bonus;
  131. }
  132.  
  133. int Player::getGoal()
  134. {
  135. return goal;
  136. }
  137.  
  138. void Player::setName(string PlayerName)
  139. {
  140. name = PlayerName;
  141. }
  142.  
  143. void Player::setAge(int myAge)
  144. {
  145. age = myAge;
  146. }
  147.  
  148. void Player::setPos(string myPosition)
  149. {
  150. position = myPosition;
  151. }
  152.  
  153. void Player::setSalary(int mySalary)
  154. {
  155. salary = mySalary;
  156. }
  157.  
  158. void Player::setBonus(int myBonus)
  159. {
  160. bonus = myBonus;
  161. }
  162.  
  163. void Player::setGoal(int myGoal)
  164. {
  165. goal = myGoal;
  166. }
  167.  
  168. //function declaration
  169. bool loadPlayerFile(list <Player>& myPlayer);
  170. bool loadGameFile(list <Game>& myGame);
  171. bool checkInt(string & num);
  172. bool checkPosition(string myPosition);
  173. void displayPlayers(list<Player>& myPlayer);
  174. void displayGame(list <Game>& myGame);
  175. int totalGames(list<Game>& myGame);
  176. int gameResults(list<Game>& myGame, int &won,int &lost,int &draw);
  177.  
  178. //main
  179. int main()
  180. {
  181. list <Game> myGame;
  182. list <Player> myPlayer;
  183.  
  184. if (!loadGameFile(myGame))
  185. {
  186. exit(0);
  187. }//end if
  188. if (!loadPlayerFile(myPlayer))
  189. {
  190. exit(0);
  191. }//end if
  192.  
  193. displayGame(myGame);
  194. displayPlayers(myPlayer);
  195.  
  196. system("pause");
  197. return 0;
  198. }//end main
  199.  
  200. //function loadPlayerFile
  201. bool loadPlayerFile(list <Player> &myPlayer)
  202. {
  203. ifstream fin;
  204. string name;
  205. string age;
  206. string position;
  207. string salary;
  208. list<Player>::iterator p;
  209. Player A;
  210.  
  211. p = myPlayer.begin();
  212. fin.open("players.txt");
  213. if (!fin.good())
  214. {
  215. cout << "File not found" << endl;
  216. return false;
  217. }
  218.  
  219. while(!fin.eof())
  220. {
  221. fin >> name >> age >> position >> salary;
  222.  
  223. //convert all position to lower case first.
  224. for (int i=0;i<position.size();i++)
  225. {
  226. position[i] = tolower(position[i]);
  227. }
  228. if(checkInt(age) && checkPosition(position) && checkInt(salary))
  229. {
  230. A.setName(name);
  231. A.setAge(atoi(age.c_str()));
  232. A.setPos(position);
  233. A.setSalary(atoi(salary.c_str()));
  234. myPlayer.insert(p, A);
  235. p++;
  236. }
  237. }//end while
  238. return true;
  239. }//emd loadPlayerFile
  240.  
  241. //function loadGameFile
  242. bool loadGameFile(list <Game>& myGame)
  243. {
  244. ifstream fin;
  245. string unwantedInfo1;
  246. int scored;
  247. string unwantedInfo2;
  248. int scoredAgainst;
  249. string scorer[15];
  250. list<Game>::iterator p;
  251. Game A;
  252.  
  253. p = myGame.begin();
  254.  
  255. fin.open("games.txt");
  256.  
  257. if (!fin.good())
  258. {
  259. cout << "File not found" << endl;
  260. return false;
  261. }//end if
  262.  
  263. while(!fin.eof())
  264. {
  265. fin >> unwantedInfo1 >> scored >> unwantedInfo2 >> scoredAgainst;
  266. A.setGoalScored(scored);
  267. A.setGoalsLetIn(scoredAgainst);
  268. for(int i=0; i<scored;i++)
  269. {
  270. fin >> scorer[i];
  271. }
  272. A.setName(scorer);
  273.  
  274. myGame.insert(p,A);
  275. p++;
  276. }//end while
  277. return true;
  278. }//end loadGameFile
  279.  
  280. //function checkInt
  281. bool checkInt(string & num)
  282. {
  283. for(int i=0;i<num.size();i++)
  284. {
  285. if(!isdigit(num[i]))
  286. {
  287. return false;
  288. }
  289. }
  290.  
  291. return true;
  292. }//end check Int
  293.  
  294. //function checkPosition
  295. bool checkPosition(string myPosition)
  296. {
  297. return (myPosition == "goalkeeper" || myPosition == "defender" || myPosition == "striker" || myPosition == "midfielder");
  298. }//end check Position
  299.  
  300. //function displayPlayers
  301. void displayPlayers(list<Player>& myPlayer)
  302. {
  303. list<Player>::iterator p;
  304.  
  305. cout<<"Name\tAge\tPosition\tBasic Salary\tBonus\tTotal Income"<<endl;
  306.  
  307. p = myPlayer.begin();
  308.  
  309. while(p!=myPlayer.end())
  310. {
  311. cout << p -> getName() << "\t" << p-> getAge() << "\t" << p -> getPosition()<<"\t$" <<p-> getSalary()<<endl;
  312. p++;
  313. }//end while
  314. }//end displayPlayers
Last edited by Ancient Dragon; Nov 29th, 2008 at 11:00 am. Reason: replaced quote tags with code tags
Similar Threads
Reputation Points: 6
Solved Threads: 0
Light Poster
AcidG3rm5 is offline Offline
25 posts
since Oct 2008
Nov 29th, 2008
0

Re: Cant display anything from this program

>> 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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Nov 29th, 2008
0

Re: Cant display anything from this program

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.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cctype>
  4. #include <string>
  5. #include <list>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. class Game
  11. {
  12. public : Game();
  13. int getGoalScored();
  14. int getGoalsLetIn();
  15. void getName(string[]);
  16. void setGoalScored(int);
  17. void setGoalsLetIn(int);
  18. void setName(string[]);
  19.  
  20. private : int goalScored;
  21. int goalsLetIn;
  22. string name[15];
  23.  
  24. };//end game
  25.  
  26. Game::Game()
  27. {
  28. goalScored = 0;
  29. goalsLetIn = 0;
  30. for (int i = 0; i < 15; i++)
  31. {
  32. name[i] = "";
  33. }
  34. }//end Game
  35.  
  36. int Game::getGoalScored()
  37. {
  38. return goalScored;
  39. }//end getGoalScored
  40.  
  41. int Game::getGoalsLetIn()
  42. {
  43. return goalsLetIn;
  44. }//end getGoalScored
  45.  
  46. void Game::getName(string scoreName[])
  47. {
  48. for(int i=0;i<goalScored;i++)
  49. {
  50. scoreName[i]=name[i];
  51. }
  52. }//end getName
  53.  
  54. void Game::setGoalScored(int score)
  55. {
  56. goalScored = score;
  57. }//end setGoalScored
  58.  
  59. void Game::setGoalsLetIn(int conceded)
  60. {
  61. goalsLetIn = conceded;
  62. }//end setGoalsLetIn
  63.  
  64. void Game::setName(string myName[])
  65. {
  66. for(int i = 0; i < goalScored; i++)
  67. {
  68. name[i] = myName[i];
  69. }
  70. }
  71.  
  72. class Player
  73. {
  74. public : Player();
  75. string getName();
  76. int getAge();
  77. string getPosition();
  78. int getSalary();
  79. int getBonus();
  80. int getGoal();
  81. void setName(string);
  82. void setAge(int);
  83. void setPos(string);
  84. void setSalary(int);
  85. void setBonus(int);
  86. void setGoal(int);
  87.  
  88.  
  89. private : string name;
  90. int age;
  91. string position;
  92. int salary;
  93. int bonus;
  94. int goal;
  95. };
  96.  
  97. Player::Player()
  98. {
  99. name = "";
  100. age = 0;
  101. position = "";
  102. salary = 0;
  103. bonus = 0;
  104. goal = 0;
  105. }//end Player
  106.  
  107. string Player::getName()
  108. {
  109. return name;
  110. }//end getName
  111.  
  112. int Player::getAge()
  113. {
  114. return age;
  115. }//end getAge
  116.  
  117.  
  118. string Player::getPosition()
  119. {
  120. return position;
  121. }
  122.  
  123. int Player::getSalary()
  124. {
  125. return salary;
  126. }
  127.  
  128. int Player::getBonus()
  129. {
  130. return bonus;
  131. }
  132.  
  133. int Player::getGoal()
  134. {
  135. return goal;
  136. }
  137.  
  138. void Player::setName(string PlayerName)
  139. {
  140. name = PlayerName;
  141. }
  142.  
  143. void Player::setAge(int myAge)
  144. {
  145. age = myAge;
  146. }
  147.  
  148. void Player::setPos(string myPosition)
  149. {
  150. position = myPosition;
  151. }
  152.  
  153. void Player::setSalary(int mySalary)
  154. {
  155. salary = mySalary;
  156. }
  157.  
  158. void Player::setBonus(int myBonus)
  159. {
  160. bonus = myBonus;
  161. }
  162.  
  163. void Player::setGoal(int myGoal)
  164. {
  165. goal = myGoal;
  166. }
  167.  
  168. //function declaration
  169. bool loadPlayerFile(list <Player>& myPlayer);
  170. bool loadGameFile(list <Game>& myGame);
  171. bool checkInt(string & num);
  172. bool checkPosition(string myPosition);
  173. void displayPlayers(list<Player>& myPlayer);
  174. void displayGame(list <Game>& myGame);
  175. int totalGames(list<Game>& myGame);
  176. int gameResults(list<Game>& myGame, int &won,int &lost,int &draw);
  177.  
  178. //main
  179. int main()
  180. {
  181. list <Game> myGame;
  182. list <Player> myPlayer;
  183.  
  184. if (!loadPlayerFile(myPlayer))
  185. {
  186. exit(0);
  187. }//end if
  188.  
  189. /*
  190.   if (!loadGameFile(myGame))
  191. {
  192.   exit(0);
  193.   }//end if
  194. */
  195. displayGame(myGame);
  196. displayPlayers(myPlayer);
  197.  
  198. system("pause");
  199. return 0;
  200. }//end main
  201.  
  202. //function loadPlayerFile
  203. bool loadPlayerFile(list <Player> &myPlayer)
  204. {
  205. ifstream fin;
  206. string name;
  207. string age;
  208. string position;
  209. string salary;
  210. list<Player>::iterator p;
  211. Player A;
  212.  
  213. p = myPlayer.begin();
  214. fin.open("players.txt");
  215. if (!fin.good())
  216. {
  217. cout << "File not found" << endl;
  218. return false;
  219. }
  220.  
  221. while(!fin.eof())
  222. {
  223. fin >> name >> age >> position >> salary;
  224.  
  225. //convert all position to lower case first.
  226. for (int i=0;i<position.size();i++)
  227. {
  228. position[i] = tolower(position[i]);
  229. }
  230. if(checkInt(age) && checkPosition(position) && checkInt(salary))
  231. {
  232. A.setName(name);
  233. A.setAge(atoi(age.c_str()));
  234. A.setPos(position);
  235. A.setSalary(atoi(salary.c_str()));
  236. myPlayer.insert(p, A);
  237. p++;
  238. }
  239. }//end while
  240. return true;
  241. }//emd loadPlayerFile
  242.  
  243. //function loadGameFile
  244. bool loadGameFile(list <Game>& myGame)
  245. {
  246. ifstream fin;
  247. string unwantedInfo1;
  248. int scored;
  249. string unwantedInfo2;
  250. int scoredAgainst;
  251. string scorer[15];
  252. list<Game>::iterator p;
  253. Game A;
  254.  
  255. p = myGame.begin();
  256.  
  257. fin.open("games.txt");
  258.  
  259. if (!fin.good())
  260. {
  261. cout << "File not found" << endl;
  262. return false;
  263. }//end if
  264.  
  265.  
  266. while(!fin.eof())
  267. {
  268. fin >> unwantedInfo1 >> scored >> unwantedInfo2 >> scoredAgainst;
  269. A.setGoalScored(scored);
  270. A.setGoalsLetIn(scoredAgainst);
  271. for(int i=0; i<scored;i++)
  272. {
  273. fin >> scorer[i];
  274. }
  275. A.setName(scorer);
  276.  
  277. myGame.insert(p,A);
  278. p++;
  279. }//end while
  280. return true;
  281. }//end loadGameFile
  282.  
  283. //function checkInt
  284. bool checkInt(string & num)
  285. {
  286. for(int i=0;i<num.size();i++)
  287. {
  288. if(!isdigit(num[i]))
  289. {
  290. return false;
  291. }
  292. }
  293.  
  294. return true;
  295. }//end check Int
  296.  
  297. //function checkPosition
  298. bool checkPosition(string myPosition)
  299. {
  300. return (myPosition == "goalkeeper" || myPosition == "defender" || myPosition == "striker" || myPosition == "midfielder");
  301. }//end check Position
  302.  
  303. //function displayPlayers
  304. void displayPlayers(list<Player>& myPlayer)
  305. {
  306. list<Player>::iterator p;
  307.  
  308. cout<<"Name\tAge\tPosition\tBasic Salary\tBonus\tTotal Income"<<endl;
  309.  
  310. p = myPlayer.begin();
  311.  
  312. while(p!=myPlayer.end())
  313. {
  314. cout << p -> getName() << "\t" << p-> getAge() << "\t" << p -> getPosition()<<"\t$" <<p-> getSalary()<<endl;
  315. p++;
  316. }//end while
  317. }//end displayPlayers
  318.  
  319.  
  320. //function displayGame
  321. void displayGame(list <Game>& myGame)
  322. {
  323. int won,lost,draw;
  324. gameResults(myGame,won,lost,draw);
  325. cout<<"*** My Team's Performance ***"<<endl;
  326. cout<<"Number Of Games Played: "<<totalGames(myGame)<<endl;
  327. cout<<"Number Of Games won: "<<won<<endl;
  328. cout<<"Number Of Games drawn: "<<draw<<endl;
  329. cout<<"Numer Of Games lost: "<<lost<<endl;
  330. cout<<"Number of Goals scored: "<<endl;
  331. cout<<"Number of goals conceded: "<<endl;
  332. cout<<"Top scorer: "<<endl;
  333. cout<<"Number of points collected: "<<endl;
  334.  
  335. }//end function displayGame
  336.  
  337. //function totalGames
  338. int totalGames(list<Game>& myGame)
  339. {
  340. list<Game>::iterator p = myGame.begin();
  341. int num = 0;
  342.  
  343. while (p != myGame.end())
  344. {
  345. num++;
  346. p++;
  347. }
  348. return num;
  349. }//end function total Games
  350.  
  351. //function gameResults
  352. int gameResults(list<Game>& myGame, int &won,int &lost,int &draw)
  353. {
  354. list<Game>::iterator p = myGame.begin();
  355. won=0;
  356. lost=0;
  357. draw=0;
  358.  
  359. while (p!=myGame.end())
  360. {
  361. if (p -> getGoalScored() > p -> getGoalsLetIn())
  362. {
  363. won++;
  364. }
  365.  
  366. else if (p -> getGoalScored() == p -> getGoalsLetIn())
  367. {
  368. draw++;
  369. }
  370.  
  371. else if (p -> getGoalScored() < p -> getGoalsLetIn())
  372. {
  373. lost++;
  374. }
  375. }
  376. }//end function gameResults

in the main function if i took out this :

C++ Syntax (Toggle Plain Text)
  1. if (!loadGameFile(myGame))
  2. {
  3. exit(0);
  4. }//end if

and the display comes out. Any idea why?
Last edited by Ancient Dragon; Nov 29th, 2008 at 1:29 pm. Reason: removed spaces within code tags
Reputation Points: 6
Solved Threads: 0
Light Poster
AcidG3rm5 is offline Offline
25 posts
since Oct 2008
Nov 29th, 2008
0

Re: Cant display anything from this program

Looks like loadGameFile() is failing to open the file games.txt
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: what is a reference parameter and how would i go about this code?
Next Thread in C++ Forum Timeline: help on homework





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC