Thread: Text file sorting help

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    Text file sorting help

    Hi guys, where to start!?

    The problem:

    1 input text file containing a name (firstname surname), followed by 12 integer numbers.

    Sort the first 6 numbers, pick the highest 4 out of them, find the average.

    Take the last 6 numbers, find the average of these, then add the 2 averages together.

    Unknown number of names each with 12 integer values

    example:

    Bill Bailey
    50 32 45 97 69 49 58 49 67 54 83 94


    Now i am not expecting someone to spend hours working this out, i'm looking for clues as how to do it!

    I managed to sort out code for the input and output files, just not the main part :P

    Code:
    #include <iostream.h>
    #include <fstream>
    using namespace std;
    
    bool getInputFilename(char fname[])
    {
    	ifstream fin;
    
    	cout << "Please enter the filename for input : ";
    	cin >> fname;
    	
    	fin.open(fname, ios::nocreate);
    	
    	if (!fin.is_open())
    		return false;
    	
    	fin.close();
    	return true;
    }
    
    
    bool getOutputFilename(char fname[])
    {
    	ofstream fout;
    
    	cout << "Please enter the filename for output : ";
    	cin >> fname;
    
    	fout.open(fname);
    
    	if (fout.fail())
    		return false;
    
    	fout.close();
    	return true;
    
    }
    
    
    
    
    void main()
    {
    	char IFname[20], OFname[20];
    
    
    	while (!getInputFilename(IFname))
    	{
    		cout << "Invalid filename try again!\n\n";
    	}
    
    	while (!getOutputFilename(OFname))
    	{
    		cout << "Invalid filename try again!\n\n";
    	}
    
    }
    I need to start somewhere any help much appreciated, thank you

    Kel

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, you may want to start with actually opening and closing the files... for example, your code just asks the user for the filename, and if it gets a valid one, does nothing with it. The only time it does anything useful is when they give you a bad filename.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    std::string checkname(std::string filetype);
    
    int main()
    {
    	std::string filename(checkname("input"));
    
    	//now you actually have a valid filename
    	std::fstream file(filename.c_str(),std::ios::in);
    	/*
    	 * now you read in from the file and put the main part of your program
    	 * right here.
    	 */
    	
    	file.close();
    	return 0;
    }/*
     * this function asks the user for input using the standard console input, and 
     * it keeps going through an internal loop until the user gives up a name of a 
     * valid (existing) file.  Note that this function checks only for files that 
     * exist and can be read from.  It does not check to see if a file can be
     * created with a certain filename.
     */
    std::string checkname(std::string filetype)
    {
    	std::string filename;
    	std::fstream file;
    	
    	do
    	{
    		/*
    		 * clear the file flags, in case they entered a wrong input
    		 * on the last iteration of the loop.  If this is the first
    		 * run of this loop, this line is just a waste of code.
    		 */
    		file.clear();
    		/*
    		 * ask for and recieve a filename.  Wether it's a valid file is
    		 * open to scrutiny later on
    		 */
    		std::cout<<"Enter the "<<filetype<<" file name: ";
    		getline(std::cin,filename,'\n');
    		/*
    		 * now we're testing to see wether or not it's a valid file.
    		 * If the file doesn't exist, it's going to run the loop again.
    		 * if the file does exist, it's going to exit the loop.
    		 */
    		file.open(filename.c_str(),std::ios::in);
    		if(!file)
    		{
    			std::cout<<"File doesn't exist - try again\n";
    		}
    	}
    	while(!file);
    
    	/*
    	 * here we're closing the file that we just opened and we're returning 
    	 * the name of that file.  
    	 */
    	file.close();
    	return filename;
    }
    as for the rest, I'd create two arrays - one for the first six numbers, and another for the second six. for the first six, it really doesn't matter what kind of sorting algorithm you use because there's only six of them, but for help with searching algorithms, a simple search will turn up a great wikipedia page: http://en.wikipedia.org/wiki/Sort_algorithm
    Last edited by major_small; 01-05-2006 at 04:59 PM. Reason: more colors (comments)
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Help sorting a text file
    By cuddlez.ini in forum C++ Programming
    Replies: 4
    Last Post: 02-25-2005, 09:11 AM
  5. Sorting text file problem...
    By John-m in forum C Programming
    Replies: 3
    Last Post: 10-01-2002, 04:51 PM