scanf not scanning
Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: scanf not scanning

  1. #1
    Senior Member
    Join Date
    Dec 2001
    Posts
    134

    scanf not scanning

    hello guys, i am facing the problem, it is a very simple one, but the solution is not striking right now, the following is the code that i have typed, but there is a problem in scanning the value of character variable 'chy', the compiler is not stopping to accept the value.
    the scanf line which is bold and in italics is the one with the problem, i think so. following is the code.
    -------------------------------------------------------------------------------------------------------
    #include<stdio.h>

    #include<math.h>
    int main()
    {
    double pie,theth,dis,velo=0.0;

    char chy;
    char ty;
    do
    {

    printf("\nEnter the angle theta: ");
    scanf("%lg",&theth);

    printf("\n Enter the angle pie: ");
    scanf("%lg",&pie);

    printf("\nEnter the velocity of the projectile: ");
    scanf("%lg",&velo);

    pie=3.1415927/180.0*pie;
    theth=3.1415927/180.0*theth;

    dis=2.0*velo*velo*cos(theth)*cos(theth)*(tan(theth)-tan(pie)/(9.81 * cos(pie)));

    printf("\nThe Distance is: %g",dis);

    printf("\nDo You whish for another calculations [Y|N]: ");
    scanf("%c", &chy);

    printf("\nthe valu of n and N is: %c, %c",chy,ty);
    }while((chy != 144) || (chy != 78));

    return 0;
    }

    -----------------------------------------------------------------------------------------------------------
    please let me know about the problem.
    thank you for your time and patience.
    Regards
    Harbir
    U get What U pay for.

  2. #2
    Banned
    Join Date
    Jun 2003
    Posts
    1,302
    I might be wrong, I haven't done C in a very long time. But shouldn't that be ch, and not C?

  3. #3
    no, the line is correct, but i've found exactly the same problem from time to time, if i use scan in an if() statement, i also have the same problem with char.

    why don't you try:

    chy = getchar();

    instead of your line?

  4. #4
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    I was just about to say the same thing. scanf() is a very fussy function. Sometimes, it just refuses to scan. To get single letters from input, I find getchar() to be much much better.

    Cheers,
    cgkanchi
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

  5. #5
    Senior Member
    Join Date
    Jul 2003
    Posts
    813
    Hmm a quick question.. if somebody were to insert more than one character, wouldn't the next chars be passed on to whatever other function would 'buy' them? This could result in an overflow. I think it does depend on the compiler because on some I did have that problem, while on others I didn't.

    But yeah, your best bet would be to do a getchar(), it should simplify things greatly.
    /\\

  6. #6
    AntiOnline n00b
    Join Date
    Feb 2004
    Posts
    665
    hi

    here you go...cant use Characters like that........... use String instead .....or the .....getche() method ...getche() looks more ...cool ...you don't have to press Return after entering the Character..I have included both methods...and a few alteratio to the code hope you don't mind ...the code i have altered or added i have made it Bold and Italics

    It's been a long since i worked on C ..it was a good feeling after that long time......

    .Code works on Turbo C/C++..... Visual C++ ....can't say about other compilers...
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    int main()
      {
         double pie,theth,dis,velo=0.0;
         char ch;
    
               do
               {
    
                   printf("\nEnter the angle theta: ");
                   scanf("%lg",&theth);
    
                   printf("\n Enter the angle pie: ");
                   scanf("%lg",&pie);
    
                   printf("\nEnter the velocity of the projectile: ");
                   scanf("%lg",&velo);
    
                   pie=3.1415927/180.0*pie;
                   theth=3.1415927/180.0*theth;
    
                   dis=2.0*velo*velo*cos(theth)*cos(theth)*(tan(theth)-tan(pie)/(9.81 * cos(pie)));
    
                    printf("The Distance is:%lg\n",dis);
      
                    printf("\nContinue? Y/N: ");
                    scanf("%s",&ch);
                    //ch=getche();
    
    
    
                }while(toupper(ch)=='Y');
    
    getch();
    return 0;
    }
    --Good Luck--

  7. #7
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    AFAIK, getch() and getche() aren't ANSI C functions. Therefore, you are not guaranteed that they will work. I know for a fact that it doesn't work on gcc and that it works on Turbo/Borland C++.

    Cheers,
    cgkanchi
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

  8. #8
    Senior Member
    Join Date
    Dec 2001
    Posts
    134
    Thanx Guys,
    the code works well with %s after including #inlcude<stdio.h>

    cheers
    U get What U pay for.

  9. #9
    AntiOnline n00b
    Join Date
    Feb 2004
    Posts
    665
    Hi

    Yes Antionline Herpetologist is right they are not ..i have always worked on Turbo C ++ that i sometimes forget that there are other Compilers as well and about the standard ANSI C functions.

    The Above Code will work without them ...Just delete the Getch(); at the end ...its of no use there ..i write that as a habit .....otherwise the DOS window Dissapers without even letting me see the result....it's just there to stop it after the whole program is finished .

    --Good Luck--

  10. #10
    Thanx Guys,
    the code works well with %s after including #inlcude<stdio.h>

    cheers
    the code you've printed in your first post, already had "#include <stdio.h>" in it, so this makes no sense

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides