Sorting data alphabetically from a input file

Hello! I am currently writing a program that takes last name, first name , pay rate, hours worked and calculates the tax and net pay. I have figured everything out except how to sort it.

I have not been taught vectors in my class yet. I'm assuming I need to take the last name string and put it into an array and sort it, but i haven't been able to do it.
here is my input data

Marion Louise 13.00 40.00
Davidson Carl 8.75 38.00
Whittle Ed 11.50 25.50
John Doe 17.00 46.50
Prentiss Paula 15.75 50.50

Any help is appreciated

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
74
75
76
77
78
79
80
81
82
83
84
85
  /*  MY EMPLOYEE RATE PROGRAM */

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	double  overtime    = 1.50,
		   overtimehrs = 0.00,
		   payrate     = 0.00,
		   hours       = 0.00,
		   overtimepay = 0.00,
		   taxrate     = 0.15,
		   grosspay    = 0.00,
		   netpay      = 0.00,
		   taxamt      = 0.00;

	       string firstN,
           	      lastN;
		   // Equations Needed to Calculate Money Totals
		 




	
	ifstream inFile("C:\\Users\\Lance\\Desktop\\School Spring 2015\\CISP\\inputdata.txt");
	
	if (inFile.is_open())
	{
		cout << "File opened\n" << endl;
	}
	else
	{
		perror("File Failed to open");
		return 1;
	}
	while (!inFile.eof())
	{
		inFile >> lastN >> firstN >> payrate >> hours;

		
		cout << endl;

		if (hours > 40)
		{
			overtimehrs = hours - 40;
			overtimepay = overtime * overtimehrs;      //Calculates overtime pay
	
		}
		else
		{
			overtimepay = 0;
		}
		

		grosspay = (payrate*hours) + overtimepay;  //Calculates the complete total before tax 
		taxamt = grosspay * taxrate;              //Calculates tax removed from paycheck
		netpay = grosspay - taxamt;               //Calculates Amount the employee receives after taxes

		cout << "\nYour employee name is " << firstN << " " << lastN << endl;
		cout << "\nYour gross pay is: " << grosspay;
		cout << "\nYour net pay is: " << netpay;
		cout << "\nAmount taxed: " << taxamt;
		
	}
	
   inFile.close();

	
	cout << "\n\n\n\n\n\n\n";




	
	system("pause");


		return 0;
}
Which part are you struggling with - getting it into an array, or sorting it?

It is disappointing that you are required to do this and have not been taught about vectors yet - are you at least able to use std::sort?
At the moment getting it into an array. I don't know how to use std::sort either.

If i can get it into an array I may be able to go from there.

Everything I did try to put it into an array kinda just blew up the program

the only sorts I have done are bubble sorts and it has been a while. (Took about 6 months off).
Last edited on
You need to know how big to make the array before you can put things in it. That means you need to go through the file twice - once to figure out how much space you need, and again to actually fill the space once you've gotten it.
The largest last name is 8 letters.

the largest piece is 26 spaces (including pay rate spaces '.' etc)

Does it have to be an array? I am calling from a data txt and the first bit is being pulled into

lastN is there any way i can sort by that and have that rotate all the other info?
Last edited on
gogobumrush wrote:
The largest last name is 8 letters.

the largest piece is 26 spaces (including pay rate spaces '.' etc)
This information is irrelevant because std::string resizes for you.
gogobumrush wrote:
Does it have to be an array?
What will you store information in while you sort? You're not seriously considering file juggling!?
gogobumrush wrote:
I am calling from a data txt and the first bit is being pulled into

lastN is there any way i can sort by that and have that rotate all the other info?
I don't understand what you mean.
I would be willing to learn std:string. I will look up what i can on that.

No i do not want to file juggle. I am just getting back into programming after a long break so I am refreshing and trying to remember if there is another way to do it.

I am trying to sort by last name alphabetically so i was wondering if i was able to just sort by the information being held in the variable lastN. and have the rest of the variables follow along. (again just trying to refresh my possibilities)
gogobumrush wrote:
I would be willing to learn std:string. I will look up what i can on that.
What? You use it on lines 5, 22, and 23 already...
gogobumrush wrote:
No i do not want to file juggle. I am just getting back into programming after a long break so I am refreshing and trying to remember if there is another way to do it.
Aside from std::vector, not really.
gogobumrush wrote:
I am trying to sort by last name alphabetically so i was wondering if i was able to just sort by the information being held in the variable lastN. and have the rest of the variables follow along. (again just trying to refresh my possibilities)
Yes, you can do that with a custom compare function passed to the sorting algorithm you use.
Is it out of the question for you to use vectors? In my classes, if you ask, the professor usually does not mind if you use your own way of completing an assignment, especially if you are doing it using real-world solutions, IE utilizing the work of those before you and not reinventing the wheel.
Topic archived. No new replies allowed.