Thread: sorting output on student info program

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    sorting output on student info program

    Here is my code and basically what I want to do is output it so that the output file is sorted by the reference number. The first case in this program takes the user info and outputs to a database file. The second case takes that database and outputs it to a report file. Now what I want to do is output the students by reference number. Lets say the user inputs one user's info with the reference number 111111, then 3 with 222222, then two more with 111111, then 4 more with 333333. How would I output it so that the first section of the page outputs the users info with 111111, then further down 222222, then further down 333333?
    Code:
        ////////////////////
       //Daniel Pritchett//
      //   COP 1220     //
     //   11-2-02      //
    ////////////////////
    /*
    This program will first ask the user for the term, then it will present 3 choices
    to pick from.  Either imput the information to a file, output the information
    from the first file to a report, or quit.  This all is looped so that when the user is
    finished with one choice, they will be able to re-chose again.
    */
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    int main ()
    {
    
    	string SSN, LastName, FirstName, semester ;
    	int choice, students, ReferenceNum;
    	int RefNum[3];
    	
    	RefNum[0] = 111111;
    	RefNum[1] = 222222;
    	RefNum[2] = 333333;
    
    	ofstream outFile1;
    	ofstream outFile2;
    	ifstream inFile;
    	if (!outFile2){
    		outFile1.open("a:sdntnfo.dat", ios::out);}
    	inFile.open("a:sdntnfo.dat", ios::in);
    	outFile2.open("a:Record.txt", ios::out);
    
    	cout << "Please enter the Class term";
    	cin >> semester;
    	//heading
    	outFile2 << setw(6) << "Introduction to C"
    			<< setw(5) <<"\nTerm:" << semester
    			<< "\n\n\nSSN" << right << setw(20) << right << setw(20) << "Last"
    			<< right << setw(20) << "First  " << right << setw(20) << "Reference Number\n";
    
    	//loops so that the user can re-chose what operation they would like to do.
    	do
    	{
    		cout << "\nWould you like to\n1: Enter Student information\n"
    		<<"2: Read from file and create report"
    		<< "\n3: Quit\n";
    	cin >> choice;
    	switch(choice)
    	{
    		//outputs the first file, student database
    	case (1):
    		cout << "\nHow many students are you entering?";
    		cin >> students;
    		while(students != 0)
    		{
    
    		cout << "\nEnter the student's nine digit ID number: ";
    		cin >> SSN;
    		outFile1 << SSN;
    		cout << "\nEnter the student's Last then First name: ";
    		cin >> LastName >> FirstName;
    		outFile1 << " " << LastName << " " << FirstName;
    		cout << "\nEnter the student's Reference number: "
    			 <<"(note, only 111111, 222222, and 333333 are valid.\n)";
    		cin >> ReferenceNum;
    
    		//tests to see if number is correct
    		while((ReferenceNum!=RefNum[0]) && (ReferenceNum!=RefNum[1]) && (ReferenceNum!=RefNum[2]))
    		{
    			cout << "\nYou have entered an invalid number, please enter a correct one: ";
    			cin >> ReferenceNum;
    		} 
    		outFile1 << " " << ReferenceNum << endl;
    		
    		students--;
    		}
    		break;
    
    		//generates reference number organized file
    	case (2):
    		
    		while (!inFile.eof())
    	{
    	inFile >> SSN >> LastName >> FirstName >> ReferenceNum;
    	outFile2 << right << SSN << right << setw(17) << LastName << right 
    			<< setw(16) << FirstName << right << setw(20) << ReferenceNum;
    
    	}
    	outFile2.close();
        break;
    
    	//quits
    	case(3):
    		exit(0);
    
    	//default, exits switch and displays switch options 
    	default:
    		cout << "\nYou did not enter a valid choise, please enter correct choice!";
    		break;
    	}
    	}
    	while(choice != 3);
    
    	outFile1.close();
    	outFile2.close();
    	inFile.close();
    
    }
    // in.seekg(0, ios::beg) OR  SeekToBegin() <----use to rewind to the beginning of the file.

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I don't know anything about structures yet. And my teacher won't allow them.

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    yeah, I leared a bit about basic one-dymensional arrays. Is there no way to use a loop or something. I was thinking of reading the file several times and ouptutting only those lines that have RefNum[0] first. then re-read the file from the top again to get RefNum[1] and then again for RefNum[3] but I tried it and it didn't come out correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling external program and reading its output.
    By Dragoon_42 in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2007, 05:34 AM
  2. Info about program execution
    By satya_nemana in forum C Programming
    Replies: 10
    Last Post: 06-18-2007, 06:52 AM
  3. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  4. Redirecting program output straight to an edit control
    By bennyandthejets in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2004, 08:25 AM
  5. program output is empty!!!
    By ayesha in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2001, 10:42 PM