RSS Forums RSS
Please support our C++ advertiser: Programming Forums

making a simple c++ game

Join Date: Dec 2005
Posts: 4,215
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 26
Solved Threads: 493
Colleague
Salem's Avatar
Salem Salem is offline Offline
Industrious Poster

Re: making a simple c++ game

  #3  
Jul 23rd, 2008
Well adopting a consistent approach to indentation will help, not some chaotic mix you have at the moment.
Eg.
  1. #include <string>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5. std::string word[5];
  6.  
  7. int main ()
  8. {
  9. //Declaring Variables
  10. ifstream inFile;
  11. char choice;
  12. int secret_number;
  13. int user_number;
  14.  
  15.  
  16.  
  17. // opening files
  18. inFile.open("secret number.txt");
  19.  
  20. //menu
  21. do{
  22. cout << "A: To Play the number guessing game." << endl;
  23. cout << "B: To Play the letter game." << endl;
  24. cout << "C: To quit program." << endl;
  25. cin >> choice;
  26. while (choice != 'c' && choice != 'C');
  27. switch (choice)
  28. {
  29. case 'A':
  30. case 'a':
  31. cout << "Welcome to the Guess the Number Game." << endl;
  32. cout << "\n I am thinking of a number between 1 and 100." << endl;
  33.  
  34. // Loops forever until user finds the number
  35.  
  36. while (user_number != secret_number)
  37. {
  38. cout<<"Pick a number : ";
  39. cin >> user_number;
  40. inFile >> secret_number;
  41.  
  42. if (secret_number > user_number)
  43. cout << "Higher!"<<endl<<endl;
  44.  
  45. else if (secret_number < user_number)
  46. cout << "Lower!"<<endl<<endl;
  47.  
  48. else
  49. cout << "Good job! You found the number!"<<endl<<endl;
  50. }
  51. }
  52. break;
  53. }
  54. }
It's immediately obvious that the break; on line 52 isn't part of the switch/case.

Also, the do at line 21, which ends at 53 is missing a condition on line 53.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 8:33 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC