Thread: CD Collector of sorts...

  1. #1
    Registered User
    Join Date
    Feb 2018

    Question CD Collector of sorts...

    I have an assignment for class and it is supposed to use structures and arrays. I have mine set up so it asks if I want to start, then proceeds to ask for a title, then artist, then genre.

    A few problems I've come across is that my program sees a space as an answer to each print statement? So it seems to work normally if I enter 1 word at a time but if a title is, lets say 3 words, each word will correspond to the title, artist, and genre in that order. (I hope I'm explaining this right)

    Another problem is that my program doesn't even loop. This problem baffles me because I ask for another "answer" but it completely ignores the question and skips through everything. Any advice is greatly appreciated.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Dec 2017
    The "%s" format of scanf only reads up to the next whitespace character (after skipping initial whitespace). So if you entered " hello world ", it would just read "hello" and leave the stream pointer pointing to the first space after "hello" (that's where the next input function will start reading).

    To read an entire line you need to read up to the newline character which can be done with the "%[^\n]" format. To skip initial whitespace (which is a good idea), put a space before it " %[^\n]".

    The reason that the program isn't looping is that the newline is left in the stream as the next character to read after the last previous scanf, and since you are reading a single character without skipping whitespace, "answer" gets set to the newline character, and since that is not 'Y', the loop stops.

    Just put a space before the format, like " %c", to skip the newline.

    Also note that you set your string sizes to 30 and 20, but tell scanf_s that they are all 50. You should put the actual size there (possibly using sizeof).
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  3. #3
    Registered User
    Join Date
    Feb 2018
    Oh, I never knew that command existed or that scanf works in that way. Thanks for the advice. Helped a ton not only with the assignment but understanding what certain things do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-18-2011, 09:54 AM
  2. Garbage Collector
    By anirban in forum C++ Programming
    Replies: 4
    Last Post: 04-20-2010, 09:33 PM
  3. C++ Garbage Collector
    By audinue in forum C++ Programming
    Replies: 27
    Last Post: 08-16-2009, 07:20 AM
  4. Garbage Collector, Managed Heap etc.
    By mynickmynick in forum C Programming
    Replies: 2
    Last Post: 08-07-2008, 12:42 PM

Tags for this Thread