I need a statement to exit my program after goto statement but i dont know where to place this statement. The statement is:

printf ("Enter \".\" to exit or \"p\" to proceed");
scanf("%c",&ch);
if (ch=='.')
getch();
if (ch=='p')
goto repeat;

also tell me if its rite or not?

#include<stdio.h>
#include<conio.h>
void main ()
{
int cy,cm,cd,by,bm,bd,age,month,days;
char ch;
clrscr();
repeat:
printf("\nEnter current year:");
scanf("%d",&cy);
printf("\nEnter current month:");
scanf("%d",&cm);
printf("\nEnter current date:");
scanf("%d",&cd);
printf("\nEnter birth year:");
scanf("%d",&by);
printf("\nEnter birth month:");
scanf("%d",&bm);
printf("\nEnter birth date:");
scanf("%d",&bd);
if (cy<=0 || cm<=0 || cd<=0 || by<=0 || bm<=0 || bd<=0)
{
printf("\nEnter correct date");
goto repeat;
}
else
if (cm>=13 || bm>=13 ||cd>=32 ||bd>=32)
{
printf("\nDont exceed the date limit");
goto repeat;
}
else
if (cm<bm && cd<bd)
{
days=30+cd-bd;
month=cm-1+12-bm;
age=cy-1-by;
printf("\nYour age is");
printf("\n%dyears-%dmonths-%ddays",age,month,days);
}
else
if (cm>bm && cd<bd)
{
days=30+cd-bd;
month=cm-1-bm;
age=cy-by;
printf("\nYour age is");
printf("\n%dyears-%dmonths-%ddays",age,month,days);
}
else
if (cm<bm && cd>bd)
{
days=cd-bd;
month=12+cm-bm;
age=cy-1-by;
printf("\nYour age is");
printf("\n%dyears-%dmonths-%ddays",age,month,days);
}
else
if(cm>=bm && cd>=bd)
{
days=cd-bd;
month=cm-bm;
age=cy-by;
printf("\nYour age is ");
printf("\n%dyears-%dmonths-%dage",age,month,days);
}
getch();
}

Recommended Answers

All 9 Replies

Do not use goto: in C applications. It is very bad coding practice (see spaghetti code). Instead, use control structures like conditional statements, loops, etc.

The compiler has less of an ideal of what you're doing, when you use gotos, and people who use them in place of functions might see a dramatic decrease of performance.
Plus it becomes impossible to read, eventually. hence the spaghetti code reference above;

Yes you need to remove the usage of "goto" statement.Below is one of the several ways how you can remove the goto.

repeat:

printf("\nEnter current year:");
scanf("%d",&cy);

printf("\nEnter current month:");
scanf("%d",&cm);

printf("\nEnter current date:");
scanf("%d",&cd);

printf("\nEnter birth year:");
scanf("%d",&by);

printf("\nEnter birth month:");
scanf("%d",&bm);

printf("\nEnter birth date:");
scanf("%d",&bd);

if (cy<=0 || cm<=0 || cd<=0 || by<=0 || bm<=0 || bd<=0)
{
               printf("\nEnter correct date");
               goto repeat;
}

You can change this as:

while(1)
{
             //Your code before the if.

             if (cy>0 && cm>0 && cd>0 && by>0 && bm>0 && bd>0)
            {
                    break;
            } 
            else
            {
                    clrscr();
                    printf("Enter correct date.")
            }
}

As you can see until you get a correct date you will be within while loop only so change your code like this its better coding style as all have said.

And your code doesn't have any problems on first look.

or something like this

bool done = false;
while( !done )
{
             //Your code before the if.

             if (cy>0 && cm>0 && cd>0 && by>0 && bm>0 && bd>0)
            {
                    done = true;
            } 
            else
            {
                    clrscr();
                    printf("Enter correct date.")
            }
}

Excellent usage of the "LOCK" concept of OS in a very simple way in coding ANCIENT DRAGON. GREAT !!!

(Might be a bit difficult for starters to comprehend in the way it ought to be taken.)

Can you explain that? I understand what Ancient Dragon did, but what about the "lock" concept?

Many "normal" people would just call it a conditional loop.

As for the actual code -- I wouldn't clear the screen so that the user can see what he/she did wrong. If you erase it the user may not remember what he did and repeat the error. IMO it is better to just print a couple newlines and leave the rest on the screen.

Thx everybody i understood! and since i've joined daniweb i see much improvement in my work thx again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.