View Full Version: number guessing

C++ Learning Community > C++ Creations > number guessing


Title: number guessing
Description: my first C++ game


candc32 - March 5, 2006 08:27 AM (GMT)
well the title pretty much says it its a guessing with numbers heres my source code:
CODE

#include <iostream>
#include <time.h>
#include <string>
#include <fstream>
using namespace std;

int main ()
{
   int n , ng (9), m (0);
   string name, play;
   ofstream file1;
   srand ( time(NULL) );
   n = rand()%100+1;
   file1.open("scores.txt",ios::app);
   cout << "the number in betwen 1 and 100 type '888' at any time to exit.\n";
   while (ng != n) {
   cout << "pick an number.\n";
   cin >> ng;
   ++m;
   if (ng==888)
   {return 0;}
   if (ng < n) {
         cout << "larger.\n";
         }
         else if (ng > n) {
         cout << "smaller.\n";
         }
         else {
         cout << "";
         }
   }
   cout << "thats right!!\n";
   cout << "it took you " << m << " trys to get it right.\n";
   cout << "what is your name?\n";
   cin >> name;
   if (m==1){
   file1 << name << " won in " << m << " guess\n";
   }
   else
   {
   file1 << name << " won in " << m << " guess(es)\n";
   }
   ifstream   file("scores.txt");
   cout << file.rdbuf();
   file1.close();
   cout << "play again? y/n\n";
   cin >> play;
   if (play=="y")
       {
       main ();
   }
       else
   {
       return 0;
       }
}

tell me what you think

myork - March 5, 2006 02:03 PM (GMT)


Next Step.
1) Count how many guesses it took.
2) Display to the user how many guesses he took.
3) Save the username and guess count into a file 'Score File'
4) Read the 'Score File' and show everybods scores.
5) When displaying scores calculate the average for every person.

AquaFox - March 5, 2006 02:06 PM (GMT)
What Myork said was good practice. Incase you go into serious game programming. Also it helps with File I/O.

candc32 - March 5, 2006 05:29 PM (GMT)
how would I save the name in an exterunal file?

AquaFox - March 5, 2006 06:20 PM (GMT)
Look for fstream. Go Google.

candc32 - March 5, 2006 09:57 PM (GMT)
i've made some changes to the code so now if you want to play again you just press y and enter

candc32 - March 11, 2006 05:56 PM (GMT)
ok I've done every thing you guys sugested and it's done the code is in the first post




Hosted for free by InvisionFree