function call in c not working???
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: function call in c not working???

  1. #1
    Join Date
    Feb 2012
    Posts
    6

    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();
    }

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    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]
    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

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  3. #3
    Join Date
    Feb 2012
    Posts
    6

    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();
    }

  4. #4
    Join Date
    Apr 1999
    Posts
    27,433

    Re: function call in c not working???

    Quote Originally Posted by nikku.smartie View Post
    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

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,315

    Re: function call in c not working???

    Quote 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++.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Apr 1999
    Posts
    27,433

    Re: function call in c not working???

    Quote Originally Posted by laserlight View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center