can someone please tell me why after input y from terminal this code not going to main function??
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
char answer;
int a,b,y;
printf ("Please enter any number and then FORGET it\n\n\n");
scanf ("%d",&a);
system("cls");
printf ("Now think a different number.....and press any number key when you done...\n\n\n");
scanf ("%d",&y);
clrscr();
printf ("now make it DOUBLE and press any number key when you done...\n\n\n");
scanf ("%d",&y);
b = (a>=0) ? ((a>=10) ? ((a>=20) ? ((a>=30) ? ((a>=40) ? ((a>=50) ? ((a>=60) ? ((a>=70) ? ((a>=80) ? ((a>=90) ? ((a>=100) ? 50 : 98) : 110) : 144) : 168) : 182) : 208) : 226) : 280) : 368) : 420) : 500;
clrscr();
printf ("now ADD %d to it....and press any number key when you done...\n\n\n",b);
scanf ("%d",&y);
system("cls");
printf ("now make it HALF....press number key when you done... \n\n\n");
scanf ("%d",&y);
clrscr();
printf ("from it SUBSTRACT the number you thought in the beginnening\n\n\n");
scanf ("%d",&y);
clrscr();
printf ("\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
printf ("\n The ANSWER IS %d",b/2);
printf ("\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\n");
printf ("\t\tWanna More??\n\n??Y/N");
answer = getchar();
if (answer == 'y')
main();
getch();
}
You should never ever call main again like that. Use a while loop instead.
Also, when posting code you should embed it within code tags to preserve intendation [code]paste code here[/code]
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
First, your program is not C. It is C++. The reason why I know this is the following:
Code:
clrscr();
char answer;
In 'C', variables must be declared at the top of the functional block. You did not do that -- you declared "answer" after you called clrscr(). So your code could never be 'C', so it must be C++.
Second, what is clrscr()? What compiler are you using to write this code?
Third, given that your program is C++, it is illegal to call main() directly in a C++ program. So not only is your program wrong, your compiler is wrong allowing you to do this. So what compiler are you using?
In 'C', variables must be declared at the top of the functional block. You did not do that -- you declared "answer" after you called clrscr(). So your code could never be 'C', so it must be C++.
This is not a requirement in C as per the 1999 edition of the C standard, so this does not demonstrate that the code must be C++.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
This is not a requirement in C as per the 1999 edition of the C standard, so this does not demonstrate that the code must be C++.
It looks like the OP is using a Borland or Turbo C++ compiler, since those compilers had the clrscr() function. If so, then those compilers are based on C89.
Bookmarks