-1

this function doesn't check the nums at the ifs... what i need to do? tnx for helping!

//Includes

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

//Defines

#define FIVE    5
#define SIX     6
#define TEN     10
#define FIFHTY  15
#define TWENTY  20
#define GEN_NUM 5309
#define GEN_MIN 1234

int main() {
  char c = ' ';
  int hits = 0, miss = 0;
  int counter = 0;
  int round = TWENTY;
  int flag = 0;
  int input = 0;
  int gen = 0;
  int num1 = 0, num2 = 0, num3 = 0, num4 = 0;
  int inum1 = 0, inum2 = 0, inum3 = 0, inum4 = 0;

  do {  
    do {
        printf("generating...\n");
        gen = rand() % GEN_NUM + GEN_MIN;
        printf("gen %d\n", gen);
        num1 = gen % TEN;
        num2 = gen % (TEN*TEN) / TEN;
        num3 = gen % (TEN*TEN*TEN) / (TEN*TEN); 
        num4 = gen / (TEN*TEN*TEN);
    } while (num1 < 0 || num1 > SIX || num1 == 0 ||
             num2 < 0 || num2 > SIX || num2 == 0 ||
             num3 < 0 || num3 > SIX || num3 == 0 ||
             num4 < 0 || num4 > SIX || num4 == 0 ||
             num1 == num2 || num1 == num3 || num1 == num4 ||
             num2 == num3 || num2 == num4 || num3 == num4);

    do {
        do {
            printf("Write your guess (only 1-6, no ENTER is needed) [%d guesses left]\n", round);
            num4 = getch();
            putch(num4);
            num3 = getch();
            putch(num3);
            num2 = getch();
            putch(num2);
            num1 = getch();
            putch(num1);

            num1 -= '0';
            num2 -= '0';
            num3 -= '0';
            num4 -= '0';

            printf("\nnum1 %d\n", num1);
            printf("num2 %d\n", num2);
            printf("num3 %d\n", num3);
            printf("num4 %d\n", num4);

            //if ill use scanf it will work fine...
            /*  scanf("%4d", &input); 
            printf ("\ninput: %d\n", input);
            inum1 = input % TEN;
            printf ("%d\n", inum1);
            inum2 = input % (TEN*TEN) / TEN;
            printf ("%d\n", inum2);
            inum3 = input % (TEN*TEN*TEN) / (TEN*TEN);
            printf ("%d\n", inum3);
            inum4 = input / (TEN*TEN*TEN);
            printf ("%d\n", inum4); */ //

            if (num1 < 0 || num1 > SIX || num1 == 0 ||
                num2 < 0 || num2 > SIX || num2 == 0 ||
                num3 < 0 || num3 > SIX || num3 == 0 ||
                num4 < 0 || num4 > SIX || num4 == 0 ||
                num1 == num2 || num1 == num3 || num1 == num4 ||
                num2 == num3 || num2 == num4 || num3 == num4)
            {
                printf("Only 1-6 are allowed, try again!");
                flag == 1;
            }
            else
            {
                flag = 0;
            }
        } while (flag == 1);

        hits = 0;
        miss = 0;
        if (inum1 == num1)
        {
            hits++;
        }
        else if(inum1 == num2 || inum1 == num3 || inum1 == num4)
        {
            miss++;
        }

        if (inum2 == num2)
        {
            hits++;
        }
        else if(inum2 == num1 || inum2 == num3 || inum2 == num4) 
        {
            miss++;
        }

        if (inum3 == num3)
        {
            hits++;
        }
        else if (inum3 == num1 || inum3 == num2 || inum3 == num4) 
        {
            miss++;
        }

        if (inum4 == num4)
        {
            hits++;
        }
        else if (inum4 == num1 || inum4 == num2 || inum4 == num3)
        {
            miss++;
        }
        if (hits == 4)
        {
            printf("4 HITS YOU WON!!!\n");
            flag = 0;
        }
        else if (round == 0)
        {
            printf("OOOOHHHH!!! Pancratius won and bought all of Hanukkah's gifts.\nNothing left for you...\n");
            printf("The secret password was %d\n", gen);
            flag = 0;
        }
        else
        {
            printf("you got\t %d HITS\t %d MISSES\n", hits, miss);
            counter++;
            round--;
            flag = 1;
        }
    } while(1 == flag);

    printf("Would you like to play again? (y/n):");
    c = getchar();

  } while (c == 'y');
}

my wanted results

 generating...
 gen 1284
 generating...
 gen 3507
 generating... 
 gen 1490
 generating...
 gen 1519
 generating...
 gen 2546
 Write your guess (only 1-6, no ENTER is needed) [20 guesses left]
 2465
 num1 5
 num2 6
 num3 4
 num4 2
 you got  0 HITS  4 MISSES
 Write your guess (only 1-6, no ENTER is needed) [19 guesses left]

And the real result is:

 generating...
 gen 1284
 generating...
 gen 3507
 generating...
 gen 1490
 generating...
 gen 1519
 generating...
 gen 2546
 Write your guess (only 1-6, no ENTER is needed) [20 guesses left]
 2465
 num1 5
 num2 6
 num3 4
 num4 2
 you got  0 HITS  0 MISSES // it should be 0 HITS 4 MISSES
 Write your guess (only 1-6, no ENTER is needed) [19 guesses left]
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Ruslan Ver
  • 127
  • 4
  • 12
  • Please state more clearly what the expected behaviour of your program is and what the current behaviour is. "doesn't check the nums at the ifs".. it isn't clear what that means (for starters there is more than one `if`). It doesn't reach that statement? It doesn't evaluate to true but is expected to? What?? And please post a [Minimal Complete and Verifiable Example](https://stackoverflow.com/help/mcve) that we can copy *as is* and run for ourselves. – kaylum Dec 21 '15 at 22:15
  • Thanks for the update. But you still have not posted an MCVE. The code is incomplete and cannot be copied and run as is. – kaylum Dec 21 '15 at 22:27
  • Your "miss" conditional checks are weired. `if(inum1 == num2 || inum1 == num3 || inum1 == num4)`. Why would that be a *miss* if `inum1` matches at least one of the `numX` inputs? Did you mean to negate that whole condition? – kaylum Dec 21 '15 at 22:36
  • no, its game program that the generated num not need to be showed but iv'e not completed and checks if the player 'miss' the place of that number or hit the 'place' i want to use with getch() becouse i want it will do "ENTER" automatically – Ruslan Ver Dec 21 '15 at 22:44

1 Answers1

0

Definition for variable round is nowhere in sight. It should be initialized to the maximum number of tries before the loop where you get user input.

When you read user input, you should store that into variables inum1, inum2... not num1, num2 etc.

Furthermore, you mix calls to getch() and calls to getchar(). I'm not sure if the behavior is what you expect.

Note that we had to reformat your code to make sense of it. Learn to format your code properly. Avoid long lines, indent consistently, using spaces, not tabs.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • i feel so stupid right now i should use with inum1, inum2... thank u very much!!!! – Ruslan Ver Dec 21 '15 at 22:51
  • @RuslanVer: some extra advice: you should use 2 arrays of 4 ints instead of 4 separate variables. Using a loop for validating the input and 2 nested loops for the matching algorithm would use less code and avoid typing mistakes. – chqrlie Dec 21 '15 at 23:24