Sorting string entries alphabetically without an array

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 AlMM86  Icon User is offline

  • New D.I.C Head

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

Sorting string entries alphabetically without an array

Posted 19 February 2013 - 05:26 PM

/*

Devon Naccarato, Alex Mieczkowski, 02/11/13
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int main()
{	
	// Recursion
	char			again;
	
	// Main Program
	do
	{
		int			id=0,		// Number of students
					i=1,		// Line number
					counter=0;	// Loop counter
		string		students,	// Name of students
					temp,		// for comparison
					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: ";
			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");
		
			// Ask for student names
		for (counter=0; counter < id; counter++)
		{
			cout << "Please enter the students name: " << counter + 1 << "." ;
			getline(cin, students, '\n');
			outFile << students << endl;
			students = temp;
			
			if (students > temp)
			{
				temp = students;
				students = first;
				first = temp;
			}
			else if (students < temp)
			{
				students = last;
			}
			else 
				students = temp;

				
			
		}
		

		// 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.\n\n";

	system("pause");
		return 0;
}//


This post has been edited by Skydiver: 19 February 2013 - 05:54 PM
Reason for edit:: Put code in code tags. Learn to do this yourself in the future.


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting string entries alphabetically without an array

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Sorting string entries alphabetically without an array

Posted 19 February 2013 - 05:47 PM

Welcome to D.I.C., but if you're going to post code ALWAYS highlight it, and click on the [code] icon in the editor.

Otherwise it won't look like code. :(
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3540
  • View blog
  • Posts: 10,958
  • Joined: 05-May 12

Re: Sorting string entries alphabetically without an array

Posted 19 February 2013 - 05:59 PM

So what problem are you encountering? What error are you getting?
Was This Post Helpful? 0
  • +
  • -

#4 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 19 February 2013 - 06:21 PM

Thanks for the advice about posting my code this is the first time I have ever posted anything. I cannot get the string value to run through the for loop comparison i am trying to find a way to compare each value as it is input into the program and determine the string first in alphabetical order and which name would be last in order. It reads into the file and stores fine and even reprints to the screen fine but will not sort . . .
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3540
  • View blog
  • Posts: 10,958
  • Joined: 05-May 12

Re: Sorting string entries alphabetically without an array

Posted 19 February 2013 - 07:15 PM

Since the only restriction was that you aren't allowed to use array, the use a list<string> or vector<string>. Get all the input and put it into the container, and then call std::sort(). Alternately you can use a set<string> since it sorts the items that get put into it as you go along.
Was This Post Helpful? 0
  • +
  • -

#6 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 19 February 2013 - 07:27 PM

Will that change the list order as it is put in the .txt file? I will give the list<sort> a try.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3540
  • View blog
  • Posts: 10,958
  • Joined: 05-May 12

Re: Sorting string entries alphabetically without an array

Posted 19 February 2013 - 07:32 PM

No it won't while you are putting input the text file. The idea is to get all your input and put into the list<string>, vector<string>, or set<string>. If you aren't using the set, then call sort(). And once you've sorted the container, iterate over the contents and write it out to the text file.
Was This Post Helpful? 0
  • +
  • -

#8 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 20 February 2013 - 01:17 AM

I have tried a different approach and have my sort doing something but it is not sorting anything into last place name and also sorting wrong strings into first place also. It only seems to be printing out or evaluation the last name entered for students
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>


using namespace std;

int main()
{	
	// Recursion
	char			again;
	
	// Main Program
	do
	{
		int			id=0,		// Number of students
					i=1,		// Line number
					counter=0;	// Loop counter
		string		students,	// Name of students
					temp,		// for comparison
					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: ";
			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");
		
			// Ask for student names
		for (counter=0; counter < id; counter++)
		{
			cout << "Please enter the students name: " << counter + 1 << "." ;
			getline(cin, students, '\n');
			outFile << students << endl;
					
			if (counter > 0)
			{
				if (students > temp)
				
					first.swap(students);
				
				else if (students < temp)
				
					last.swap(students);
				
				else 
					students = temp;
			}
				
			
		}
		cout << "First: " << first << endl << "Last: " << 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.\n\n";

	system("pause");
		return 0;
}

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6053
  • View blog
  • Posts: 23,488
  • Joined: 23-August 08

Re: Sorting string entries alphabetically without an array

Posted 20 February 2013 - 04:31 AM

fflush(stdin);


No, no, NO! Do NOT do this. Firstly, this results in undefined behavior. Read this. Second, you're mixing C++ and C I/O, which is really not a good idea. Instead, #include <limits> and add cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') after your clear() calls. This reads all data left in the input buffer, up to and including the newline, and discards it.

There is only ever one students. You need a container of some sort (at least in any sort of sane scenario), but you seem to be limited by your assignment, to which we have no access.
Was This Post Helpful? 0
  • +
  • -

#10 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 20 February 2013 - 11:19 AM

I will definitely change my flush statements. What kind of containter would I need to compare the first value of students with the next. I cannot use an Array and have a very limited knowledge of vectors. Is there no way to copy the value of students into a temp to compare to the following entries in the loop?
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6053
  • View blog
  • Posts: 23,488
  • Joined: 23-August 08

Re: Sorting string entries alphabetically without an array

Posted 20 February 2013 - 12:28 PM

It's very difficult to help you in this without intimate knowledge of what you've learned and what knowledge this assignment is supposed to be covering given its artificial constraints of "no arrays". Assignments are usually given as a part of what you've been learning in class...and we are not in your class.

I mean, you could use conceivably use a std::map with the string as the key, but I'm guessing that's not what your teacher is shooting for here. Maybe you should address this with him or her.
Was This Post Helpful? 1
  • +
  • -

#12 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 20 February 2013 - 12:56 PM

In class we have only been discussing control statements and loops and such. The program is supposed to prompt the teacher to enter the total number of students in a class, and then loops to read in that many full names. Then record the names to a .txt file. Once all the names have been read in,the program reports which student should be at the front of the line, and which one should be at the end of the line, in alphabetical order of their names. But he said no arrays which would make this easy. I have got my program to start sorting sort of right but still gives me the wrong output with strings of varying size and value.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>


using namespace std;

int main()
{	
	// Recursion
	char			again;
	
	// Main Program
	do
	{
		int			id=0,		// Number of students
					i=1,		// Line number
					counter=0;	// Loop counter
		string		students,	// Name of students
					temp,		// for comparison
					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: ";
			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");
		
			// Ask for student names
		for (counter=0; counter < id; counter++)
		{
			cout << "Please enter the students name: " << counter + 1 << "." ;
			getline(cin, students, '\n');
			outFile << students << endl;
				
			
			if (counter >= 0)
			{
				
				if (students < temp)
				{
					first.swap(students);
					cout << "in the if temp loop\n";
				}
				else if (students > temp)
				{
					last.swap(students);
					cout << "in the else if loop\n";
				}
				else
					cout << "else\n";
					temp.swap(students);
				 
			}
				
			
		}
		cout << "First: " << first << endl << "Last: " << 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.\n\n";

	system("pause");
		return 0;
}

Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3540
  • View blog
  • Posts: 10,958
  • Joined: 05-May 12

Re: Sorting string entries alphabetically without an array

Posted 20 February 2013 - 01:09 PM

Having those details would have been useful. The only thing that is required is far the person at the head and tail of the line to be correctly placed. There is no requirement that the others after the first and before the last need to be sorted correctly. With those constraints, then yes, it is possible to do the assignment without using an array or any other container.
Was This Post Helpful? 0
  • +
  • -

#14 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 20 February 2013 - 01:18 PM

Have you ran the program? I can get it to sort but cant get the order right is there a better way to compare string characters or do i need another way to consider the values of the strings?
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is online

  • member icon


Reputation: 4007
  • View blog
  • Posts: 12,361
  • Joined: 25-December 09

Re: Sorting string entries alphabetically without an array

Posted 20 February 2013 - 01:27 PM

You don't need to sort, you need to compare names to each other. Basically you will have a string that holds the first person in line and one to hold the last person in line. The first person will be alphabetically less than everyone else while the last person will be the higher than everybody else, no sorting required, no array required. Just read the first name assign that name to both the first and last variables then compare the rest of the file to that name, swapping the values as necessary.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2