how do i program a guessing game?

hey guys. i really need ur help. how do i program a guessing game with this following requirements??
Write a 3-digit number-guessing game based on the requirements below:

•Each digit of the number cannot be repeated
•The first digit can start with 0
•If the number entered by the user has repeating digits, the user has to be asked to re-enter a new number
•Each attempt of guessing need to be numbered
•The user will be told how many digits correctly positioned and how many digits wrongly positioned after each guess
•The user will be congratulated if the user successfully guessed the number.
•The user will be asked whether to replay or not at the end of the game

this is my attempt so far :)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void)
{
int play_again;
int guess;
int result[3];
int i;
int r_num;
srand (time (NULL));
r_num = 9 + 1;

play_again=0;
for (i = 0; i < 3; i++)
{
result[i] = rand( ) % 3 + 1;

}
do
{
guess=0;
do
{
printf("Hey am thinkin of a number!! Wanna guess?? ;) \n");
scanf("%d", &guess);

if (guess != r_num)
{
printf("Wrong answer!!\n");
}

else
{
printf("Correct answer!!\n");
}

guess++;
}
while (r_num != guess);

printf("The correct answer is %d\n", rand () % r_num);
printf("Do you wish to play again?? (Y/N)");
scanf("%c", &play_again);
}
while (play_again== 'y'||play_again=='Y');

return 0;
}
anyone aware about this? really need help... :)
So what is the issue? I Can't compile, I'm on my phone ;)

Edit: at a glance things seem ok but your indenting is non-existent and you aren't taking advantage of c++ style output/input streams.
Last edited on
well the issue is i dun noe hw i do i make the program tell the user on what are the correct and wrong positions.... am very very new to c++ programming... :)
I would use an int array element for each number.

I'll cook you up a quick example, but I won't implement it into your program for you ;)

Edit: Ok here it is, I you have any questions about it please ask ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <iostream>

using namespace std;

int main()
{
    int Guess[5] = {1,2,3,4,5};
    int Correct[5] = {1,2,3,4,9};

    int AmountCorrect = 0;

    for(int x = 0; x < 5; x++)
    {
        if(Guess[x] == Correct[x]) AmountCorrect++;
    }

    cout << "Correct digits: " << AmountCorrect << endl;
    cout << "Incorrect digits: " << 5-AmountCorrect << endl;

    return 0;
}


Edit:
Oh! Were you asking for something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

using namespace std;

int main()
{
    // Define the amount of digits in the number (length)
    #define NUM_LEN 5

    // Create Variables.
    int Guess[NUM_LEN] = {1,2,3,4,5};
    int Correct[NUM_LEN] = {1,2,2,4,9};
    bool Positions[NUM_LEN] = {false};

    // check each element and set approptiate value in Positions[]
    for(int x = 0; x < NUM_LEN; x++)
    {
        if(Guess[x] == Correct[x]) Positions[x] = true;
    }

    // Output correct (true) positions
    cout << "Correct Positions: ";
    for(int x = 0; x < NUM_LEN; x++)
    {
        if(Positions[x] == true) cout << x+1 << ", ";
    }

    cout << endl;

    // Output incorrect (false) positions
    cout << "Incorrect Positions: ";
    for(int x = 0; x < NUM_LEN; x++)
    {
        if(Positions[x] == false) cout << x+1 << ", ";
    }

    // Pause
    cin.get();

    return 0;
}
Last edited on
Topic archived. No new replies allowed.