Like Tree1Likes
  • 1 Post By qny

Help with Program that only allows three tries

This is a discussion on Help with Program that only allows three tries within the C Programming forums, part of the General Programming Boards category; Hi so for this program it says: 1)Allow the user only three chances to enter the correct number. 2)The program ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    3

    Help with Program that only allows three tries

    Hi so for this program it says:
    1)Allow the user only three chances to enter the correct number.
    2)The program should stop if the user doesn't enter the correct value after the third try
    3) The program should stop if the user guess correctly(and is less than 3 tries)

    here is the code:
    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<string.h>
    #include<time.h>
    
    
    // Declaration Section
    int num;
    int guess;
    
    
    void pauseProgram()
    {
         printf("\nPress any key to continue...");
         getchar();
         }
         
         // Function Title
         void title(char*programTitle)
         {
              int len=strlen(programTitle);
              system("cls");
              printf("\n");
              for(int i=1;i<40-len/2;i++) printf("");
              printf("%s\n",programTitle);
              }
              
              // User lnput
              void userlnput()
              {
                   title("Random Number");
                   printf("\nEnter a number between 1 and 100:");
                   scanf("%d",&guess);
                   getchar();
                   }
                   
                   //Funcion displayRand
                   void displayRand()
                   {
                        /* The following 2 lines are needed for generating
                        dufferent random numbers for each execution.
                        Simple "srand()" seed: just use "time()"
                        */
                        unsigned int iseed= (unsigned int)time(NULL);
                        srand(iseed);
                        
                        num= rand()%100;
                        num= num+1;
                        title("Random Number");
                        if(guess>num)
                        printf("\nCome down!\n");
                        else if(guess<num)
                        printf("\nWay down there?\n");
                        else
                        printf("\nRight on!\n");
                        
                        pauseProgram();
                        }
                        
                        // Main Program
                        int main()
                        {
                            do
                            {
                                  userlnput();
                                  displayRand();
                                  if(num==guess)
                                  {
                                                break;
                                                }
                                                }while(1);
                                                }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    I am struggling to find the part that you describe us the error, or why you are not happy with the code....

    If i read the code i might find out myself, but this would be unfair i think

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    3
    Quote Originally Posted by std10093 View Post
    I am struggling to find the part that you describe us the error, or why you are not happy with the code....

    If i read the code i might find out myself, but this would be unfair i think
    Basically in this program you have to guess the correct number between 1-100. However at this moment the user gets an infinite amount of guesses.

    I want to make it so that they only have 3 guesses. If they get it right the program says "Good job", and even if they dont get it right in 3 guesses it says "Try next time"

    I was wondering if someone could tell me the code I would have to write to make this work or maybe the location in which i'm supposed to put it

    Thanks

  4. #4
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    vololo: if you're using vim try, at command mode, pressing gg=G.
    AndiPersti likes this.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Quote Originally Posted by vololo View Post

    I want to make it so that they only have 3 guesses.
    Thank you

    You want the loop in main to be executed maximum three times and at least once! Good choice of the do-while loop..
    So i will make an example of loop i want to run maximum 5 times and at least one.I want the loop to stop executed if the user types 5.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a;
        int counter = 0;
    
        do{
             /* Maximum iterations allowed */
             if(counter == 5 )
                    break;
             /* Increment counter by one */
             counter++;
             /* Read input */
             scanf("%d",&a);
        /* Check if the user typed 5 */
        }while(a != 5 );
    
        return 0;
    }
    Now try to modify your code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 11-03-2010, 12:45 PM
  2. Replies: 1
    Last Post: 03-03-2009, 03:47 PM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 12:11 PM
  5. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 04:33 PM

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