954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Quick Question

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
#include

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

Drew06
Newbie Poster
3 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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..

Drew06
Newbie Poster
3 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks alot!

Drew06
Newbie Poster
3 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You