scanf not scanning - Page 2
Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: scanf not scanning

  1. #11
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    Originally posted here by cgkanchi
    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++.
    getch() and getche() (and some other console functions) are included in conio.h. But yes, they aren't ANSI C functions. And conio.h doesn't come with (original) gcc. However, MinGW port of gcc (and Dev-CPP and cygwin's gcc which uses MinGW) does have conio.h header file.

    So if you have C code which uses getch() or getche() that works on Turbo/Borland C++ or VC++, just do #include <conio.h>, and it will work on MinGW and Dev-CPP.

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  2. #12
    AntiOnline n00b
    Join Date
    Feb 2004
    Posts
    665
    hi

    Originally posted here by lepricaun
    the code you've printed in your first post, already had "#include <stdio.h>" in it, so this makes no sense
    lol lepricaun he ment #include <String.h>.......not <studio.h>.....typing error..

    So if you have C code which uses getch() or getche() that works on Turbo/Borland C++ or VC++, just do #include <conio.h>, and it will work on MinGW and Dev-CPP.
    Same is the case is with Turbo C++ ...you will have to #inclue <conio.h> to get them working.. ...So what is the difference..sorry for my ignorance...the only C/C++ Compilers i have used are Turbo, Borland and Visul C++....

    --Good Luck--

  3. #13
    Senior Member
    Join Date
    Dec 2001
    Posts
    134
    Sorry i ment #include<string.h>
    U get What U pay for.

  4. #14
    ok, i get the point , i'm learning C myself at the moment, so thats the reason why i didn't saw you meant strings.h, but hey, at least i've learned something new from it now :lol:

  5. #15
    Banned
    Join Date
    Apr 2004
    Posts
    94
    all of the above replies makes sense but i have got the real reason why this isn't working! this is because u must be using Browland C++ 3.0 version of turbo c++, the compiler of this version is that the compiler gives problems sometimes for scanning the value with scanf().
    what u have to do is to get a C compiler which will definitely work! i m sure about this!

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

    First of all Welcome to AO...........

    Yes Trubo C++ will always give you this problem of not scanning the character variable if used just after some other variable in this faschion......it will just ignore it and go to the next statement .........it would have scaned the same value if it would have been the first value you have scanned .....i have no idea why.......apart from changing the compiler.......more simple solution i think is to use string %s it solves this problem...or specify the index number ch[0] where to store the value......that solves this problem........I had encountered this problem in the very early days of my learning C and at that time it got me soooo pissed ........because i just couldn't make out what the problem was ..........so so now that he has brought up the issue i would too like to know the reason if anybody knows........

    If a few people didn't understand what akshayakrsh and i am trying to say here is look at the following code

    Code:
    
    #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
    	char ch;
    	int num;
    
    
    	printf("Enter Munber :");
    	scanf("%d",&num);
    
    	printf("Enter character : ");
    	scanf("%c",ch);
    
    	printf("The Number you entered was : %d",num);
    	printf("The Character you entered: %c",ch);
    
    getch();
    
    Output
    The Number you entered was :12
    }
    In the following code it wouldn't even wait for me to put in the character value ....just after putting the number and pressing enter it would show the result wouldn't wait at the character............and the strange thing is i don't find anything wrong with this code........

    If i input the number after the character it would work fine.......as in the following code

    Code:
    
    	printf("Enter character : ");
    	scanf("%c",ch);
    
    	printf("Enter Nunber :");
    	scanf("%d",&num);
    

    The Solution to this is to use String instead of Character....
    Code:
    
            printf("Enter Munber :");
    	scanf("%d",&num);
    
    	printf("Enter character : ");
    	scanf("%s",ch);
    Or specify the erray location while entering the character ch[0]...

    Anybody have any idea why........


    And one thing more akshayakrsh........Some.People here generally don't appriciate bumping of very old threads until you have something useful to add......which i think you had here..........the thing is it is considered that if the thread is soo old the problem might have been solved by now.......If you have to post to a very old thread to add something to it.......... the Golden rule to Bump the thread first and then post ........keeps tham wondering ...........just a word of advice i know this thread is not that old .

    And i would like to suggest you would want to remove your Complete address from the location.....because you got the whole thing there with house Number With Pin Code......this being a public site you never know we got quite a few stakers around here j/k.........or are you Expecting Fan-mails


    --Good Luck---

Posting Permissions

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