Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,190 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,151 people online right now. Registration is fast and FREE... Join Now!




Guess the number game

 
Reply to this topicStart new topic

Guess the number game, infinit looppy :Sprob

manzoor
6 Sep, 2007 - 06:40 AM
Post #1

D.I.C Regular
Group Icon

Joined: 7 Aug, 2007
Posts: 398



Thanked: 2 times
Dream Kudos: 50
My Contributions
hey i have modified this game i have copied the original from my friend here is the code its working fine but when it asks for input and u input sumthing else (a-z, {,},:,",<?,!,@,#,%,^,&,)except the numbers then it becomes an infinite loop dont know whats the prob

CODE
/*Author - Manzoor.
This program uses the rand() function to
generate random numbers.*/

// Guess the number

#include <iostream>
#include <cstdlib>
#include<ctime>
using namespace std;
void play(int m);   // function prototype
int main()
{
    int option;
    int magic;
    srand(time(0));
    magic = rand() % 17 + 1;  // random number generator

    cout << "************************\n*      Welcome         *\n";
    cout << "*                   *\n*               *\n*        To           *\n";
    cout << "*               *\n*               *\n*   Guess the Number   *\n";
    cout << "*               *\n************************\n" << endl;


    do {
       cout << "\n\n[1]. Start Play\n";
       cout << "[2]. Quit\n";       do {

       cout << "\nEnter your choice [1 / 2]: ";
       cin >> option;

       if ( option == 1 ) {
          cout << "\nGuess the Number between 0 - 17.\n";
          cout << "\nRemember ! You only have 3 turns to guess the number.\n";
          cout << "\nGood Luck !\n\n";
          }
       } while(option < 1 || option > 2);
                        switch(option) {
                        case 1:
                        magic = rand() % 17 + 1;
                        play(magic);
                        break;
                        case 2:
                        cout << "\n************************\n*      GoodBye !       *\n";
                        cout << "*                   *\n*  Thanks for Playing  *\n";
                        cout << "*               *\n*        Made           *\n";
                        cout << "*               *\n*         By           *\n";
                        cout << "*               *\n*       Manzoor           *\n";
                        cout << "*               *\n************************\n\n";
                        break;
                         }
    } while(option != 2);
    system ("PAUSE"); // causes the program to quit after pressing a key
    return 0;
}
    // Play the game.
void play(int m)
{
    int tries, x;
    for(tries=0; tries < 3; tries++) {
    cout << "\nYou have used " << tries << " turns.\n\n";
    cout << "Guess the number: ";
    cin >> x;

    if(x == m) {
         cout << "** Right **\n";
         cout << "\n** You got the guess in " << tries << " guesses." << endl;
         return;
         }
    else if(x < m) cout << "\n\nToo low.\n";
         else cout << "\n\nToo high.\n";
    }
    cout << "\nYou have used up all your guesses.\n";
    cout << "\nThe number was " << m << ".\n";
    cout << "\nTry Again !" << endl;
}



User is online!Profile CardPM
+Quote Post

no2pencil
RE: Guess The Number Game
6 Sep, 2007 - 06:49 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,451



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Use the isdigit function to check the users input. FORCE that value to be a number.
http://www.cplusplus.com/reference/clibrar...pe/isdigit.html

User is online!Profile CardPM
+Quote Post

manzoor
RE: Guess The Number Game
6 Sep, 2007 - 06:56 AM
Post #3

D.I.C Regular
Group Icon

Joined: 7 Aug, 2007
Posts: 398



Thanked: 2 times
Dream Kudos: 50
My Contributions
i dont know how to use this function ? any one help me out ? plz
User is online!Profile CardPM
+Quote Post

no2pencil
RE: Guess The Number Game
6 Sep, 2007 - 06:59 AM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,451



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(k00ldude47 @ 6 Sep, 2007 - 07:56 AM) *

i dont know how to use this function ? any one help me out ? plz

I provided a link on the last post....
User is online!Profile CardPM
+Quote Post

manzoor
RE: Guess The Number Game
6 Sep, 2007 - 07:06 AM
Post #5

D.I.C Regular
Group Icon

Joined: 7 Aug, 2007
Posts: 398



Thanked: 2 times
Dream Kudos: 50
My Contributions
yea i visit that link but can;t understand it :s plz if some one can eexplain this function ( isdigit() ) in way so beginners like me can understand it thx
User is online!Profile CardPM
+Quote Post

PennyBoki
RE: Guess The Number Game
6 Sep, 2007 - 07:19 AM
Post #6

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hey see the example:

http://www.cprogramming.com/fod/isdigit.html

and then it would be nice if you tell us what that function does.
User is offlineProfile CardPM
+Quote Post

manzoor
RE: Guess The Number Game
6 Sep, 2007 - 07:26 AM
Post #7

D.I.C Regular
Group Icon

Joined: 7 Aug, 2007
Posts: 398



Thanked: 2 times
Dream Kudos: 50
My Contributions
thx i understood it through the example thx PennyBoi
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 01:24AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month