Thread: How would I have a user input multi-character strings?

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    4

    How would I have a user input multi-character strings?

    I recently wrote a program something along the lines of:
    Code:
    #include <stdio.h>
    char Choice;
    int main()
    {
    printf("Enter (y/n)\n");
    scanf("%c", &Choice);
    if (Choice=='y')
    {
    printf("Output for y\n");
    }
    else if (Choice=='n')
    {
    printf("Output for n\n");
    }
    return(0);
    }
    I was thinking of doing a program slightly similar to this. When I input a word, it outputs the definition. This is just for the ten page glossary at the back of my Biology textbook. Is there some change I need to make to I/O multi-character strings? Also, how would I have it return to the beginning and prompt for another word once it outputs the definition?
    Last edited by Milano; 07-07-2006 at 12:00 AM. Reason: Bad wording :P

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    What you will need to do:

    1. Write data into a text file (you could hard code it into the
    program but that's not the best way to do it IMO).

    2. Read in from the file line by line using fgets as mentioned by
    Salem.

    3. Compare the word entered (also with fgets) with data from
    the file.

    4. If match, print out line, otherwise read another line from file.
    If there are no more lines in the file, print error message.

    5. Return to point where you enter word again:

    What you will need to know well to do it:
    1. Format the file as follows (or some other appropriate format):
    [TERM] [SPACE] [DATA] [NEWLINE] - eg:

    mitosis the process of cell division

    or this would work the same:

    mitosis - the process of cell division

    2. File I/O - Strings - Loops

    3. sscanf to separate the first word of the line from the rest of
    it and strcmp to check if it matches what was entered.

    NOTE - the use of gets in the strcmp link is very bad, but the code
    is ok besides - always use fgets instead of gets - see my sig for
    the reasons.

    4. If statements

    5. See Loops above in 2.

    As you can see, there's a lot you need to know, I'd suggest
    reading all the tutorials - not just these - before trying this - you
    need a good handle on these at least to be able to do this.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Why use a second file? Input comes from stdin, which is a file. Just use fgets(), and pass stdin as the "file":
    Code:
    fgets(buffer, buffer_length, stdin);
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I input an unbuffered character via the keyboard???
    By Michael_in_Ohio in forum C Programming
    Replies: 1
    Last Post: 03-23-2008, 12:00 PM
  2. User determined input question
    By lyoncourt in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 06:10 PM
  3. Very odd segmentation fault/core dump.
    By ChristianTool in forum C Programming
    Replies: 19
    Last Post: 04-26-2004, 06:38 AM
  4. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 01:08 AM