need string help

This is a discussion on need string help within the C Programming forums, part of the General Programming Boards category; printf ("Title >"); fflush(stdin); scanf ("%s", ctlg[x].title); printf ("Title :%s \n", ctlg[i].title); i've typed the program above to do a ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    18

    need string help

    printf ("Title >");
    fflush(stdin);
    scanf ("%s", ctlg[x].title);
    printf ("Title :%s \n", ctlg[i].title);

    i've typed the program above to do a program on library catalog. When I type "The best book", the scanf function works well but when i wanted to print it back using the printf function, the title became "The".. can anyone help?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    1) Do not flush stdin.
    2) Do not read strings with scanf.
    3) Use fgets.
    4) Your app will work.
    http://cpwiki.sf.net/Buffer_overrun
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    fflush(stdin) is undefined, read this FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    scanf stops at white space, read this FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    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
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    thanks..
    u guys helped a lot..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 03:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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