Thread: I'm Stumped on a Programming problem. Not sure how to start it etc. Please Help

  1. #1
    Registered User fanathiter's Avatar
    Join Date
    Oct 2014
    Location
    Franklin, Massachusetts, United States
    Posts
    16

    I'm Stumped on a Programming problem. Not sure how to start it etc. Please Help

    Write a program that will generate, but not display, a three-digit “target” number that has three distinct digits. (Hint: use random number generator.) Then, input a maximum of ten user guesses and for each guess, output the number of hits and matches in the guess. Stop when the user guesses the number or runs out of guesses. For example, if the target is 427, the guess 207 has one hit (7) and one match (2).

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Maybe start by writing some pseudocode steps e.g.

    generate 3-digit target
    input guess from user
    determine the number of hits in guess
    determine the number of matches in guess
    ...

    Do you understand how to do all the steps? If something is unfamiliar write a small program so that you understand how to do that (e.g. to generate a 3 digit number).

  3. #3
    Registered User fanathiter's Avatar
    Join Date
    Oct 2014
    Location
    Franklin, Massachusetts, United States
    Posts
    16
    I understand how to do the random numbers and user inputs. for the hit guesses im thinking of if statements, but my problem is how to make the program stop after 10 guesses.

  4. #4
    Registered User fanathiter's Avatar
    Join Date
    Oct 2014
    Location
    Franklin, Massachusetts, United States
    Posts
    16
    Thank you for the reply btw

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by fanathiter View Post
    I understand how to do the random numbers and user inputs. for the hit guesses im thinking of if statements, but my problem is how to make the program stop after 10 guesses.
    Use a counter and a loop.

    http://www.cprogramming.com/tutorial/c/lesson3.html

  6. #6
    Registered User fanathiter's Avatar
    Join Date
    Oct 2014
    Location
    Franklin, Massachusetts, United States
    Posts
    16
    I'll Attempt to code it tonight and tomorrow and post my progress tomorrow afternoon, after my classes thank you

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Excellent. In the mean time, keep in mind that you should build your program gradually, one step at a time. Break the problem down in smaller problems, and solve each of those smaller problems one at a time. Then when you're ready to start coding, just write a little bit of code at a time, compile, and test. Then add a bit more, compile, test. Repeat.

    A development process

    c99tutorial started showing you some individual steps in post #2. Start tackling them one at a time.

  8. #8
    Registered User fanathiter's Avatar
    Join Date
    Oct 2014
    Location
    Franklin, Massachusetts, United States
    Posts
    16
    Ok so this is what I have so far Im having problems making the code end once the user guesses the 3 correct digits. any ideas???

    Code:
     #include <stdio.h>
     #include <stdlib.h>
     #include <time.h>
     void playGame();
     char getPlayerMove();
     int main()
     {
         playGame();
         return 0;
     }
     void playGame()
     {
         int a,b,c,d,e,f,x;
         char playerMove;
         printf("You have 10 guesses of a 3 digit number, I will tell you if you are correct or not\n");
         srand(time(NULL));
         a = (rand() %6)+1; //random #
         b = (rand() %6)+1;
         c = (rand() %6)+1;     for(x = 0; x<10; x++)
         {
             printf("Lets start the game, what is your guess? Enter: (#) (#) (#)\n");
             scanf("%d%d%d",&d,&e,&f);
             if(d == a)
             {
                 printf("Nice! You've got the first number: %d\n", a);
             }
             if(d == b)
             {
                 printf("Nice! You've got the second number: %d\n", b);
             }
             if(d == c)
             {
                 printf("Nice! You've got the thrid number: %d\n", c);
             }
             if(e == a)
             {
                 printf("Nice! You've got the first number: %d\n", a);
             }
             if(e == b)
             {
                 printf("Nice! You've got the second number: %d\n", b);
             }
             if(e == c)
             {
                 printf("Nice! You've got the third number: %d\n", c);
             }
             if(f == a)
             {
                 printf("Nice! You've got the first number: %d\n", a);
             }
             if(f == b)
             {
                 printf("Nice! You've got the second number: %d\n", b);
             }
             if(f == c)
             {
                 printf("Nice! You've got the third number: %d\n", c);
             }
             if(a b c)  /*  --------------> don't know how to make this to end the code once the user gets the 3 right digits.*/
             {
                 printf("You solved it! %d%d%d\n", a,b,c);
                 exit(0);
             }
         
         }
      
     }

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Look up "logical operators"; i.e. logic AND (&&).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Directx programming! Where to start from?
    By freiza in forum Game Programming
    Replies: 2
    Last Post: 02-24-2012, 07:51 PM
  2. Absolutely stumped with running sum problem.
    By jaja009 in forum C Programming
    Replies: 3
    Last Post: 02-26-2010, 04:03 AM
  3. I want to start programming on linux
    By MIH1406 in forum Linux Programming
    Replies: 3
    Last Post: 05-22-2009, 01:15 PM
  4. New to programming, were to start??
    By jsmith in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 05-19-2008, 07:37 AM
  5. i want to start gui programming
    By kaos_frack in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2007, 01:41 PM