C programming scanf function help... urgent

This is a discussion on C programming scanf function help... urgent within the C Programming forums, part of the General Programming Boards category; I tried to write multiple scanf statements but the program generate some garbage value. Here is the program Code: #include<stdio.h> ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    2

    C programming scanf function help... urgent

    I tried to write multiple scanf statements but the program generate some garbage value. Here is the program

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    char a,b,c;
    printf("Enter three character\n");
    scanf("%c",a);
    scanf("%c",b);
    scanf("%c",c);
    printf("The characters entered are %c %c %c", a,b,c);
    getch();
    }
    This is the answer generated.
    Enter three character
    s
    d
    The characters entered are Å ╖ 2

    I am using window 7 x86 version.
    Please help with the solution ASAP.

    Thank you.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Well use fgetc to read in a char. Scanf function leave the return key behind in the inoput buffer causing the second scanf function be skipped. Or an alternative solution would be clear the input buffer afer very scanf call. Insert this statement just after every scanf call.

    Code:
    scanf("%*[^\n]%*c");
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    It might be windows 7, but it looks for all the world like you're using 20+ year old TurboC.
    void main and conio.h are a dead give away.

    Or you're reading the wrong books.

    No wait, you're from India right - the land that time forgot (at least when it comes to teaching relevant modern technologies).

    Upgrade to one of these.
    Free Developer Tools - Visual Studio 2010 Express | Microsoft Visual Studio
    smorgasbordet - Pelles C
    Code::Blocks

    Oh, and it's
    scanf("%c", &variable);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    And it's int main(void), not void main()!

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    This should work....

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    char a,b,c;
    printf("Enter three character\n");
    scanf("%c",&a);
    scanf("%c",&b);
    scanf("%c",&c);
    printf("The characters entered are %c %c %c", a,b,c);
    getch();
    }
    No need to upgrade to any of the development tools mentioned by salem! I guess u r just starting up with C....its fine to use any compiler you wish.....it will satisfy ur purpose, more than enough. Though latest compilers are recomended.
    You can use Borland or Dev C++ compilers.
    There is no mistake in "void main()" but its generally found that programers prefer "int main()" and manually return a value from main() as ur program ends. Zero is the normal return value if there were no errors or ur program terminates normally; otherwise some other integers are returned to signify the error.

    Just go ahead, dont b afraid and happy programming!!!


    @Salem
    From which country r u from???
    And for what the hell shall he install all those heavy weight development tools when he is just a starter?????
    Win7 does not create problems in such simple things.....!!!!

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,457
    That code will not work on any modern computer with a modern compiler. Sure, it may seem to work, but deep down, it doesn't. There is a mistake with "void main", it's not just a matter of preference. The C99 standard, which is now 12 years old, states that main must have an explicit return type of int and that it must actually return an int. Borland accepts it for two reasons. First, they haven't bothered to update their compiler to work with the new version of the language, and second, because they like to make up their own rules. Read these two articles to learn why it actually matters:
    Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])
    void main(void) - the Wrong Thing

    You really should upgrade. If you're going to learn C, learn it using current tools. If you wanted to become a construction worker, you wouldn't go out and find a rock to practice nailing boards together, nobody would hire you if you showed up to work with a rock for a hammer. You would go out and get a proper hammer or even a nail gun. Both Borland and Dev C++ are old and outdated, and neither is well maintained. Borland in particular allows a lot of stuff that no modern compiler accepts, limits you to a tiny memory footprint, and lets you do all sorts of horrible things to string literals and other parts of memory that would crash all over the place with a modern compiler. Not to mention 64-bit machines are not backwards compatible to the 16-bit executables that it produces. If you insist on using crappy old tools, you will "learn" to program C incorrectly, and when you hit the real world, you'll be in trouble. We're trying to get you started off on the right foot so you don't get screwed in the work place, or end up creating nasty, buggy code with massive security holes that ends up on everybody's computer.

    The "India" comment was directed to the Indian educational institutes, not you personally or the Indian people. The problem is that the Indian schools continue to teach on crappy outdated tools, when there are plenty of other, better, modern, free tools out there like Pelles or CodeBlocks/MinGW. They aren't terribly heavyweight tools and they're well-suited for beginners, especially when you put them up against big commercial tools like Visual C++.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Oh really?

    The OP's code, compiled with a compiler with more that 3 error messages.
    Code:
    $ gcc -W -Wall baz.c
    baz.c:3: warning: return type of ‘main’ is not ‘int’
    baz.c: In function ‘main’:
    baz.c:5: warning: implicit declaration of function ‘clrscr’
    baz.c:8: warning: format ‘%c’ expects type ‘char *’, but argument 2 has type ‘int’
    baz.c:9: warning: format ‘%c’ expects type ‘char *’, but argument 2 has type ‘int’
    baz.c:10: warning: format ‘%c’ expects type ‘char *’, but argument 2 has type ‘int’
    baz.c:12: warning: implicit declaration of function ‘getch’
    baz.c:8: warning: ‘a’ is used uninitialized in this function
    baz.c:9: warning: ‘b’ is used uninitialized in this function
    baz.c:10: warning: ‘c’ is used uninitialized in this function
    /tmp/cc48Ss25.o: In function `main':
    baz.c:(.text+0xb): undefined reference to `clrscr'
    baz.c:(.text+0x86): undefined reference to `getch'
    collect2: ld returned 1 exit status
    Yeah, that's right - with some decent modern tools correctly configured to give the average noob a LOT of useful advice, they would have been able to at least see that something was wrong with the scanf calls.

    Nor was what was written even strictly 'C' code to begin with, it was very poor C++ code. Did you spot that?
    I guess not, since you repeated the mistake.

    But no, you're happy for them to carry on using stone tools.

    I guess since both of you are from India, then this is worthwhile reading for the both of you.
    India Graduates Millions, but Too Few Are Fit to Hire - WSJ.com

    Anybody with any smarts at all would realise they're being sold lemons by the establishment, and take it upon themselves to learn something relevant for the real world. It's your choice, but I would suggest you both start to smell the coffee.

    > And for what the hell shall he install all those heavy weight development tools when he is just a starter?
    You mean you want to drag him down to your level, rather than let him race ahead and leave you in the dust?
    code::blocks is a light-weight tool, and probably no more complicated to set up than TurboCrap. Plus you get a damn fine compiler along with it.

    C is hard enough to learn it once. Having to unlearn a bunch of crap so you can learn it properly is much much harder.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    I agree with you about the state of Borland products, obsolete should not be used for new development. However the following is misleading.
    Quote Originally Posted by anduril462 View Post
    Borland accepts it for two reasons. First, they haven't bothered to update their compiler to work with the new version of the language, and second, because they like to make up their own rules.
    First Borland has not been in the compiler business for at least 20 years, hard to upgrade your compilers when you no longer own the product. Second these compilers were pre-any-standard, so the compiler manufactures all made up their own rules, this was the same for the Microsoft compilers of that era.

    The major problem with these compilers for C-programming is that they will not produce a 32 bit executable and use non-standard headers, which users tend to use, thereby teaching bad habits and producing programs that will not run properly on modern operating systems. The use of these compilers for C++ is another, even worse pile of worms.

    Embarcadero, who now owns the line, has upgraded the compiler and it is C++ standard compliant, I don't know if it has a C compiler any longer though.

    I also recommend that the OP get a modern compiler. Any compiler would be better.

    Jim

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,457
    Didn't realize Borland sold it off. There were definitely still in the compiler business (or at least dev tools business) as of the early 2000s, though not working on their "Turbo" line. I had to use Borland C++ Builder at one of my jobs. They also did continue to make their C compiler post 1989 (the first standard), but it was rolled into Turbo C++. It wasn't standards compliant, at the very least since it accepted void main, which the C89 standard didn't.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Avenger625 View Post
    No need to upgrade to any of the development tools mentioned by salem! I guess u r just starting up with C....its fine to use any compiler you wish.....it will satisfy ur purpose, more than enough. Though latest compilers are recomended.
    You can use Borland or Dev C++ compilers.
    I'm sorry but I have to side with the others... this is terrible advice to give anyone.

    I use Pelles C which is an 11 megabyte download, produces both 32 and 64 bit code and is C-99 compliant. It's actually smaller than Borland C++... you'd have to be an idiot not to upgrade to A) current code generation and B) current standards.

    The 16 bit code produced by the Borland compilers will not run on 64 bit windows... yeah, it's that outdated!

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    @salem
    yes i am from INDIA; let me know from "which edge of the known universe" you come from where people are born genius like you.
    Listen you are asked for the solution and not to comment on others nationality.
    Last edited by red99; 06-19-2011 at 07:24 AM.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by red99 View Post
    yes i am from INDIA; let me know from "which edge of the known universe" you come from where people are born genius like you.
    Listen you are asked for the solution and not to comment on others nationality.
    I've never seen anyone here claim genius status (although some probably could). It's as simple as the explaination given, you don't use a rock to drive nails when you could be using a power hammer. You should always want to use the most modern software for programming and the fact that all of the software recommended is free removes every excuse you may have. Programming is the one place where there simply is not excuse for stupidity...

    As for the comments on India... You guys have a real problem with corruption and incompetence in your educational system. There are news stories world wide about things such as writing your phone number on exams so you can negotiate the price of a passing grade with your teacher. Growing up in a system like that one can understand (sort of) why you might not see the wrong in it. So no that wasn't a comment about your nationality... it was about well known problems in India's educational system.

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    There are many C++ compilers available in the market; it’s really not possible for a person to know about each of them and their C99 compliance, especially for a person who is into Java. But, I agree that i never knew that Borland has stopped its C++ compiler neither I knew about Pelles C. Pelles C is really a grreeeaaatt tool!! My 'upgradation-comment' was targeted to Visual Studio. Is it not heavy-weight?! I often find beginner’s gets confused with the overwhelming interface of VS. But i never said that one should not upgrade rather i said, "latest compilers are recommended" but perhaps not VS.
    I find many....many people still use Dev C++ and they are not only from India. My friends from UK and Australia use it, too. Moreover, DC++ is GCC based.
    exe produced by Borland C++ 5.2 ran fine on a 64-bit CPU with 64-bit OS. I saw it. Though i agree it’s out-dated and now i discourage it too. Pelles C is really good!

    About everything else, red99's program seems that he is just beginning so i did not want to discourage him. We will tell him something and his teacher will tell him something else and in the middle he will be confused. So, I thought when he will be at least confident with is programs (will be able to debug his own code), then he can "smell the coffee" and can upgrade to the standards, easily. May be u did the same if u had been programing in C since before 1999. But, i CANNOT refute, "If you're going to learn C, learn it using current tools".

    About C99 standards, hardly there are compilers that "exactly" follow it. Even GCC and Pelles C follow it "mostly" but NOT "completely". The Portland Group PGI C/C++ supports it completely. Microsoft Visual Studio 2010 does not support it at all.

    I agree there are certain holes in the Indian Education System but one can’t judge the entire system just by one incident of writing phone numbers in the answer script. I heard a 21-year-old made out with his own mom in US....does that mean every American is a mother........er??!!
    @SALEM:
    My code ran fine in Pelles C (the current standard) with 1 warning about "void main()". I just changed "clrscr()" to "_clrscr()" and "getch()" to "_getch()".
    And i know "clrscr()" or "_clrscr()" and "getch()" or "_getch()" are not C99 standards but these r common extensions that most compilers support in their own way. I could not find any connection between my code and the part of the error code highlighted, red. Seems u 'raced' ahead but could not leave me much back in the 'dust'.


    Newyz, lets shut the ........ up!!

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,658
    Quote Originally Posted by Avenger625 View Post
    @SALEM:
    My code ran fine in Pelles C (the current standard) with 1 warning about "void main()". [...] I could not find any connection between my code and the part of the error code highlighted, red. Seems u 'raced' ahead but could not leave me much back in the 'dust'.
    There never was a connection. He wasn't talking about you.
    Quote Originally Posted by Salem
    The OP's code, compiled with a compiler with more that 3 error messages.
    [sic] Unless you started the thread, you are not the OP, the Original Poster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 10-20-2009, 09:39 AM
  2. Urgent help needed in Socket Programming
    By lisa23 in forum C Programming
    Replies: 2
    Last Post: 09-15-2005, 11:18 PM
  3. Urgent Programming Problem
    By mchap1643 in forum C Programming
    Replies: 5
    Last Post: 10-16-2003, 08:54 AM
  4. advanced scanf in c programming
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-07-2001, 06:44 AM
  5. Urgent ! Need Advices On Windows Programming !
    By SonicWave in forum Windows Programming
    Replies: 6
    Last Post: 09-16-2001, 11:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21