Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Declaration Syntax Error on line 155?[ HELP URGENT] (http://www.go4expert.com/forums/declaration-syntax-error-line-155-help-t19333/)

networkmancer 7Sep2009 20:45

Declaration Syntax Error on line 155?[ HELP URGENT]
 
Written in Turbo C++ so dont tell me its very messy because I know it is and this is an assignment that we must stick in to. The source of the problem is in Case 5 int main { . I just dont know how to fix it.

Code:


#include <stdio.h>
#include <conio.h>
#include<process.h>
#include<dos.h>
void graph();
void foo(){
int a,b;
clrscr();

gotoxy(27,12);textcolor(12);cprintf(" ");
gotoxy(40,14);textcolor(12);cprintf(" ");
gotoxy(29,16);textcolor(12);cprintf("  ");
for(a=1;a<=72;a++){
gotoxy(5+a,2);printf("");
gotoxy(5+a,23);printf("");
}
for(b=1;b<=20;b++){
gotoxy(5,2+b);printf("");
gotoxy(78,2+b);printf("");
}
for(int s=1; s<=3;s++){
gotoxy(41,2+s);printf(""); }
for(int o=1;o<=72;o++){
gotoxy(5+o,5);printf("");
}
gotoxy(5,2);printf("");
gotoxy(78,2);printf("");
gotoxy(5,23);printf("");
gotoxy(78,23);printf("");
gotoxy(41,2);printf("");
gotoxy(5,5);printf("");
gotoxy(78,5);printf("");
gotoxy(41,5);printf("");


getche();


getche();

graph();
}
void graph()
{
clrscr();
int choice;
char z,m;

 gotoxy (12,12);textcolor(4+BLINK); cprintf("LOADING:");
 for(m=1;m<=50;m++){
 gotoxy(19+m,12);textcolor(RED);cprintf("");
 }for (z=1;z<=50;z++){
 gotoxy(19+z,12);textcolor(GREEN);cprintf("");
 delay(40);
 }clrscr();
gotoxy(27,3);textcolor(12);cprintf(" ");
gotoxy(40,4);textcolor(12);cprintf(" ");
gotoxy(29,5);textcolor(12);cprintf(" ");

for(int g=1;g<=72;g++){
gotoxy(5+g,2);printf("");
gotoxy(5+g,23);printf("");
}
for(int v=1;v<=20;v++){
gotoxy(5,2+v);printf("ߺ");
gotoxy(78,2+v);printf("");
}
for(int k=1; k<=3;k++){
gotoxy(41,2+k);printf(""); }
for(int p=1;p<=72;p++){
gotoxy(5+p,6);printf("");
}
gotoxy(5,2);textcolor(GREEN);cprintf(" ");
gotoxy(78,2);textcolor(GREEN);cprintf(" ");
gotoxy(5,23);textcolor(GREEN);cprintf("");
gotoxy(78,23);textcolor(GREEN);cprintf("");
gotoxy(41,2);textcolor(GREEN);cprintf("");
gotoxy(5,6);textcolor(GREEN);cprintf("");
gotoxy(78,6);textcolor(GREEN);cprintf("");
gotoxy(41,6);textcolor(GREEN);cprintf("");
for(int l=1;l<=50;l++){
gotoxy(17+l,7);textcolor(YELLOW);cprintf("");
gotoxy(17+l,20);textcolor(YELLOW);cprintf("");
}
for(int n=1;n<=14;n++)
{
gotoxy(18,6+n);textcolor(YELLOW);cprintf("");
gotoxy(67,6+n);textcolor(YELLOW);cprintf("");
}
gotoxy(37,7);textcolor(9);cprintf("  M E N U  ");
gotoxy(30,9);textcolor(6);cprintf("[1] EVEN/ODD IDENTIFIER");
gotoxy(30,11);textcolor(6);cprintf("[2] 1000x NAME MULTIPLIER");
gotoxy(30,13);textcolor(6);cprintf("[3] MULTIPLICATION TABLE");
gotoxy(30,15);textcolor(6);cprintf("[4] TRIANGLE AREA CALCULATOR");
gotoxy(30,17);textcolor(6);cprintf("[5] P A Y R O L L");
gotoxy(29,19);textcolor(6);cprintf(" [6] E X I T ");
gotoxy(24,22);textcolor(4);cprintf("ENTER CHOICE:");
scanf("%d",&choice);
switch(choice)
{
case 1:int dog;
clrscr();
    printf("Enter number:");
    scanf("%d",&dog);
    if(dog%2==0)
    {
    printf("EVEN \n");
    }
    else
    {
    printf("ODD \n");
    } getche();graph();
    break;
case 2:
 clrscr();
 int e,r,t;
 char name[15];
 printf("Enter your name:");
 scanf("%s",&name);
 gotoxy (12,12);textcolor(GREEN+BLINK); cprintf("LOADING:");
 for(t=1;t<=50;t++){
 gotoxy(19+t,12);textcolor(RED);cprintf("");
 }for (r=1;r<=50;r++){
 gotoxy(19+z,12);textcolor(BLUE);cprintf("");
 delay(40);
 }clrscr();
 for(e=1; e<=1000; e++){
 printf("%s \t",name);
 }
 getche();graph();
 break;
case 3:  clrscr();
    for(int a1=1;a1<=9;a1++)
    {
    for(int a2=1;a2<=9;a2++)
    {
    printf("\t");
    printf("%d",a1*a2);
    }
    printf("\n");
    }
    getche();graph();
    break;
case 4: int a,b;
clrscr();
    printf("Enter base and height:");
    scanf("%d%d",&a,&b);
    printf("Area is %d.",(a*b)/2); getche();graph();
    break;
case 5:

float rate,day,gp,netpay,totald,sss,med,love;int age;
float basic,cola,tax;char employee[20],addr[30],sex[10],bday[8];

int main(){
clrscr();

char choice;
gotoxy(20,2);textcolor(4);cprintf("Payroll System");
gotoxy(20,4);textcolor(5);cprintf("Select Choices:");
gotoxy(18,6);textcolor(7);cprintf("[1] Add Customer Info");
gotoxy(18,7);textcolor(7);cprintf("[2] Compute Payroll");
gotoxy(18,8);textcolor(7);cprintf("[3] Display Information");
gotoxy(18,9);textcolor(7);cprintf("[4] Exit");
gotoxy(18,10);textcolor(7);cprintf("Enter Choice:");
scanf("%d",&choice);
switch (choice){
case 1:printf("Employee Information\n\n");
    printf("Enter Employee name:");
    scanf("%s",&employee);
    printf("Enter address:");
    scanf("%s",&addr);
    printf("Enter sex:");
    scanf("%s",&sex);
    printf("Enter age:");
    scanf("%d",&age);
    printf("Enter bday:");
    scanf("%s",&bday);main();

case 2:printf("Payroll Input\n:");
    printf("Enter rate:");
    scanf("%f",&rate);
    printf("Enter day:");
    scanf("%f",&day);
    basic=rate*day;
    cola=basic*.02;
    gp=basic+cola;
    sss=gp*.073;
    med=gp*.081;
    love=gp*.092;

    if(gp>=2500){
        tax=gp*.12;}else
        {
        printf("No tax");}

    totald=sss+med+love+tax;
    netpay=gp-totald;main();

case 3:printf("Employee Information\n");
    printf("Employee name is %s\n",employee);
    printf("Employee address %s\n",addr);
    printf("Employee gender%c\n",sex);
    printf("Employee age %d\n",age);
    printf("Employee bday%s\n",bday);
    printf("\n");
    printf("payroll Information\n");
    printf("Rate per day%.2f\n",rate);
    printf("Number of day%.2f\n",day);
    printf("Basic%.2f\n",basic);
    printf("Cola %.2f\n",cola);
    printf("Grosspay%.2f\n",gp);

printf("Tax is%.2f\n",tax);
    printf("Total deduction %.2f\n",totald);
    printf("netpay%.2f\n",netpay);getche();main();

case 4: exit(0);
default:printf("Invalid input");getche();main();
}
getche();graph();
break;
    }
case 6: exit(0);break;
default:printf("Invalid Entry");
}
getche();}


xpi0t0s 7Sep2009 21:14

Re: Declaration Syntax Error on line 155?[ HELP URGENT]
 
You're trying to declare main within the graph() function. C does not support nested functions so to fix this you have to move the declaration of main() to after the graph() function.

So what you're trying is this:
Code:

void graph()
{
    // ...
    int main(){
    // ...
    }
}

and the fix is this:
Code:

void graph()
{
    // ...
}

int main(){
// ...
}

Use of correct indentation will help a lot. I strongly advise putting the code through a code beautifier.

networkmancer 8Sep2009 05:10

Re: Declaration Syntax Error on line 155?[ HELP URGENT]
 
How can I close it ? because case 5 must return to its menu but i cannot put int main(){ inside it because when I will use int main outside case 5 it will just lead to the main menu and not the menu in case 5. I really need it badly. :embarasse

Code:

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

void graph();
void foo(){
int choice;
char z,m;

 gotoxy (12,12);textcolor(4+BLINK); cprintf("LOADING:");
 for(m=1;m<=50;m++){
 gotoxy(19+m,12);textcolor(RED);cprintf("");
 }for (z=1;z<=50;z++){
 gotoxy(19+z,12);textcolor(GREEN);cprintf("");
 delay(40);
 }clrscr();
gotoxy(27,3);textcolor(12);cprintf(" ");
gotoxy(40,4);textcolor(12);cprintf(" ");
gotoxy(29,5);textcolor(12);cprintf(" ");

for(int g=1;g<=72;g++){
gotoxy(5+g,2);printf("");
gotoxy(5+g,23);printf("");
}
for(int v=1;v<=20;v++){
gotoxy(5,2+v);printf("ߺ");
gotoxy(78,2+v);printf("");
}
for(int k=1; k<=3;k++){
gotoxy(41,2+k);printf(""); }
for(int p=1;p<=72;p++){
gotoxy(5+p,6);printf("");
}
gotoxy(5,2);textcolor(GREEN);cprintf(" ");
gotoxy(78,2);textcolor(GREEN);cprintf(" ");
gotoxy(5,23);textcolor(GREEN);cprintf("");
gotoxy(78,23);textcolor(GREEN);cprintf("");
gotoxy(41,2);textcolor(GREEN);cprintf("");
gotoxy(5,6);textcolor(GREEN);cprintf("");
gotoxy(78,6);textcolor(GREEN);cprintf("");
gotoxy(41,6);textcolor(GREEN);cprintf("");
for(int l=1;l<=50;l++){
gotoxy(17+l,7);textcolor(YELLOW);cprintf("");
gotoxy(17+l,20);textcolor(YELLOW);cprintf("");
}
for(int n=1;n<=14;n++)
{
gotoxy(18,6+n);textcolor(YELLOW);cprintf("");
gotoxy(67,6+n);textcolor(YELLOW);cprintf("");
}
gotoxy(37,7);textcolor(9);cprintf("  M E N U  ");
gotoxy(30,9);textcolor(6);cprintf("[1] EVEN/ODD IDENTIFIER");
gotoxy(30,11);textcolor(6);cprintf("[2] 1000x NAME MULTIPLIER");
gotoxy(30,13);textcolor(6);cprintf("[3] MULTIPLICATION TABLE");
gotoxy(30,15);textcolor(6);cprintf("[4] TRIANGLE AREA CALCULATOR");
gotoxy(30,17);textcolor(6);cprintf("[5] P A Y R O L L");
gotoxy(29,19);textcolor(6);cprintf(" [6] E X I T ");
gotoxy(24,22);textcolor(4);cprintf("ENTER CHOICE:");
scanf("%d",&choice);
switch(choice)
{
case 1:codes...
case 2:codes.
case 3:codes......
case 4:coes....

case 5:

int main(){

float rate,day,gp,netpay,totald,sss,med,love;int age;
float basic,cola,tax;char employee[20],addr[30],sex[10],bday[8];

clrscr();

char choice;
gotoxy(20,2);textcolor(4);cprintf("Payroll System");
gotoxy(20,4);textcolor(5);cprintf("Select Choices:");
gotoxy(18,6);textcolor(7);cprintf("[1] Add Customer Info");
gotoxy(18,7);textcolor(7);cprintf("[2] Compute Payroll");
gotoxy(18,8);textcolor(7);cprintf("[3] Display Information");
gotoxy(18,9);textcolor(7);cprintf("[4] Exit");
gotoxy(18,10);textcolor(7);cprintf("Enter Choice:");
scanf("%d",&choice);
switch (choice){
case 1:printf("Employee Information\n\n");
    printf("Enter Employee name:");
    scanf("%s",&employee);
    printf("Enter address:");
    scanf("%s",&addr);
    printf("Enter sex:");
    scanf("%s",&sex);
    printf("Enter age:");
    scanf("%d",&age);
    printf("Enter bday:");
    scanf("%s",&bday);main();

case 2:printf("Payroll Input\n:");
    printf("Enter rate:");
    scanf("%f",&rate);
    printf("Enter day:");
    scanf("%f",&day);
    basic=rate*day;
    cola=basic*.02;
    gp=basic+cola;
    sss=gp*.073;
    med=gp*.081;
    love=gp*.092;

    if(gp>=2500){
        tax=gp*.12;}else
        {
        printf("No tax");}

    totald=sss+med+love+tax;
    netpay=gp-totald;main();

case 3:printf("Employee Information\n");
    printf("Employee name is %s\n",employee);
    printf("Employee address %s\n",addr);
    printf("Employee gender%c\n",sex);
    printf("Employee age %d\n",age);
    printf("Employee bday%s\n",bday);
    printf("\n");
    printf("payroll Information\n");
    printf("Rate per day%.2f\n",rate);
    printf("Number of day%.2f\n",day);
    printf("Basic%.2f\n",basic);
    printf("Cola %.2f\n",cola);
    printf("Grosspay%.2f\n",gp);

printf("Tax is%.2f\n",tax);
    printf("Total deduction %.2f\n",totald);
    printf("netpay%.2f\n",netpay);getche();main();

case 4: exit(0);
default:printf("Invalid input");getche();main();
}
getche();graph();
break;
}

getche();



here is the separate code for Case 5:

Code:

#include<stdio.h>
#include<conio.h>
#include<process.h>
float rate,day,gp,netpay,totald,sss,med,love;int age;
float basic,cola,tax;char employee[20],addr[30],sex[10],bday[8];

void main(){
clrscr();

char choice;
gotoxy(20,2);textcolor(4);cprintf("Payroll System");
gotoxy(20,4);textcolor(5);cprintf("Select Choices:");
gotoxy(18,6);textcolor(7);cprintf("[1] Add Customer Info");
gotoxy(18,7);textcolor(7);cprintf("[2] Compute Payroll");
gotoxy(18,8);textcolor(7);cprintf("[3] Display Information");
gotoxy(18,9);textcolor(7);cprintf("[4] Exit");
gotoxy(18,10);textcolor(7);cprintf("Enter Choice:");
scanf("%d",&choice);
switch (choice){
case 1:printf("Employee Information\n\n");
    printf("Enter Employee name:");
    scanf("%s",&employee);
    printf("Enter address:");
    scanf("%s",&addr);
    printf("Enter sex:");
    scanf("%s",&sex);
    printf("Enter age:");
    scanf("%d",&age);
    printf("Enter bday:");
    scanf("%s",&bday);main();break;

case 2:printf("Payroll Input\n:");
    printf("Enter rate:");
    scanf("%f",&rate);
    printf("Enter day:");
    scanf("%f",&day);
    basic=rate*day;
    cola=basic*.02;
    gp=basic+cola;
    sss=gp*.073;
    med=gp*.081;
    love=gp*.092;

    if(gp>=2500){
        tax=gp*.12;}else
        {
        printf("No tax");}

    totald=sss+med+love+tax;
    netpay=gp-totald;main();break;

case 3:printf("Employee Information\n");
    printf("Employee name is %s\n",employee);
    printf("Employee address %s\n",addr);
    printf("Employee gender%c\n",sex);
    printf("Employee age %d\n",age);
    printf("Employee bday%s\n",bday);
    printf("\n");
    printf("payroll Information\n");
    printf("Rate per day%.2f\n",rate);
    printf("Number of day%.2f\n",day);
    printf("Basic%.2f\n",basic);
    printf("Cola %.2f\n",cola);
    printf("Grosspay%.2f\n",gp);

printf("Tax is%.2f\n",tax);
    printf("Total deduction %.2f\n",totald);
    printf("netpay%.2f\n",netpay);getche();main();
    break;
case 4: exit(0);break;
default:printf("Invalid input");getche();main();
}
getche();}


xpi0t0s 8Sep2009 13:27

Re: Declaration Syntax Error on line 155?[ HELP URGENT]
 
Ending a case block with a recursive call back into main is a very bad idea. Trust me on this; you definitely do not want to do that. What I mean is this:
Code:

case 1:
  // some code (skipped)
  main();
  break;

Instead what you need is a loop construct, for example:
Code:

for (int exitflag=0; !exitflag; )
{
  // display the menu here
  switch (choice)
  {
    case 1:
      // case 1 stuff
      break;

    case 2:
      // case 2 stuff
      break;

    // other cases

    case 5:
      // case 5 stuff
      break;

    case 6:
      exitflag=1;
      break;
  }
}

This will loop until exitflag is set to 1, which is done in case 6. When case 6 is selected, the loop will terminate. After the other cases have run, because of the loop they will return to the point where the menu is displayed, which I think is what you want.

Try simplifying the code until you've got the structure sorted out. There is a lot of clutter in the program, and with the lack of formatting it's no surprise you can't figure it out. *I* wouldn't be able to figure it out given the state of the code, and my first step would be to reformat the code. (Actually my first step would be to get an exact spec of the project and find out what the code is supposed to do.)


All times are GMT +5.5. The time now is 10:17.