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