User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,935 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,893 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Oct 20th, 2005
Views: 2,255
Ok I greatly modified a tutorial to have a completely different interface, a highscore list and a little tiny really rare easter egg.
to get the highscore list. I want to figure out how to sort the highscore list if it is possible. And dont worry about it freaking out, if you type a letter instead of a number it will yell at you basically, it will stop the program and tell you what the random number was and have you quit the program. If you dont guess the number in time your name is not added to the list. (Dev-C++)
cplusplus Syntax | 4 stars
  1. #include <iostream.h>//required files
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <fstream.h>
  5. // Define the greatest possible value:
  6. #define MAX_RANGE 1000
  7. main ()
  8. {
  9. system("color 0E");
  10. system("title Guessing Game");
  11. char highscore;
  12. char name[20];
  13. int counter=0;
  14. long value,input;
  15. srand ( time (NULL) ); // Initialize random generator
  16. value = rand()%MAX_RANGE+1; // Get random between 1 and MAX_RANGE
  17. cout<<" You have 10 tries to guess the correct number\n";
  18. cout<<"Please enter your name:";
  19. cin>> name;
  20. if (value==666){
  21. system("color 4C");
  22. cout<<"Satan has compelled you to guess the correct number"<< endl;}
  23. cout << "\nEnter a number between 1 and " << MAX_RANGE << " : \n";
  24. do {
  25. cin >> input; // Get user input
  26. counter++; // increase attempts counter
  27. if (value>input) // is value grater than the input?
  28. cout << "Value is greater than " << input << ". Trys left ["<< 10-counter <<"]";
  29. else if (value<input) // if not, is it less?
  30. cout << "Value is less than " << input << ". Trys left ["<< 10-counter <<"]";
  31. else { // else it is the correct number!
  32. cout << "That's right! number was " << input;
  33. cout << "\nYou have needed " << counter <<" trys.\n";
  34. system("PAUSE");
  35. ofstream examplefile ("highscore.txt", ios::ate | ios::app);
  36. if (examplefile.is_open())
  37. {
  38. examplefile << ""<< name <<"--------"<< counter <<"\n";
  39. examplefile.close(); }}
  40. if(counter==10 && input==value){
  41. goto bah;
  42. }
  43. if (counter==10)
  44. { system("cls");
  45. cout<<"Sorry, You failed to guess the number in time\n";
  46. cout<<"The correct value was '"<< value <<"'\n";
  47. system("PAUSE");
  48. return 0;}
  49. } while (value!=input);
  50. bah: cout<<"Would you like to see the highscore list? [Y][N]"<< endl;
  51. cin>> highscore;
  52. if (highscore=='y'){
  53. system("cls");
  54. char buffer[256];
  55. ifstream examplefile ("highscore.txt");
  56. if (! examplefile.is_open()) { cout << "Error opening file"; exit (1); }
  57. while (! examplefile.eof() ){
  58. examplefile.getline (buffer,100);
  59. cout << buffer <<endl;}}
  60. else {cout<<"Thanks for playing!\n";
  61. system("PAUSE");
  62. return 0;}
  63. system("PAUSE");
  64. return 0;}
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:30 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC