Guessing Game

ShawnCplus 0 Tallied Votes 163 Views Share

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++)

#include <iostream.h>//required files
#include <stdlib.h>
#include <time.h>
#include <fstream.h>
// Define the greatest possible value:
#define MAX_RANGE 1000
main ()
{ 
     system("color 0E");
  system("title Guessing Game");
  char highscore;
  char name[20];  
  int counter=0;
  long value,input;
  srand ( time (NULL) );         // Initialize random generator
  value = rand()%MAX_RANGE+1;    // Get random between 1 and MAX_RANGE
  cout<<"                  You have 10 tries to guess the correct number\n";
  cout<<"Please enter your name:";
  cin>> name;
     if (value==666){
                     system("color 4C");
    cout<<"Satan has compelled you to guess the correct number"<< endl;}
    cout << "\nEnter a number between 1 and " << MAX_RANGE << " : \n";
    do {
    cin >> input;                // Get user input
    counter++;                   // increase attempts counter
    if (value>input)             // is value grater than the input?
cout << "Value is greater than " << input << ". Trys left ["<< 10-counter <<"]";
else if (value<input)        // if not, is it less?
  cout << "Value is less than " << input << ". Trys left ["<< 10-counter <<"]";
    else {                       // else it is the correct number!
      cout << "That's right! number was " << input;
      cout << "\nYou have needed " << counter <<" trys.\n";
      system("PAUSE");
       ofstream examplefile ("highscore.txt", ios::ate | ios::app);
  if (examplefile.is_open())
  {
     examplefile << ""<< name <<"--------"<< counter <<"\n";    
     examplefile.close();  }}
    if(counter==10 && input==value){
                   goto bah;
                   }
    if (counter==10)
    { system("cls");
      cout<<"Sorry, You failed to guess the number in time\n";
      cout<<"The correct value was '"<< value <<"'\n";
      system("PAUSE");
      return 0;}
  } while (value!=input);
 bah: cout<<"Would you like to see the highscore list? [Y][N]"<< endl;
    cin>> highscore;
    if (highscore=='y'){ 
    system("cls");
    char buffer[256];
  ifstream examplefile ("highscore.txt");
  if (! examplefile.is_open()) { cout << "Error opening file"; exit (1); }
  while (! examplefile.eof() ){
        examplefile.getline (buffer,100);
        cout << buffer <<endl;}}
  else {cout<<"Thanks for playing!\n";
  system("PAUSE"); 
  return 0;}
system("PAUSE");
return 0;}
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.