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');
}
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]
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]
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.