Sorting string entries alphabetically without an array

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1549 Views - Last Post: 21 February 2013 - 07:12 AM Rate Topic: -----

#16 AlMM86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-February 13

Re: Sorting string entries alphabetically without an array

Posted 21 February 2013 - 12:30 AM

Got it running smooth!! Take a look . . . I appreciate all the input.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>


using namespace std;

int main()
{	
	// Recursion
	char			again;
	
	// Main Program
	do
	{
		int			id=1,		// Number of students
					i=1,		// Line number
					counter=0;	// Loop counter
		string		students,	// Name of students
					first,		// Student that goes first
					last;		// Student that goes last
		bool		cat=true;	// Recursion boolean
		ifstream	inFile;		// Read from file
		ofstream	outFile;	// Write to file

		// Ask for number of students
		do
		{
			cat = true;
			cout << "Enter number of students: "; // Data entry qualification for number of students in class
			if (!(cin >> id) ||id < 5 || id > 30)
			{
				cout << "\nERROR - Invlid entry\n";
				cin.clear();
				fflush(stdin);
			}
			else
			{
				cat = false;
				cin.clear();
				fflush(stdin);
			}
		}while (cat == true);

		// Start writing to file
		outFile.open("students.txt");
		
		// For loop to retrieve names and compare
		for (counter=1; counter <= id; counter++)
		{
			cout << "Please enter the students NAME: " << counter << "." ; // recieve input for names
			getline(cin, students, '\n');
			outFile << students << endl;
			
			//Assign first and last to student to compare
			if ( counter == 1)
			{
				first = last = students;
			}
			else // compare values after first iteration
			{
				if (first > students)
				{
					first = students; // reassign first
					
				}
				if (last < students)
				{
					
					last = students; // reassign last
					
				}	
				
			}
		}
			// Display first and last students	
			cout << "First student in line : " << first << endl << "Last student in line : " << last << endl;
		

		

		// Close file
		outFile.close();

		// Open student file
		inFile.open("students.txt");

		// Checks to see if file exists
		if (!inFile)
		{
			cout << "File does not exist!";
		}

				
		
		// Print student names
		cout << "\nYou entered: \n";
		while (getline(inFile, students, '\n'))
		{
			cout << " " << i++ << ". "<< students << endl;
		}

		// Close file
		inFile.close();

		// Recursion
		cout << "\n\nWould you like to run this program again?: [Y/N] ";
		cin >> again;
	}while (again == 'Y' || again =='y');

	// Programmers
	cout << "\n\nThis program was written by: Devon Naccarato, and Alex Mieczkowski. for CISC 192\n\n";

	system("pause");
		return 0;
}


I got the program running great just had to adjust some things in the for loop and change a couple logic statements. You were right easily done without any kind of sort just a couple well placed if statements!
Was This Post Helpful? 0
  • +
  • -

#17 jimblumberg  Icon User is online

  • member icon


Reputation: 4286
  • View blog
  • Posts: 13,453
  • Joined: 25-December 09

Re: Sorting string entries alphabetically without an array

Posted 21 February 2013 - 07:12 AM

There is one problem with your code when I run it. It skips the entry of the first name. This is being caused by your use of fflush(cin). Using this function on an input stream invokes undefined behavior, your compiler "may" produce the desired results but most others won't. You should use something like cin.ignore(), including the optional arguments for the number of characters to skip and the delimiting character.

Other than that good work.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2