Heap Program - Needs to sort smallest to largest

I've written this program and when I compile it, it gives me an output, but the sorting is incorrect. The first "week" is suppose to be the lowest/smallest worth clients and progressively get larger then in the final week of calls is suppose to have the largest amounts listed. I'm uncertain as to whats wrong within the code itself. I will place all info below.

NameNum.in
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
Taylor marie  939-1512
james wilis thomas 261-8342
Hewy Lewy Dewy   623-9921
Stone Rock  544-2372
lea high lee 266-8324
stephens reynolds 696-9231
mack russell 123-1234
Lewis Michelle Tee 828-2148
john marshall 888-2891
Mister Rogers   924-7221
Hasey Moto     823-8000
Snow White		111-9999
Oe vey 177-1423
Twoseeor knocksee 823-8321
Legg T.        587-2839
Znot Noz Oozey  531-4353
Hofstra M.      601-3225
Malioneyh P. J. 287-4344
Morier G. E.    544-2319
Cray Z. Cyco   134-7242
Hauser Yauser    606-2940
Hack N Zakkers    231-4449
LessGo Eat       233-1984
Fanny Pac Jac    842-3242
Trigger Phanger   421-3435
Sid MacGriddy    882-2424
Currie W. D.    701-4281
Antoney Wenner  999-3255
Hoos R. Dadie   818-3821
Chipoff E Oblock 773-4152
Booger Runs      822-7724
Smelly Tau      707-7281
Tobe Ore Knott 613-2414
Bee Yond Lawz   634-2454 
Tyson Chicken  602-3152
TooB OrNot   283-5214
SixOne Other  843-2343
Big Tow  384-5624
Juan Legged    882-6246
Tau Jam       532-6871
Zeventh Heaven  834-2563
Man Oh Mann     313-7422
E Lec Shaun      709-7424
Sticky Finger McRidder  829-9853
Mighty Mouse  222-2222
G P Morier  832-4562
Six One Another  829-8221
Pha Seed Doe   243-9233
Lu C Kant   247-7345
Thor Thumb   833-6391
Big I KanC    878-0129
Knot Dun Uet   729-5223
Ah C Mowtoe    334-8294
Lase Won    928-2824


Worth.in
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
$19571000000.00
$10136000000.00
$12627000000.00
$27983000000.00
$28093000000.00
$20349000000.00
$16529000000.00
$11272000000.00
$26749000000.00
$17856000000.00
$2179000000.00
$14666000000.00
$25010000000.00
$1648000000.00
$5060000000.00
$14546000000.00
$25461000000.00
$21331000000.00
$15572000000.00
$31668000000.00
$6364000000.00
$4152000000.00
$26904000000.00
$24397000000.00
$286000000.00
$14729000000.00
$29986000000.00
$6266000000.00
$24056000000.00
$10528000000.00
$13875000000.00
$16135000000.00
$24911000000.00
$7515000000.00
$13566000000.00
$11627000000.00
$24086000000.00
$13939000000.00
$1455000000.00
$2226000000.00
$114000000.00
$24073000000.00
$24876000000.00
$1210000000.00
$20764000000.00
$19714000000.00
$25499000000.00
$14498000000.00
$2056000000.00
$9109000000.00
$5156000000.00
$6016000000.00
$29252000000.00
$18338000000.00
$6648000000.00


PROGRAM
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

ofstream out("Clients.out");// Outfile

//Prototypes
void swap(int&, int&);
void sort(double[], string[], int);
void Hswap(double[], string[], int, int);
void print(double[], string[], int, int);

//Swap function
void swap(int& i, int& j)
{
     int temp = i;
     i = j;
     j = temp;
}

//Function Sorts the Heap
void sort(double amt[], string client[], int sz)
{
     // Builds Heap 
     for (int i = sz / 2 - 1; i >= 0; i--)
          Hswap(amt, client, sz, i);

     // Extracts one element at a time
     for (int j = sz - 1; j > 0; j--)
     {
          swap(amt[0], amt[j]);
          swap(client[0], client[j]);
          Hswap(amt, client, j, 0);
     }
}

//Function Swaps Heap values
void Hswap(double amt[], string client[], int sz, int i)
{
     int small = i;//Root
     int LC = 2 * i + 1;//Left Child
     int RC = 2 * i + 2;//Right Child

                        //IF left child is smaller than root
     if (LC < sz && amt[LC] < amt[small])
          small = LC;

     //IF right child is smaller than root
     if(RC < sz && amt[RC] < amt[small])
          small = RC;

     //If small is not root
     if (small != i)
     {
          swap(amt[i], amt[small]);// Swap amount with client
          swap(client[i], client[small]);
          Hswap(amt, client, sz, small);// Recursively heap the tree
     }
}

// Function prints the heap
void print(double amt[], string client[], int start, int end)
{
     for (int i = start; i >= end; --i)
          out << client[i] << "    $" << amt[i] << endl;
}

int main()
{
     ifstream in1("NameNum.in");// Names and Numbers of infile
     if (!in1)
          out << "Error with NameNum.in check file\n";
     
     ifstream in2("Worth.in");// Dollar Amount infile
     if (!in2)
          out << "Error with Worth.in check file\n";

     const int CLIENTS = 75; // Max Array size for clients
     int CountAmount = 0, // Number of worth
          CountClient = 0;   // Number of names 
     double amt[CLIENTS], // Amount of money per client
                worth; // Amount of worth per client 
     char dolla = '$';
     string client[CLIENTS], // Names of the clients
               name, // Name of client
               input, // Data from infile
               tele; // Phone number of client

     while (in1 >> input)
     {
          if (isdigit(input[0]))
          {
               tele = input;
               client[CountClient] = name + "   #" + tele;
               name.clear();
               tele.clear();
               CountClient++;
          }
          else
          {
               if (!name.empty())
                    name += ' ';
               input[0] = toupper(input[0]);
               name += input;
          }
     }

     while (CountAmount < CountClient)
     {
          in2 >> dolla >> amt[CountAmount];
          CountAmount++;
     }

     sort(amt, client, CountAmount);
     out << fixed << setprecision(2)
             << "Week 1 Calls \n";
     print(amt, client, CountAmount - 1, CountAmount - 5);
     out << "\nWeek 2 Calls\n";
     print(amt, client, CountAmount - 6, CountAmount - 10);
     out << "\nFinal Clients to phone\n";
     print(amt, client, CountAmount - 50, CountAmount - CountAmount);

     in1.close();
     in2.close();
     out.close();
     return 0;
}


OUTPUT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Week 1 Calls 
Zeventh Heaven   #834-2563    $114000000.00
Trigger Phanger   #421-3435    $286000000.00
Sticky Finger McRidder   #829-9853    $1210000000.00
Juan Legged   #882-6246    $1455000000.00
Twoseeor Knocksee   #823-8321    $1648000000.00

Week 2 Calls
Lu C Kant   #247-7345    $2056000000.00
Hasey Moto   #823-8000    $2179000000.00
Tau Jam   #532-6871    $2226000000.00
Hack N Zakkers   #231-4449    $4152000000.00
Legg T.   #587-2839    $5060000000.00

Final Clients to phone
Stone Rock   #544-2372    $27983000000.00
Lea High Lee   #266-8324    $28093000000.00
Ah C Mowtoe   #334-8294    $29252000000.00
Currie W. D.   #701-4281    $29986000000.00
Cray Z. Cyco   #134-7242    $31668000000.00 

Spec sheet incase anyone wanted to review it


I currently work for an investment/insurance company and I’m looking for clients to call, ones with funds. I need to have a schedule that shows the list of customers to call and the order to be called. The list of customers names and phone numbers are in the file ‘NamesAndPhoneV2.txt’. A second file contains a net worth value for each client. The files are separated for security and protection reasons, but the dollar value are in sync with the client list. (ie first client matches with the first dollar value listed etc.). The file is ‘NamesandPhoneWorth.txt’. There are less than 75 clients you need to handle.
Format ‘NamesAndPhoneV2’
personName phonenumber ;one per line
example: Tom Jones 234-2425
Format ‘NameandPhoneWorth.txt’
$xxxxxxx.xx
example: $12340000.00
I need a list of clients to call each week. Since I am new, I will need to develop some finesse with my communications and develop some style in talking with clients. So I will need to start with the least valued clients first and finish with the more valued ones last. I will call one client per day each day of the week. So I need a list for the first two weeks (5 clients each week). The list needs to be the client’s name, his phone number and his net worth listed in the order to be call. Generate a list for week one and a list for week two. I also would like a list of the last five clients to call, which will be the clients with most net worth. That just gives me a goal to work towards.
You are to use a Heap data structure for this assignment. The lecture showed a heap with the big one on top. You will not have a heap with the big one on top for this assignment.
Happy Heaping

Last edited on
Hello CodeNovice01,

I would be interested in seeing what you did to plan this program. And if you did not you should have.


The files are separated for security and protection reasons, but the dollar value are in sync with the client list.


Understandable, but what you posted for the 2 file they are not in sync. "NameNum.in" has 54 lines and "Worth.in" has 55 lines. One is not correct.

I can understand keeping the 2 input files separate, but I do not see anything in the specs that says that you have to keep them separate.

I see no reason that the program can not use a struct to hold the information while working on it.

In "main":
1
2
3
4
5
6
7
8
9
ifstream in1("NameNum.in");// Names and Numbers of infile

if (!in1)
    out << "Error with NameNum.in check file\n";

ifstream in2("Worth.in");// Dollar Amount infile

if (!in2)
    out << "Error with Worth.in check file\n";

Even if you enter an if statement and print the error message after the if statement ends the program will continue whether the file is open or not.

I am also wondering what "out" is. Did you mean "cout"? Or is "out" an output file?

What you want is something more like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ifstream in1("NameNum.in");// Names and Numbers of infile

if (!in1)
{
    out << "Error with NameNum.in check file\n";

    return 1;  // <--- Leaves program to fix the problem.
}

ifstream in2("Worth.in");// Dollar Amount infile

if (!in2)
{
    out << "Error with Worth.in check file\n";

    return 2;  // <--- Leaves program to fix the problem.
}


Then you have:
1
2
3
double amt[CLIENTS];

string client[CLIENTS];

The subject of the topic starts with "Heap Program", but these 2 lines will create space on the stack not the heap.

You might consider reading both input files to see how many lines are in each file. If they match no problem then you can use the counts to create an array, I suggest an array of structs, on the heap.

I have not tested it yet, but I do not think the while loop will work the way that you want.

While you so some planning, if you do, consider some better variable names.
"inFileNamNum" makes more sense than "in1" and the other could be "inFileWorth" At least it gives you a better idea of what file you are reading.

char dolla = '$';. Not sure if "dolla" is short for something or if it from a language other than English? First I would suggest char dolla{};. There is no need to initialize it to a "$" that you will never use. Another possibility would be char junk{}; since the use is to read and dispose of the first character. You could also make use of inFileWorth.ignore() for the first character of the file and after that I am thinkinginFileWorth.ignore(2) for the rest of the lines. Using the junk variable may be easier to understand.

There may be some useful code in what you have done that may work well with some adjustments. I would not through it away in case you can still use it, but I would re-plan the program to something that works better. This will be much easier than trying to spend 2 or 3 times more time trying to fix what you have. Maybe not.

Andy
Hello CodeNovice01,

In the input file "NaneNum" is this file provided to you? or do you have control over how the file is created?

Right now it is very difficult to read and will require more work that you need to do.

And yes the way the while loop is set up it will not read the files correctly because of the name and the way you are trying to read it.

If you could create the file as:

Taylor marie,939-1512
james wilis thomas,261-8342


It would make it very simple to read.

Andy
Hello CodeNovice01,

Now that I have seen the program run I am impressed in how well the while loop works and I like it.

It did take awhile to figure out how you are using the "count..." variables.

Two things I would do is move the global variable ofstream out("Clients.out");// Outfile to "main' and pass it to the "print" function. Also you need to check that the output file did open, DO NOT just count on that it did open and is usable.

When you showed the output is that the output from the program or is it what you expect?

It is time to see what the full sorted arrays look like to judge what the final output should be.

Andy
Hello CodeNovice01,

I think I have figured out most of the program if this is what you are looking for:

                     Week 1 Calls
------------------------------------------------------
Zeventh Heaven         #834-2563       $  114000000.00
Trigger Phanger        #421-3435       $  286000000.00
Sticky Finger McRidd   #829-9853       $ 1210000000.00
Juan Legged            #882-6246       $ 1455000000.00
Twoseeor Knocksee      #823-8321       $ 1648000000.00

                     Week 2 Calls
------------------------------------------------------
Lu C Kant              #247-7345       $ 2056000000.00
Hasey Moto             #823-8000       $ 2179000000.00
Tau Jam                #532-6871       $ 2226000000.00
Hack N Zakkers         #231-4449       $ 4152000000.00
Legg T.                #587-2839       $ 5060000000.00

                Final Clients to phone
------------------------------------------------------
Stone Rock             #544-2372       $27983000000.00
Lea High Lee           #266-8324       $28093000000.00
Ah C Mowtoe            #334-8294       $29252000000.00
Currie W. D.           #701-4281       $29986000000.00
Cray Z. Cyco           #134-7242       $31668000000.00


The only part I have not check yet is if the names match the amounts correctly.

Andy
> I'm uncertain as to whats wrong within the code itself.
I'm uncertain as what's the problem with the current output
¿are they not sorted? ¿are those not the correct elements? ¿is the client/worth relationship fucked up?
your testcase does not allow to check that at first glance, provide the expected output.


> The subject of the topic starts with "Heap Program", but these 2 lines will
> create space on the stack not the heap.
a heap is a data structure, a balanced binary tree where the value at any node is bigger (or lower) than its left and right subtree
1
2
3
is_heap?(root):
	current = root > root.left and root > root.right
	return current and is_heap?(root.left) and is_heap?(root.right)
(do not confuse with a binary search tree, where left < node < right)

this allows for fast retrieval of the maximum element, and can be used to create a priority queue (and for heap-sort)


> You will not have a heap with the big one on top for this assignment.
¿why not? it would facilitate the «list of the last five clients to call» creation
Handy Andy,

Apologies for the delay in response. The output you provided is identical to the one that I managed to get, I was uncertain if it were correct or not.

When you mentioned about the infiles being one off from the other does that make a issue with the compiler or something else when it is read?

I'll incorporate the suggestion for the char dollar. Makes sense to take it out since its just ignoring it anyhow.

How would I change the variables you mentioned above to be from the heap instead of stack?
ne555,

thank you for the response. The issue in my code that I thought was incorrect is that the program outputs but I wasn't certain if it were correct. Upon looking at it, you can instantly tell some of the values are off, meaning the "client" is in the wrong position compared to the dollar value the client is assigned.
Hello CodeNovice01,


When you mentioned about the infiles being one off from the other does that make a issue with the compiler or something else when it is read?


N. There is no problem with the compiler. Only when it runs. After seeing the while loop work if "Worth" is longer than "NameNum" there is no problem. "NameNum" will read until it reaches "eof" and the "Worth" file will have 1 more line not used. If it was the other way around then "Worth" would not have enough to match the "NameNum" file.

What I started with is this:
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
int main()
{
    ifstream inFileNameNum("NameNum.in");// Names and Numbers of infile

    if (!inFileNameNum)
    {
        cerr << "Error with NameNum.in check file\n";

        return 1;  // <--- Leaves program to fix the problem.
    }

    ifstream inFileWorth("Worth.in");// Dollar Amount infile

    if (!inFileWorth)
    {
        cerr << "Error with Worth.in check file\n";

        return 2;  // <--- Leaves program to fix the problem.
    }

    const std::string outFileName{ "Clients.out" };

    std::ofstream outFile(outFileName);

    if (!outFile)
    {
	    std::cerr << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;

        return 3;
    }

    constexpr int CLIENTS{ 75 }; // Max Array size for clients

    int CountAmount = 0,   // Number of worth
        CountClient = 0,   // Number of names 
        idx{};
    double amt[CLIENTS]{},   // Amount of money per client
        worth; // Amount of worth per client 
    char junk{};
    string client[CLIENTS], // Names of the clients
        name, // Name of client
        input, // Data from infile
        tele; // Phone number of client

    while (std::getline(inFileNameNum, name))
    {
        CountClient++;
    }

    inFileNameNum.clear();
    inFileNameNum.seekg(0);

    while (std::getline(inFileWorth, name))
    {
        CountAmount++;
    }

    inFileWorth.clear();
    inFileWorth.seekg(0);

    if (CountAmount != CountClient)
    {
        std::cerr << "\n     Input files do not match in length!\n";

        //return 3;
    }

    name.clear();

Lines 23 - 30 is what I normally use and it is more a paste into than something I write. The code for input file is basically the same except that it is changed to define an "ifstream".

Where you defined the variables I mostly left alone. I did add line 36, but you may not need this. At the time it helped me understand things a little better and it kept me from having to use one of the variables "CountAmount" or "CountClient" which I used differently to start with.

Lines 45 - 56 just count the number of lines in the files., reset the state bits and set the file pointer to the beginning so it can be read again. Giving value to "CountAmount" or "CountClient" these variables can be used later. That was part of the reason I added "idx".

The last if statement can be used to tell you that the files do not match. The return statement is optional. I am thinking that you should use it because the 2 input files should match. I added 1 line to "NameNum" so they would match.

Since I used "name" to read the files I had to clear it before any next use so it would work correctly.

In the while in the if statement I added this as the first line: name.resize(20,' ');. This way, before you add the phone number, it makes every name the same size. This helps line things up in the output.

At the end of the if statement "CountClient" already has a value and you do not need to be adding toit, so I commented that line out.

I should have mentioned this eariler, but I just noticed it. input[0] = toupper(input[0]);. If you are going to use "tolower" or "toupper" include the header file "cctype". DO NOT count all header files including the same unseen header files that your header files do. Not everyone uses what you do and these functions may not work the way that your program is set up as.

Your next while loop is this:
1
2
3
4
5
while (idx < CountClient)
{
    inFileWorth >> junk >> amt[idx++];
    //CountAmount++;
}

The "junk" variable just reads and does not use the "$" in the file.

The sort is where I made a change.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
                     Week 1 Calls
//------------------------------------------------------
Zeventh Heaven         #834-2563       $  114000000.00
Trigger Phanger        #421-3435       $  286000000.00
Sticky Finger McRidd   #829-9853       $ 1210000000.00
Juan Legged            #882-6246       $ 1455000000.00
Twoseeor Knocksee      #823-8321       $ 1648000000.00

                     Week 2 Calls
//------------------------------------------------------
Lu C Kant              #247-7345       $ 2056000000.00
Hasey Moto             #823-8000       $ 2179000000.00
Tau Jam                #532-6871       $ 2226000000.00
Hack N Zakkers         #231-4449       $ 4152000000.00
Legg T.                #587-2839       $ 5060000000.00

                Final Clients to phone
//------------------------------------------------------
Stone Rock             #544-2372       $27983000000.00
Lea High Lee           #266-8324       $28093000000.00
Ah C Mowtoe            #334-8294       $29252000000.00
Currie W. D.           #701-4281       $29986000000.00
Cray Z. Cyco           #134-7242       $31668000000.00
                     Week 1 Calls
------------------------------------------------------
Cray Z. Cyco           #134-7242       $31668000000.00
Currie W. D.           #701-4281       $29986000000.00
Ah C Mowtoe            #334-8294       $29252000000.00
Lea High Lee           #266-8324       $28093000000.00
Stone Rock             #544-2372       $27983000000.00

                     Week 2 Calls
------------------------------------------------------
LessGo Eat             #233-1984       $26904000000.00
John Marshall          #888-2891       $26749000000.00
Six One Another        #829-8221       $25499000000.00
Hofstra M.             #601-3225       $25461000000.00
Oe Vey                 #177-1423       $25010000000.00

                Final Clients to phone
------------------------------------------------------
Twoseeor Knocksee      #823-8321       $ 1648000000.00
Juan Legged            #882-6246       $ 1455000000.00
Sticky Finger McRidd   #829-9853       $ 1210000000.00
Trigger Phanger        #421-3435       $  286000000.00
Zeventh Heaven         #834-2563       $  114000000.00

Ignore the (//) in the left window. I needed those so it would work right.

The left window is what I did with a bubble sort the right is from your sort code. I am having a hard time with the "LC", (Left Child), and "RC, (Right Child), just to sort a simple array. When you define double amt[CLIENTS]{} this is a simple C style array created on the stack. There is no binary tree or any other tree type structure it is just a contiguous block of memory. The same would be true if this array is created on the heap.

This is the function I used:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void BubbleSort(double amt[], string client[], const int MAXSIZE)
{
    for (int idxo = 0; idxo < MAXSIZE - 1; idxo++)
    {
        for (int idxi = idxo + 1; idxi < MAXSIZE; idxi++)
        {
            if (amt[idxi] < amt[idxo])
            {
                swap(amt[idxi], amt[idxo]);
                swap(client[idxi], client[idxo]);
            }
        }
    }
}

This still made use of your "swap" function.

Memory on the heap or dynamic memory is done with "new" as amtPtr = new double[CountAmount];. If you use "new" to create memory on the heap you must use "delete" when you are done with it. Otherwise you may leave the program with a memory leak that could possibly hang around after the program ends. Since "new" returns a pointer to the beginning of the array you need to create a pointer variable of the same type.

Andy
Hello CodeNovice01,

Sorry I ran out of room.

By reading the input files and counting the lines the variables "CountAmount" and "CountClient" have the total number of lines in each file, so with amtPtr = new double[CountAmount] "CountAmount" or "CountClient", as long as they are the same size, would contain the proper number to create an array large enough to hold the data in the file.

Not sure about the array of strings how that would work to put everything on the heap as the string class will have a variable on the stack pointing to the memory on the heap where it actually stores the contents of the string. Since the string class does all this for you I am not sure if it would work to put everything in the string array on the heap. Something I will have to try. I am thinking it should work.

If there is anything I have missed I will let you know. Any questions let me know.

Andy
Hello CodeNovice01,

Something I did realize.

At the end of "main" your code:
1
2
3
4
5
6
7
8
9
    out << fixed << setprecision(2)
        << "Week 1 Calls \n";
    print(amt, client, CountAmount - 1, CountAmount - 5);

    out << "\nWeek 2 Calls\n";
    print(amt, client, CountAmount - 6, CountAmount - 10);

    out << "\nFinal Clients to phone\n";
    print(amt, client, CountAmount - 50, CountAmount - CountAmount);

In your calls to the print function the 3rd and 4th parameters are wrong. Based on the value of "CountAmount" "week 1" would be printing the end of the array which would be your highest numbers and the 3rd call would be printing the beginning of the array.

I changed the code to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
outFile << fixed << setprecision(2)
    << '\n' << std::string((WIDTH/2)-6,' ') << "Week 1 Calls\n" << std::string(WIDTH, '-') << '\n';
print(outFile, amt, client, 0, 5);

outFile << '\n' << std::string((WIDTH / 2) - 6, ' ') << "Week 2 Calls\n" << std::string(WIDTH, '-') << '\n';
print(outFile, amt, client, 5, 10);

outFile << '\n' << std::string((WIDTH / 2) - 11, ' ') << "Final Clients to phone\n" << std::string(WIDTH, '-') << '\n';
print(outFile, amt, client, CountAmount - 5, CountAmount);

//inFileNameNum.close();  // <--- Good form, but not necessary. Will close when the dtor is called just before the function looses scope.
//inFileWorth.close();
//outFile.close(); 

This appears to work which produces the output I showed you earlier.

Andy
check yet is if the names match the amounts correctly.


A 'simple' way to check is to read each file in tandem and use a map with the amount as key to store the client data. Simple iterations of the map will then give the required data for output. Doesn't use heap as required, but shows the required output for checking purposes.

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
#include <fstream>
#include <map>
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iterator>

struct Contact {
	std::string name;
	std::string tel;
};

int main()
{
	const auto display = [](auto it) {
		std::cout << std::setfill(' ') << std::left << std::setw(25) << it->second.name;
		std::cout << '#' << std::setw(12) << it->second.tel;
		std::cout << '$' << std::right << std::setw(14) << std::fixed << std::setprecision(2) << it->first << '\n';
	};

	std::ifstream in1("NameNum.in");

	if (!in1.is_open())
		return (std::cout << "Error with NameNum.in check file\n"), 1;

	std::ifstream in2("Worth.in");

	if (!in2.is_open())
		return (std::cout << "Error with Worth.in check file\n"), 2;

	std::multimap<double, Contact> clients;

	std::string line;
	double amnt {};
	char dllr {'$'};

	while (getline(in1, line) && (in2 >> dllr >> amnt)) {
		const auto tele {std::find_if_not(line.crbegin(), line.crend(), [](auto ch) {return std::isspace(ch); })};
		const auto telb {std::find_if(tele, line.crend(), [](auto ch) {return std::isspace(ch); })};
		const auto name {std::find_if_not(telb, line.crend(), [](auto ch) {return std::isspace(ch); })};

		clients.emplace(amnt, Contact {std::string(line.cbegin(), name.base()), std::string(telb.base(), tele.base())});
	}

	int cnt {1};
	constexpr int clntsperweek {5};

	std::cout << "Week 1\n" << std::setw(7) << std::setfill('-') << '\n';
	for (auto it = clients.cbegin(); it != clients.cend() && cnt <= clntsperweek * 2; ++it, ++cnt) {
		display(it);
		if (cnt == clntsperweek)
			std::cout << "\nWeek 2\n" << std::setw(7) << std::setfill('-') << '\n';
	}

	std::cout << "\nFinal week\n" << std::setw(11) << std::setfill('-') << '\n';
	for (auto it = std::prev(clients.cend(), clntsperweek); it != clients.cend(); ++it)
		display(it);
}



Week 1
------
Zeventh Heaven           #834-2563    $  114000000.00
Trigger Phanger          #421-3435    $  286000000.00
Sticky Finger McRidder   #829-9853    $ 1210000000.00
Juan Legged              #882-6246    $ 1455000000.00
Twoseeor knocksee        #823-8321    $ 1648000000.00

Week 2
------
Lu C Kant                #247-7345    $ 2056000000.00
Hasey Moto               #823-8000    $ 2179000000.00
Tau Jam                  #532-6871    $ 2226000000.00
Hack N Zakkers           #231-4449    $ 4152000000.00
Legg T.                  #587-2839    $ 5060000000.00

Final week
----------
Stone Rock               #544-2372    $27983000000.00
lea high lee             #266-8324    $28093000000.00
Ah C Mowtoe              #334-8294    $29252000000.00
Currie W. D.             #701-4281    $29986000000.00
Cray Z. Cyco             #134-7242    $31668000000.00

Last edited on
Hello CodeNovice01,

Something I realized when I went to bed last night.

Your code for sorting does work as long as you realize that it is in descending order with the name and highest amount first and name and lowest amount last.

With that in mind your original "print" function works as long as you send it the correct numbers, i.e., (for start and end).

When I changed your print calls:
1
2
3
4
5
6
7
print(outFile, amt, client, CountAmount - 1, CountAmount - 5);
outFile << '\n' << std::string((WIDTH / 2) - 6, ' ') << "Week 2 Calls\n" << std::string(WIDTH, '-') << '\n';

print(outFile, amt, client, CountAmount - 6, CountAmount - 10);

outFile << '\n' << std::string((WIDTH / 2) - 11, ' ') << "Final Clients to phone\n" << std::string(WIDTH, '-') << '\n';
print(outFile, amt, client, CountAmount - 51, CountAmount - CountAmount);

it produced the output of:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
                     Week 1 Calls
//------------------------------------------------------
Zeventh Heaven         #834-2563       $  114000000.00
Trigger Phanger        #421-3435       $  286000000.00
Sticky Finger McRidd   #829-9853       $ 1210000000.00
Juan Legged            #882-6246       $ 1455000000.00
Twoseeor Knocksee      #823-8321       $ 1648000000.00

                     Week 2 Calls
//------------------------------------------------------
Lu C Kant              #247-7345       $ 2056000000.00
Hasey Moto             #823-8000       $ 2179000000.00
Tau Jam                #532-6871       $ 2226000000.00
Hack N Zakkers         #231-4449       $ 4152000000.00
Legg T.                #587-2839       $ 5060000000.00

                Final Clients to phone
//------------------------------------------------------
Stone Rock             #544-2372       $27983000000.00
Lea High Lee           #266-8324       $28093000000.00
Ah C Mowtoe            #334-8294       $29252000000.00
Currie W. D.           #701-4281       $29986000000.00
Cray Z. Cyco           #134-7242       $31668000000.00
                     Week 1 Calls
------------------------------------------------------
Zeventh Heaven         #834-2563       $  114000000.00
Trigger Phanger        #421-3435       $  286000000.00
Sticky Finger McRidd   #829-9853       $ 1210000000.00
Juan Legged            #882-6246       $ 1455000000.00
Twoseeor Knocksee      #823-8321       $ 1648000000.00

                     Week 2 Calls
------------------------------------------------------
Lu C Kant              #247-7345       $ 2056000000.00
Hasey Moto             #823-8000       $ 2179000000.00
Tau Jam                #532-6871       $ 2226000000.00
Hack N Zakkers         #231-4449       $ 4152000000.00
Legg T.                #587-2839       $ 5060000000.00

                Final Clients to phone
------------------------------------------------------
Stone Rock             #544-2372       $27983000000.00
Lea High Lee           #266-8324       $28093000000.00
Ah C Mowtoe            #334-8294       $29252000000.00
Currie W. D.           #701-4281       $29986000000.00
Cray Z. Cyco           #134-7242       $31668000000.00

Your original code is on the left and what I came up with is on the right.

Although your code is over thought and over done, it could be shortened, it does work as long as you know what the sorted arrays look like before you call the "print" function.

Andy
Handy Andy,

I greatly appreciate all of your input and time. BUT I figured out the issue, the code works as its suppose to. The issue was in the "HSwap" function, I had the "if(small != i) as the first loop rather than the last(where it is now) and my names and amounts etc are all identical to your output that you've provided.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Week 1 Calls
  Client	        Phone Number	 Worth
Zeventh Heaven           #834-2563    $114000000.00
Trigger Phanger          #421-3435    $286000000.00
Sticky Finger McRidder   #829-9853    $1210000000.00
Juan Legged              #882-6246    $1455000000.00
Twoseeor Knocksee        #823-8321    $1648000000.00

Week 2 Calls
  Client	Phone Number	 Worth
Lu C Kant        #247-7345    $2056000000.00
Hasey Moto       #823-8000    $2179000000.00
Tau Jam          #532-6871    $2226000000.00
Hack N Zakkers   #231-4449    $4152000000.00
Legg T.          #587-2839    $5060000000.00

Final Clients to phone
  Client	Phone Number	 Worth
Stone Rock       #544-2372    $27983000000.00
Lea High Lee     #266-8324    $28093000000.00
Ah C Mowtoe      #334-8294    $29252000000.00
Currie W. D.     #701-4281    $29986000000.00
Cray Z. Cyco     #134-7242    $31668000000.00
Last edited on
Topic archived. No new replies allowed.