Thread: Reading in a text file

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    1

    Reading in a text file

    I've been trying to figure out how to write this program. Programing is a whole new language to me so it's been frustrating trying to wrap my head around how to do so. Hopefully someone here can help point me in the right direction

    Problem:
    Reading in a text file and sorting the list of animals and their babies (use a selection sort algorithm to do this), printing the list to the console and outputing the new sorted list to an output file sorted with a header. You will create functions to print the array, read the data from the file and output the data to a file. Use a string array to store the string parent/animal baby name combinations (sort by the parent name).

    animals.txt
    Turtle Hatchling
    Oyster Spat
    Jellyfish Ephyna
    Sand-Dollar Pluteus
    Porpoise Calf
    Trout Fry
    Manatee Calf
    Shark Pup
    Fish Fry


    Constraints:

    • NO GLOBAL VARIABLES ALLOWED. All variables must be declared in main and then passed to the functions properly. Declare a 1D array to store the animal names in main.
    • Create a function (this function will be passed the animal names array from main) to read Names from an input file called animals.txt (you MUST use the getline() function. If you use the >> operator to read in the data from the file it will NOT work)
    • Create a function (this function will be passed the animal names array from main) to sort the array using a selection sort algorithm.
    • Create a function (this function will be passed the animal names array from main) to print the array the console.
    • Create a function (this function will be passed the animal names array from main) to write the results of your selection sort to a text file call results.txt. In the output file put the header "Sea Animals and Their Baby's Names".
    • Output file - results.txt written properly
    • Uses array in main and is passed properly


    WRITE THE PROGRAM:
    Write the program for the Problem above.

    Pseudo Code for main function:

    1. Declare and Initialize Variables.
    2. Call the function to Read in the names from the input file animals.txt
    3. cout << "Sea Animals and their Baby's Names Unsorted:\n";
    4. cout << "-------------------------------------------------------------\n";
    5. Call the function to print the array.
    6. Call the function to sort the array.
    7. cout<<"Here are Sea Animals and their Baby's Names Sorted:\n";
    8. cout << "--------------------------------------------------------------------\n";
    9. Call the function to print the array.
    10. Call the function to write the output file results.txt

    Last edited by slightlytoast; 04-17-2016 at 11:36 AM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Some reading, to help you implement file I/O in your project.

    This: C++ File I/O Tutorial - Cprogramming.com
    and perhaps this also: A Gentle Introduction to IO Streams in C++ - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  2. Reading text file
    By cmanrules in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2009, 12:11 AM
  3. reading text from a file
    By ktran03 in forum C Programming
    Replies: 25
    Last Post: 02-02-2009, 05:58 PM
  4. Reading from text file
    By wiz23 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2005, 04:30 AM
  5. Reading in a text file
    By speve in forum C Programming
    Replies: 1
    Last Post: 01-01-2002, 08:00 AM