Guess number game (simple)

This is a discussion on Guess number game (simple) within the C++ Programming forums, part of the General Programming Boards category; Hi programmers! I'm new at c++ and i want help about a simple guessing number game. I want: the player ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    Guess number game (simple)

    Hi programmers!

    I'm new at c++ and i want help about a simple guessing number game.

    I want:
    • the player has 3 tries (the number is between 0 and 20)
    • if player doesnt guess the right number, then the programm will close
    • if the player guess the right number then print "congrats" and ask the player if he want a new game.
    I post the code i have already write. Because i am new, any help are welcome!


    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    main()
    
    
    {     
    
    
          int x;
          int guess; 
          int tries;
          char answer;
          
          do
          x = rand();
          while ( x < 0 || x > 20 );
    
    
          tries=0;
    
    
    do      
          do
    
    
            do
            printf("Put your guess from 0 to 20 please;");
            scanf("%d",&guess);
            while ( guess < 0 || guess > 20 );
            
            tries=tries+1
            
            if (guess==x)
               printf ("Congratulations, you guess the right number. Do you want play again?");
               scanf("%s", answer);
            else 
                printf ("wrong number!");
      
          while tries=3
    
    
    while (answer!=no);
    
    
    }

    Thank you very much.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    You should start by adding ALL the braces you seem to have deleted from the source code.

    python works off the indentation of blocks - C does not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    Quote Originally Posted by Salem View Post
    You should start by adding ALL the braces you seem to have deleted from the source code.

    python works off the indentation of blocks - C does not.
    I'm sorry. You have right.


    I wrote new code.




    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int main()
    {
     int random;
     int usersGuess;
     
     printf ("Welcome to the game\n\n"
    "I will generate a number between 1 and 20\n\n"
    "and you have 3 chances to guess it\n\n");
       
        do
        random = rand();
        while ( random < 0 || random > 20 );
    
    
            
       printf ("Guess a number between 0 and 20");
       printf ("Input guess : ");
       scanf("%d",&usersGuess);
       int attempts = 1;
       while (attempts <3)
       {
        if (usersGuess > random || usersGuess < random)
        {
         printf ("GUESS AGAIN\n");
         printf ("Input guess : ");
         scanf("%d",&usersGuess); 
         attempts++;
        }
        else
        {
        attempts = 3;
        printf ("YOU WON\n");
        }
       }
    
    
     return(0);
    }

    Now, the only change i want is
    • if the player doesn't guess the right number, then programm print the right number and close and

    • if the player guess the right number, then will be asked if he want a new game or not.


    Thank you very much.
    Last edited by Fotis; 12-22-2011 at 11:56 AM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Have a go at your additiomal requirements. You can at least add the lines that output the extra messages.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    Make up your mind: are you going to write C or C++?
    If you are going to write C++, then go re-learn basic I/O.
    If not, then this should be moved to the C board.
    While you're at it, improving indentation would help a lot.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 10-20-2011, 06:32 PM
  2. Replies: 7
    Last Post: 10-19-2011, 08:45 AM
  3. Guess my Number game! check it out
    By Yosif in forum Game Programming
    Replies: 1
    Last Post: 06-09-2007, 09:43 PM
  4. A "guess my number" game, with simple AI
    By h3ro in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2006, 09:45 PM
  5. Guess the number game
    By Ninestar in forum C Programming
    Replies: 12
    Last Post: 12-08-2005, 10:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21