Display selected words from text file

This is a discussion on Display selected words from text file within the C++ Programming forums, part of the General Programming Boards category; i have a text file with 20 words. And when user enter any number between 1- 20 it will display ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    1

    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;

    apple
    orange
    grape
    ....

    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
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may want to read the homework policy:
    http://cboard.cprogramming.com/annou...t.php?f=4&a=39

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

    --
    Mats
    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
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    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
    Posts
    7,319
    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, 01: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-01-2002, 11:59 PM

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