-
February 18th, 2012, 04:38 PM
#1
function call in c not working???
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();
}
-
February 18th, 2012, 04:51 PM
#2
Re: function call in c not working???
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]
-
February 19th, 2012, 04:06 AM
#3
Re: function call in c not working???
Do You Mean This??
Code:
printf ("\t\tWanna More??\n\n??Y/N");
while (answer != 'y')
{
answer = getchar();
main();
}
-
February 19th, 2012, 04:48 AM
#4
Re: function call in c not working???
Originally Posted by nikku.smartie
Do You Mean This??
No. It means you don't call main() directly.
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?
Regards,
Paul McKenzie
-
February 19th, 2012, 05:24 AM
#5
Re: function call in c not working???
Originally Posted by Paul McKenzie
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++.
-
February 19th, 2012, 07:05 AM
#6
Re: function call in c not working???
Originally Posted by laserlight
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.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|