if-else Declaration Terminated Incorrectly

Declaration Terminated Incorrectly

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2621 Views - Last Post: 09 January 2011 - 02:13 PM Rate Topic: -----

#1 Guest_ciel*


Reputation:

if-else Declaration Terminated Incorrectly

Posted 03 January 2011 - 01:16 PM

The "declaration terminated incorrectly" error message.
I've tried moving the parentheses around a bit, but this just causes more errors. What exactly is the problem here? Any help is greatly appreciated. Thank you.


if (return == 0)
  {
   printf("INCORRECT");
  }
else 
  {
   printf ("CORRECT");
  }

This post has been edited by JackOfAllTrades: 08 January 2011 - 10:17 AM
Reason for edit:: Added code tags

Is This A Good Question/Topic? 0

Replies To: if-else Declaration Terminated Incorrectly

#2 GWatt  Icon User is offline

  • member icon

Reputation: 257
  • View blog
  • Posts: 3,035
  • Joined: 01-December 05

Re: if-else Declaration Terminated Incorrectly

Posted 03 January 2011 - 01:20 PM

return is a reserved word in C. Call your variable something else.
Was This Post Helpful? 1
  • +
  • -

#3 Guest_ciel*


Reputation:

Re: if-else Declaration Terminated Incorrectly

Posted 03 January 2011 - 01:32 PM

View PostGWatt, on 03 January 2011 - 12:20 PM, said:

return is a reserved word in C. Call your variable something else.


Thank you...ahm but i used the return like this:

printf("\n\nInput alg. expression:\n");

     scanf("%i%c%i%c%i%c%i%c%i",&q1,&sym,&q2,&sym,&q3,&sym,&q4,&sym,&q5);

    if(sym=='+')
	printf("\n%i",add(q1,q2,q3,q4,q5));
    if(sym=='-')
	printf("\n%i",sub(q1,q2,q3,q4,q5));
    if(sym=='*' || sym=='(' || sym==')')
	printf("\n%i",product(q1,q2,q3,q4,q5));
    if(sym=='(')
	printf("\n%i",product(q1,q2,q3,q4,q5));
    if(sym==')')
	printf("\n%i",product(q1,q2,q3,q4,q5));
    if(sym=='/')
        printf("%i",divide(q1,q2,q3,q4,q5));

    printf("\nDo you wish to continue[y/n]");
    scanf("%s",&choice);
    if(choice=='y'||choice=='Y')
	main();
}


int add(int m1,int m2,int m3,int m4,int m5)
{
    return(m1+m2+m3+m4+m5);
}

int sub(int m1,int m2,int m3,int m4,int m5)
{
    return(m1-m2-m3-m4-m5);
}

int product(int m1,int m2,int m3,int m4,int m5)
{
    return(m1*m2*m3*m4*m5);
}

int divide(int m1,int m2,int m3,int m4,int m5)
{
    return(m1/m2/m3/m4/m5);
}



 if (return == 0)
  {printf("INCORRECT");}
 else {printf ("CORRECT");}

Was This Post Helpful? 0

#4 GWatt  Icon User is offline

  • member icon

Reputation: 257
  • View blog
  • Posts: 3,035
  • Joined: 01-December 05

Re: if-else Declaration Terminated Incorrectly

Posted 03 January 2011 - 01:55 PM

First off, :code:
I didn't mention anything at first as it was only a few lines of code

Second, return is reserved for the purpose of exiting a function. If you specify a value with return that value can be used from wherever you called your function.
int add(int a, int B)/> {
    return a + b;
}

int main() {
    int sum = add(5, 7);
    printf("5 + 7 = %d\n", sum);

    return 0;
}


return is not a variable. You cannot assign a value to it, retrieve a value from it, get its address, or anything else you would do with a variable. Using return like you have in your arithmetic functions (add, sub, product, divide) is correct but that usage does not assign the value of whatever expression you use with return to return. It's also not a function. You don't need the parenthesis around the expression you return, but it does nothing wrong either.

Also, it looks like
if (return == 0) {
    printf("INCORRECT");
}
else {
    printf ("CORRECT");
}


is outside of a function which is wrong and will not compile either. You don't need to check the return value of your main function, if that's what you're trying to do.
Was This Post Helpful? 1
  • +
  • -

#5 CodeGrappler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 120
  • Joined: 29-November 10

Re: if-else Declaration Terminated Incorrectly

Posted 03 January 2011 - 01:57 PM

*EDIT* Damn you GWatt; beat me to it.

Use code tags!

[ code] Insert code here [/code ] (without spaces between brackets)

Also NEVER call main manually it is bad!


int add(int m1,int m2,int m3,int m4,int m5)
{
return(m1+m2+m3+m4+m5);
}

int sub(int m1,int m2,int m3,int m4,int m5)
{
return(m1-m2-m3-m4-m5);
}

int product(int m1,int m2,int m3,int m4,int m5)
{
return(m1*m2*m3*m4*m5);
}

int divide(int m1,int m2,int m3,int m4,int m5)
{
return(m1/m2/m3/m4/m5);
}

if (return == 0)
{printf("INCORRECT");}
else {printf ("CORRECT");} 


This makes no sense at all. You have if statements floating in the middle of no where. And you didn't even change the variable name. As was stated return is a reserved keyword.

This post has been edited by CodeGrappler: 03 January 2011 - 01:58 PM

Was This Post Helpful? 1
  • +
  • -

#6 Guest_ciel*


Reputation:

Re: if-else Declaration Terminated Incorrectly

Posted 03 January 2011 - 02:06 PM

Wow!!!! Thank you so much that's really a great help ^_^

i changed my variable to m :

if (m == 0)
{printf("INCORRECT");}
else {printf ("CORRECT");}


but it still has the error "Declaration terminated incorrectly"
why is that? :(
Was This Post Helpful? 0

#7 Guest_ciel*


Reputation:

Re: if-else Declaration Terminated Incorrectly

Posted 03 January 2011 - 02:11 PM

View PostCodeGrappler, on 03 January 2011 - 12:57 PM, said:

*EDIT* Damn you GWatt; beat me to it.

Use code tags!

[ code] Insert code here [/code ] (without spaces between brackets)

Also NEVER call main manually it is bad!


int add(int m1,int m2,int m3,int m4,int m5)
{
return(m1+m2+m3+m4+m5);
}

int sub(int m1,int m2,int m3,int m4,int m5)
{
return(m1-m2-m3-m4-m5);
}

int product(int m1,int m2,int m3,int m4,int m5)
{
return(m1*m2*m3*m4*m5);
}

int divide(int m1,int m2,int m3,int m4,int m5)
{
return(m1/m2/m3/m4/m5);
}

if (return == 0)
{printf("INCORRECT");}
else {printf ("CORRECT");} 


This makes no sense at all. You have if statements floating in the middle of no where. And you didn't even change the variable name. As was stated return is a reserved keyword.



actually, my prof. told us to have Incorrect/Correct for the result that's why i have the Incorrect/Correct
Was This Post Helpful? 0

#8 GWatt  Icon User is offline

  • member icon

Reputation: 257
  • View blog
  • Posts: 3,035
  • Joined: 01-December 05

Re: if-else Declaration Terminated Incorrectly

Posted 03 January 2011 - 02:12 PM

:code:
This is the third time someone has asked you to use them. Code tags improve readability.
What you've written should work. Did you put that inside of a function or is it hanging around in the middle of nowhere? If you need to, post the entire program, but please :code:
Was This Post Helpful? 2
  • +
  • -

#9 CodeGrappler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 120
  • Joined: 29-November 10

Re: if-else Declaration Terminated Incorrectly

Posted 03 January 2011 - 02:14 PM

You really need to post the entire code file.

You have posted an insufficient amount of data for us to be able to properly help you.

Also stop using Turbo-C++.
Was This Post Helpful? 2
  • +
  • -

#10 Guest_ciel*


Reputation:

Re: if-else Declaration Terminated Incorrectly

Posted 03 January 2011 - 02:17 PM

View PostGWatt, on 03 January 2011 - 01:12 PM, said:

:code:
This is the third time someone has asked you to use them. Code tags improve readability.
What you've written should work. Did you put that inside of a function or is it hanging around in the middle of nowhere? If you need to, post the entire program, but please :code:


ohh..im so sorry about that hehe :) here:

#include<stdio.h>
#include<conio.h>

int add(int q1,int q2,int q3,int q4,int q5);
int sub(int q1,int q2,int q3,int q4,int q5);
int product(int q1,int q2,int q3,int q4,int q5);
int divide(int q1,int q2,int q3,int q4,int q5);

void main()
{
    int q1,q2,q3,q4,q5;
    char sym,choice;
    clrscr();

     printf("\n\nInput alg. expression:\n");

     scanf("%i%c%i%c%i%c%i%c%i",&q1,&sym,&q2,&sym,&q3,&sym,&q4,&sym,&q5);

    if(sym=='+')
	printf("\n%i",add(q1,q2,q3,q4,q5));
    if(sym=='-')
	printf("\n%i",sub(q1,q2,q3,q4,q5));
    if(sym=='*' || sym=='(' || sym==')')
	printf("\n%i",product(q1,q2,q3,q4,q5));
    if(sym=='(')
	printf("\n%i",product(q1,q2,q3,q4,q5));
    if(sym==')')
	printf("\n%i",product(q1,q2,q3,q4,q5));
    if(sym=='/')
        printf("%i",divide(q1,q2,q3,q4,q5));

    printf("\nDo you wish to continue[y/n]");
    scanf("%s",&choice);
    if(choice=='y'||choice=='Y')
	main();
}


int add(int m1,int m2,int m3,int m4,int m5)
{
    return(m1+m2+m3+m4+m5);
}

int sub(int m1,int m2,int m3,int m4,int m5)
{
    return(m1-m2-m3-m4-m5);
}

int product(int m1,int m2,int m3,int m4,int m5)
{
    return(m1*m2*m3*m4*m5);
}

int divide(int m1,int m2,int m3,int m4,int m5)
{
    return(m1/m2/m3/m4/m5);
}



 if (m == 0)
  {
  printf("INCORRECT");
  }
 else
  {
  printf ("CORRECT");
  }


This post has been edited by Martyr2: 03 January 2011 - 02:44 PM
Reason for edit:: Please use code tags in the future, thanks!

Was This Post Helpful? 0

#11 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3872
  • View blog
  • Posts: 11,405
  • Joined: 18-April 07

Re: if-else Declaration Terminated Incorrectly

Posted 03 January 2011 - 02:44 PM

It appears to be hanging out in the middle of no where. If statements must be inside a function. You need to move the if statement inside of main and also you need to define the variable "m" before you can use it. Lastly, as previously mentioned, you should not be calling main() from a function... and especially not within main().

:)
Was This Post Helpful? 2
  • +
  • -

#12 Guest_ciel*


Reputation:

Re: if-else Declaration Terminated Incorrectly

Posted 08 January 2011 - 08:18 AM

View PostMartyr2, on 03 January 2011 - 01:44 PM, said:

It appears to be hanging out in the middle of no where. If statements must be inside a function. You need to move the if statement inside of main and also you need to define the variable "m" before you can use it. Lastly, as previously mentioned, you should not be calling main() from a function... and especially not within main().

:)


wow...thanks a lot... ^_^ sorry it took me so long before im online again
i change my code...but there's still error
the errors are: Declaration syntax error and Declaration missing ;
why is that? thanks again :)

#include<stdio.h>
#include<conio.h>

int add(int q1,int q2,int q3,int q4,int q5);
int sub(int q1,int q2,int q3,int q4,int q5);
int product(int q1,int q2,int q3,int q4,int q5);
int divide(int q1,int q2,int q3,int q4,int q5);

void func()
{ int m;
int add(int m1,int m2,int m3,int m4,int m5)
{
    m=(m1+m2+m3+m4+m5); return m;
}

int sub(int m1,int m2,int m3,int m4,int m5)
{
    m=(m1-m2-m3-m4-m5); return m;
}

int product(int m1,int m2,int m3,int m4,int m5)
{
    m=(m1*m2*m3*m4*m5); return m;
}

int divide(int m1,int m2,int m3,int m4,int m5)
{
    m=(m1/m2/m3/m4/m5); return m;
}
 if (m == 0)
  {
  printf("INCORRECT");
  }
 else
  {
  printf ("CORRECT");
  }
}

void main()
{
    int q1,q2,q3,q4,q5;

    char sym,choice;
	clrscr();
     printf("\n\nInput alg. expression:\n");

     scanf("%i%c%i%c%i%c%i%c%i",&q1,&sym,&q2,&sym,&q3,&sym,&q4,&sym,&q5);

    if(sym=='+')
	printf("\n%i",add(q1,q2,q3,q4,q5));
    if(sym=='-')
	printf("\n%i",sub(q1,q2,q3,q4,q5));
    if(sym=='*' || sym=='(' || sym==')')
	printf("\n%i",product(q1,q2,q3,q4,q5));
    if(sym=='(')
	printf("\n%i",product(q1,q2,q3,q4,q5));
    if(sym==')')
	printf("\n%i",product(q1,q2,q3,q4,q5));
    if(sym=='/')
	printf("%i",divide(q1,q2,q3,q4,q5));

    printf("\nDo you wish to continue[y/n]");
    scanf("%s",&choice);
    if(choice=='y'||choice=='Y')
    func();

}


MOD EDIT: When posting code...AGAIN!!! USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 08 January 2011 - 10:18 AM

Was This Post Helpful? 0

#13 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 145
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: if-else Declaration Terminated Incorrectly

Posted 08 January 2011 - 08:39 AM

You forgot to post in code tags :code:

And i found this in your code
void main()
never use void main it's
int main()

Also the conio library is outdated, it's not really good to use.

This post has been edited by Alex6788: 08 January 2011 - 08:40 AM

Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is offline

  • member icon

Reputation: 3041
  • View blog
  • Posts: 9,274
  • Joined: 25-December 09

Re: if-else Declaration Terminated Incorrectly

Posted 08 January 2011 - 09:08 AM

Where are your code tags?????

You are trying to define a function within a function which is wrong.

void func()
{ int m;
int add(int m1,int m2,int m3,int m4,int m5)


Please study the following links: Program structure, Functions I, Functions II.


Jim
Was This Post Helpful? 1
  • +
  • -

#15 Guest_ciel*


Reputation:

Re: if-else Declaration Terminated Incorrectly

Posted 08 January 2011 - 09:53 AM

View PostAlex6788, on 08 January 2011 - 07:39 AM, said:

You forgot to post in code tags :code:

And i found this in your code
void main()
never use void main it's
int main()

Also the conio library is outdated, it's not really good to use.


ahh..thanks you :) i remove the <#include conio.h> thanks again :)
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2