Thread: Display selected words from text file

  1. #1
    Registered User
    Join Date
    Oct 2007

    Display selected words from text file

    i have a text file with 20 words. And when user enter any number between 1- 20 it will display out the selected word.

    eg. in text file i have;


    when user enter "1" when prompt, apple will display out on screen.

    Can someone give me an ideal of how can i display out the selected words when user entered any number between 1-20?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    You may want to read the homework policy:

    Show us that you are trying, then ask specific questions.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Open the file. Read the words line by line (perhaps using getline) into a string container and push that onto a vector<string> container which will store all your words from the file. You can then prompt the user for the number and use the value as an index into the vector from which to obtain the value you wish to display.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jan 2005
    If you don't know vectors, you can use a regular array since you know the file will have exactly 20 lines. The user's choice of 1-20 can be converted to an array index.

    Are you studying arrays right now?

    If you haven't learned arrays, and only loops or something, you could open the file and count how many words you've read in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. random selection of words from a text file
    By archie in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 12:59 AM