Sorting Data From Text File

I need help on how to write a code that reads and sorts the data from a text file in ascending order. If anyone can provide an example, I would appreciate it!

The example text file contains the students' ID, Name, and GPA.

Before Sorting:
1
2
3
4
5
6
7
8
ID:          Name:          GPA:
____________________________________
123456789    John   Johnson     3.5
512434990    Mary   Jackson     3.9
342432444   Peter     Young     2.3
470068625     Jim       Lee     2.9
234324324   Tammy    Gaddis     3.1
121219000   Ester    Schwab     2.7



After Sorting:
1
2
3
4
5
6
7
8
ID:          Name:          GPA:
____________________________________
512434990    Mary   Jackson     3.9
123456789    John   Johnson     3.5
234324324   Tammy    Gaddis     3.1
470068625     Jim       Lee     2.9
121219000   Ester    Schwab     2.7
342432444   Peter     Young     2.3
Hello Orion98,

Do you mean to read the input file and sort as you read, most difficult, or read the file into something like a class or a struct and store that in a vector or array? It is not clear what you want to do.

In the block of code, (Before sorting), also output tags would be better (the button with the two columns), do I take this to be the input file with headings? Or is it what the output should look like. Best to include the actual input file, so everyone is using the same information.

It is also best to post any code that you have, so that questions and answers can fit the problems. Or so problems can be pointed out if you are not sure of what you did.

To the best of my knowledge no one here can read minds. At this point no one knows what you know, i.e., vectors, arrays, structs, classes along with what you are think about for dealing with the data in the input file.

It is also helpful to mention what IDE you are using along with the compiler used with the IDE or what you write your code in and how you compile it.

Here is an idea to help you start if needed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <vector>
#include <chrono>
#include <thread>

#include <fstream>

struct StudentData
{
	StudentData(std::string ID, std::string fName, std::string lName, double gpa) // <--- Overloaded ctor.
	{
		s_id = ID;
		s_fName = fName;
		s_lName = lName;
		s_gpa = gpa;
	}

	std::string s_id;
	std::string s_fName;
	std::string s_lName;
	double s_gpa{};
	// or
	//std::string s_gpa;
};

int main()
{
	std::vector<StudentData> students;
	std::string ID, fName, lName;
	double gpa{};  // <--- This could be a std::string. Depends on what you need it for and how you will use it.

	std::string inFileName{ "Input.txt" }; // <--- Put file name here.

	std::ifstream inFile(inFileName);

	if (!inFile)
	{
		std::cout << "\n File " << std::quoted(inFileName) << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread". Optional as is the header files.
		return 1;  //exit(1);  // If not in "main".
	}

	// 1. Read file
	// 123456789    John   Johnson     3.5

	std::cout << "\n    ID" << std::string(14, ' ') << "Name" << std::string(15, ' ') << "GPA\n";
	std::cout << ' ' << std::string(41, '_') << '\n';

	while (inFile >> ID >> fName >> lName >> std::ws >> gpa)
	{
		// 1a. Store data
		students.emplace_back(ID, fName, lName, gpa);

		std::cout << ' ' << std::left
			<< std::setw(10) << ID
			<< "  "
			<< std::setw(10) << fName
			<< std::setw(10) << lName
			<< std::right << std::string(5, ' ')
			<< std::setw(4) << gpa
			<< '\n';
	}

	// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue :";
	std::cin.get();

	return 0;
}

This is an idea based on what I am thinking about based on my interpretation of your instructions.

When writing a program it is best to work in small steps. Write some code and test it, (compile and run) until it is working the way you want. Until you can read the file an do something with the information, i.e., store it in something for later use, there is not much point discussing how to sort it until you have something to sort.

Hope that helps,

Andy
Topic archived. No new replies allowed.