Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Guess the number..... :)

Got an idea for a practice project / debug snippet / short-code contest / etc? Tell us about it here!

Guess the number..... :)

Postby VicToMeyeZR on Sat Jul 18, 2009 8:11 pm

Yeah, I know I am a NooB. hehe, but I just started c++ 2 hours ago, so I guess I am doing ok. hehe

Code: Select all
#include "stdafx.h"

#include <iostream>

using namespace std;

int main ()
{
   int SecretNumber = 25;
   int PlayerNumber = 0;
   while (PlayerNumber != SecretNumber)
   {
   cout << "Number is between 1 and 100. \n Please enter your guess: ";
   cin >> PlayerNumber;
      
   if (PlayerNumber == SecretNumber)
      cout << "Congratulations, you have successfully guessed the secret number. \n";
   else
      if (PlayerNumber < SecretNumber) {
         cout << "I am sorry, your guess if to low.  Please try again. \n";
      }
      else
         if (PlayerNumber > SecretNumber) {
            cout << "I am sorry, your guess is to high.  Please try again. \n";
         }
   }
   char f;
   cin >> f;
   return 0;
}


Anyways, thought I would post this. I did this before I even got to the loop tutorial (kind of cheated though as I know php ok)....
VicToMeyeZR
 
Posts: 7
Joined: Sat Jul 18, 2009 5:22 pm

Re: Guess the number..... :)

Postby VicToMeyeZR on Sat Jul 18, 2009 8:56 pm

well updated already. This is now a random number. ;)

Code: Select all
#include "stdafx.h"
#include "stdlib.h"
#include "time.h"

#include <iostream>

using namespace std;

int main ()
{
   srand (time (NULL));

   int SecretNumber = rand() % 100;
   int PlayerNumber = 0;
   while (PlayerNumber != SecretNumber)
   {
   cout << "Number is between 1 and 100. \n Please enter your guess: ";
   cin >> PlayerNumber;
      
   if (PlayerNumber == SecretNumber)
      cout << "Congratulations, you have successfully guessed the secret number. \n";
   else
      if (PlayerNumber < SecretNumber) {
         cout << "I am sorry, your guess if to low.  Please try again. \n";
      }
      else
         if (PlayerNumber > SecretNumber) {
            cout << "I am sorry, your guess is to high.  Please try again. \n";
         }
   }
   char f;
   cin >> f;
   return 0;
}
VicToMeyeZR
 
Posts: 7
Joined: Sat Jul 18, 2009 5:22 pm

Re: Guess the number..... :)

Postby VicToMeyeZR on Sat Jul 18, 2009 9:24 pm

and again. lol. I had to put in a limit. you get 10 trys. :)

Code: Select all
#include "stdafx.h"
#include "stdlib.h"
#include "time.h"

#include <iostream>

using namespace std;

int main ()
{
   srand (time (NULL));
   int trysLeft = 10;
   int SecretNumber = rand() % 100, PlayerNumber = 0;
   cout << "The secret number is between 0 and 100.\n";
   while (PlayerNumber != SecretNumber)
   {
      for (int trys = 0; trys < 10; trys++)
      {
         cout << "You have " << trysLeft << "trys left.  Please enter your guess: ";
         cin >> PlayerNumber;
         if (PlayerNumber == SecretNumber)
         {
         cout << "Congratulations, you have successfully guessed the secret number. \n";
         }
         else
            if (PlayerNumber < SecretNumber)
            {
               cout << "I am sorry, your guess if to low.  Please try again. \n";
            }
            else
               if (PlayerNumber > SecretNumber)
               {
               cout << "I am sorry, your guess is to high.  Please try again. \n";
               }
            trysLeft--;
      }
   }
   char f;
   cin >> f;
   return 0;
}
VicToMeyeZR
 
Posts: 7
Joined: Sat Jul 18, 2009 5:22 pm

Re: Guess the number..... :)

Postby aznlee on Thu Jul 23, 2009 8:14 pm

Nice job figuring out how to randomize numbers.
Image
aznlee
 
Posts: 5
Joined: Thu Jun 25, 2009 2:41 pm


Return to -- Code Practice Submissions --

Who is online

Users browsing this forum: No registered users and 0 guests