-1

I just started with C and I can't get a simple for loop to loop inside of a function that returns a bool. The program is a simple guessing game.

Here is my code:

//standard input and output header
#include <stdio.h>
#include <conio.h>
#include <stdbool.h>

bool CheckGuess(int guess, int nums[]);

int main(void)
{
int nums[5];
int i;
int userGuess;

for (i = 0; i < sizeof(nums)/sizeof(int); i++)
{
    nums[i] = 1 + rand() % 5;
}

printf("Guessing Game\n\n");

printf("Guess a number between 1 and 5: ");
scanf("%d", &userGuess);

if (CheckGuess(userGuess, nums))
{
   printf("You win!");
}
else
{
    printf("You lose!\nThe correct numbers are:\n\n");
    for (i = 0; i < sizeof(nums)/sizeof(int); i++)
    {
        printf("%d\n", nums[i]);
    }
}

//get character, wait/pause
getch();
}

bool CheckGuess(int guess, int nums[])
{
 int z;
 for (z = 0; z < sizeof(nums)/sizeof(int); z++)
 {
     printf("%d\n", z); //z just stops at 0 all the time

    if (guess == nums[z])
    {
              return true;  
    }
  }
  return false;
}
unwind
  • 391,730
  • 64
  • 469
  • 606
  • This condition `sizeof(nums)/sizeof(int)` looks rather suspect. Could you explain what you were trying to do with this? – FrustratedWithFormsDesigner Aug 27 '13 at 15:55
  • 1
    Have you tried printing sizeof(nums)? That plus a quick google for the term "array decay" should get you going. – stonemetal Aug 27 '13 at 16:07
  • possible duplicate of [Sizeof an array in the C programming language?](http://stackoverflow.com/questions/1975128/sizeof-an-array-in-the-c-programming-language) – Blastfurnace Aug 27 '13 at 16:25
  • @Blastfurnance. No it's not. People are trying to learn how to correctly pass the known size of an array to a function. That's perfectly valid. – dcaswell Aug 27 '13 at 16:35
  • I don't know if your code is formatted that way on your machine but if it is, indentation is your friend. Other developers who need to read your code later on (maybe even you) will be happy if you properly indent your code. – Onorio Catenacci Aug 27 '13 at 18:28

1 Answers1

4

When you write CheckGuess() you need to do it like this:

bool CheckGuess(int guess, int nums[], int numsCount)

and then call it with:

if (CheckGuess(userGuess, nums, sizeof nums / sizeof *nums)

Checkguess isn't "receiving" the size. Inside that function it's just a pointer.

dcaswell
  • 3,137
  • 2
  • 26
  • 25
  • 1
    +1, but `numSize` is a really weird name to me at least. I understand it's "size of `nums`", but I think `numsLength` or `numsCount` or something would be more clear. – unwind Aug 27 '13 at 16:22
  • add reason also that `int nums[]` is not an array in function parameter – Grijesh Chauhan Aug 27 '13 at 16:23
  • @unwind -- I made the change – dcaswell Aug 27 '13 at 16:24
  • 1
    @user814064 Great. I also would recommend the call being made as `sizeof nums / sizeof *nums`, to avoid repeating the `int` type. This makes it slighly safer if the type of `nums` were to change. – unwind Aug 27 '13 at 16:25