Thread: String Question

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    7

    String Question

    Hello All,

    I was wondering which function I would use to accept a line of characters from a user (including spaces) and assign it into an array.

    I tried scanf but it terminates when it reads a space. Any help would be appreciated. Thanks

    Ross

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    FAQ
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Samuel shiju's Avatar
    Join Date
    Dec 2003
    Posts
    41
    try
    Code:
    char line[80];
    scanf("%[^\n]",line);

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    try
    Code:
    char line[80];
    scanf("%[^\n]",line);
    How about not. The above is nothing but trouble. So let's stick to fgets where fgets excels and leave scanf to the careful or daring.
    My best code is written with the delete key.

  5. #5
    Registered User Seph_31's Avatar
    Join Date
    Nov 2003
    Posts
    24
    scanf() is just a primitive way to get input. If he does not want to get into a complicated fgets() the he doesnt have to. scanf() works fin for me when doing simple things.
    --Seph

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >scanf() is just a primitive way to get input.
    You're turned around, scanf is a very sophisticated function that is not well suited to interactive input.

    >If he does not want to get into a complicated fgets()
    Maybe I'm just crazy, but which of the following is easier to understand?
    Code:
    scanf ( "%[^\n]", buffer );
    Code:
    fgets ( buffer, sizeof buffer, stdin );
    Which one is easier to check for correctness?

    >works fin for me when doing simple things
    How do you define simple things? I use scanf when I'm lazy and not writing production code, but only because it makes for convenient numeric conversions and I have to be very careful to do it right. But for any real code I avoid scanf like the plague because it's just too risky and too awkward.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM