I can't seem to figure out the syntax error on this one, it says the error is at the end of the input....

#include <stdio.h>
#include <math.h>

int determine(int);

void main(void)
{
     int num1, even, odd;

     printf("Enter a number: ");
     scanf("%d", &num1);

     printf("The number is: %d", determine(num1));
     scanf("%d", &num1);
     getchar();

int determine(int num1)
{
    if(num1 %2 == 0)
            num1 = even;
    else
            num1 = odd;
}

Any help would be appreciated,
Andrew

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Your main doesn't have a closing brace. You also can't have nested functions, so the use of even and odd in determine has to be done differently.

Heh, yeah I just noticed that... Im a newbie to C programming... :(


I changed that so it is running now, but after it displays if the number is even or odd it asks for another number and when you enter a number the console goes away..

>Im a newbie to C programming...
Then I'll adjust my next answer accordingly, because it's not exactly a trivial problem.

>when you enter a number the console goes away..
You're using getchar to pause the program it seems, but because there's still a newline left in the stream, getchar reads it immediately and doesn't block. You can add a second getchar:

int main(void)
{
  int num1, even, odd;

  printf("Enter a number: ");
  scanf("%d", &num1);

  printf("The number is: %d", determine(num1));
  scanf("%d", &num1);
  getchar();
  getchar();

  return 0;
}

And that will probably work, or you can loop until you find a newline, which is more likely to work most of the time:

int main(void)
{
  int num1, even, odd;

  printf("Enter a number: ");
  scanf("%d", &num1);

  printf("The number is: %d", determine(num1));
  scanf("%d", &num1);
  while ( getchar() != '\n' )
    ;
  getchar();

  return 0;
}

Notice that I changed your void main to int main and returned a value. void main is wrong, don't do it.

This article has been dead for over six months. Start a new discussion instead.