Thread: Help with guessing game im making

  1. #1
    Unregistered
    Guest

    Exclamation Help with guessing game im making

    Im making a guessing game in c++. THis is my first attempt at a program. I cant figure out how to do it. Heres my code, i dont know whats wrong:

    #include <iostream.h>
    int main ()
    {
    int guess;
    int num;
    cout<<"Enter a guess";
    cin>>guess;
    if(guess = num)
    {
    cout>>"You guessed the right number!";
    }
    else
    {
    cout>>"Try again"
    }
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    88
    hmmm

    try to give to the GUESS
    something
    some number

    becouse the GUESS can be -34343434
    and you never guess it

  3. #3
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    also use == instead of =.
    and also << istead of >> for cout on the last one...
    so:

    #include <iostream.h>
    int main ()
    {
    int guess;
    int num=4; //or any other number
    cout<<"Enter a guess";
    cin>>guess;
    if(guess == num)
    {
    cout << "You guessed the right number!";
    }
    else
    {
    cout << "Try again" ;
    }
    }

    yipyep!

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    23
    the num should have a value as godlike said.
    e.g. int num=33;

    Aslo the equlity sign is ==
    if(guess==num)


    and you should return a value to main.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    208
    You should use the rand() function too I think thats what it is. Ti create a random number. Use like rand()%100 to get a number less the 100.

    Code:
     also use == instead of =. 
    and also << istead of >> for cout on the last one... 
    so: 
    
    #include <iostream.h> 
    int main () 
    { 
    int guess; 
    int num; //or any other number 
    num=rand()%100;//creates a random number between 1 -99
    cout<<"Enter a guess"; 
    cin>>guess; 
    if(guess == num) 
    { 
    cout << "You guessed the right number!"; 
    } 
    else 
    { 
    cout << "Try again" ; 
    } 
    }
    to find out more functions and stuff click here http://www.cplusplus.com/ref/#libs

    cplusplus home is a good resource site. By the way if anyone knows anybetter ones would they let me know cause i have pretty much read everything on this on
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  6. #6
    Unregistered
    Guest

    Exclamation I have one more question...

    It exits the program too fast when i run it. I cant see weather or not the guess was right. Is there some way to make it pause untill the user hits a key, like the SLEEP command in QBasic.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    Code:
    #include <windows.h>
    ..
    Sleep(x); // where X is the number of milliseconds to pause the program for
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    29
    There are many ways to make a pause before exiting a program.
    You can include <conio.h> and then use
    Code:
     while (!kbhit());
    or <stdio.h> and getchar();
    In DOS you can use sleep(int number_of_seconds); or delay(int number_of_miliseconds); (maybe this will work also under another OS, but probably not good..)

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    you could also try adding maybe 3 tries before it tells you the answer, because its kinda illogical to be guessing millions of times for one number. and if you make the limit, say, one hundred and negative one hundred, check if the number that the user inputs is larger or smaller than the limit. just some more things to gain more experience .

  10. #10
    Unregistered
    Guest

    why so many include stuff?

    Why do i have to include all this stuff? How am i suposed to know which to include in my programs? What do they do? Are they like libraries are in qbasic?

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    317
    Yes, the header files are like libraries in qbasic. They enable you not to have to worry about codeing some of the everyday routine things such as writing to the screen and getting input and checking to see if a key is hit, ect. You will learn what each header file offers you by experience. By the way you can also open those files and read through them, which will give you the best understanding of what they offer.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If you're learning C++, forget everything you ever learned about BASIC. If you keep comparing stuff, it doubles your learning time. You have to include all that stuff because it's the code for what you're doing. When using higher level languages like C, the goal is to not write code if someone already has.

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Also, it'll help you get a better answer next time if you specify what is going wrong with the problem. You don't have to know the source, just say what ou don't like about that. And no, it couldn't be -34343434. It's an int - ints don't have that range. It all comes down to what is in the binary code of the memory location that was next. It's not a random number (however unpredictable it may be)

  14. #14
    Unregistered
    Guest

    ok, need help with multiple tries

    What I want is to have 3 tries and then have it ask if the user wants to try again. How do I do this? Heres the code I have now:

    #include <iostream.h>
    #include <windows.h>

    int main ()
    {
    int guess;
    int num=4; //or any other number

    cout<<"Enter a guess: ";
    cin>>guess;

    if(guess == num)
    {
    cout << "You guessed the right number!";
    Sleep(1000);
    }
    else
    {
    cout << "Try again" ;
    Sleep(1000);
    }
    }

  15. #15
    Ryce
    Guest
    Code:
    #include <iostream.h> 
    #include <windows.h> 
    
    int main () 
    { 
    int guess; 
    int trys=1;
    int num=4; //or any other number 
    
    cout<<"Enter a guess number " << trys << ": "; 
    cin>>guess; 
    cin.ignore(80,'\n'); // i uselly need that
    
    while(trys <=4) {// 4 tries
    if(guess == num) 
    { 
    cout << "You guessed the right number!"; 
    Sleep(1000); 
    return 0;
    } 
    else 
    { 
    cout << "Try again" ; 
    Sleep(1000); 
    } 
    
    }
    cout << " Sorry, but you faild!!!" << endl << "The number was " << num << endl;
    cin.get();
    return 0;
    }
    should work maybe a typo here or there, im to lazy to compile

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM
  2. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  3. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  4. Making a game
    By KneeGrow in forum Game Programming
    Replies: 1
    Last Post: 04-16-2004, 12:40 PM
  5. Lets Play Money Making Game
    By ggs in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-04-2001, 08:36 PM