Thought I had this going, but I guess not. Would you look it over and get me headed in the right direction please.

Need help creating this to these instructions, mainly with the part where I call an existing files that has already been created and saved in Notepad as attached.(File one and File two). Below are the directions and some code. I think you will be able to tell what I’m trying to do, Thanks

Requirements:
Create a program to output the following. Make sure it is exact (except for the commission and percentage values):
Commissions calculated using a 0.07 value.
Sales Name Commission

See attachments(2)

This will be accomplished using the following:
1. Read the sales and name values from two input files.
2. Create an array of names and sales amounts (5 total).
3. Determine the commission by randomly generating a percentage number between .05 and. 10. Calculate the commission by multiplying this value times the sales amount.
4. Sort the file by name before printing.
5. Print the file to a third output file. The only thing that should show on the screen is the successful message.
6. Main should have no logic other than calling other functions. All actions should be done in functions (print, read files, sort).

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
char arrSales[7];
char arrNames[5][16];
 
const int ARRAY_SIZE = 5;
void PrintArray(char[ARRAY_SIZE][25], char ARRAY_SIZE);
void BubbleSort(char[ARRAY_SIZE][25], char ARRAY_SIZE);
void GetNames(char[ARRAY_SIZE][25], char ARRAY_SIZE);
int main()
{
char arrSales[ARRAY_SIZE][25];
GetNames(arrSales, ARRAY_SIZE);
PrintArray(arrSales, ARRAY_SIZE);
BubbleSort(arrSales, ARRAY_SIZE);
PrintArray(arrSales, ARRAY_SIZE);
return 0;
}
void GetNames(char arrSales [ARRAY_SIZE][25], char Size)
{
for(int i = 0; i < Size; i++)
{
cout << "Enter a char: ";
cin.getline(arrSales[i], 24, '\n');
}

{
cout << "Enter a name";
cin.getline (arrNames[5], 15, '\n');
}
}
void PrintArray(char arrSales[ARRAY_SIZE][25], char Size)
{
for(int i = 0; i < Size; i++)
{
cout << "arrSales[" << i << "] = " << arrSales[i] << endl;
}
}
void BubbleSort(char arrNames[ARRAY_SIZE][25], char Size)
{
char Hold[25];
for(int Pass = 1; Pass < Size; Pass++)
{
for(int i = 0; i < Size - 1; i++)
{
if(strcmp(arrNames[i], arrNames[i + 1]) < 0)
{
strcpy(Hold, arrNames[i]);
strcpy(arrNames[i], arrNames [i + 1]);
strcpy(arrNames[i + 1], Hold);
}
}

}
void BubbleSort(char arrNames[ARRAY_SIZE][25], char Size)
{
char Hold[25];
for(int Pass = 1; Pass < Size; Pass++)
{
for(int i = 0; i < Size - 1; i++)
{
if(strcmp(arrNames[i], arrNames[i + 1]) < 0)
{
strcpy(Hold, arrNames[i]);
strcpy(arrNames[i], arrNames [i + 1]);
strcpy(arrNames[i + 1], Hold);
}
}

On first viewing the code, i see a mistake with your bubble sort logic.
Your both the loops run for SIZE * SIZE times which is not what actually bubble sort is.

As you keep on sorting the arrray one by one you need to shrink the elements which you visit but not visiting the elements which are already sorted.

Try something like this;

void BubbleSort(char arrNames[ARRAY_SIZE][25], char Size)
{
char Hold[25];
for(int i = 0; i < Size - 1; i++)
{
for(int j = i + 1; j < Size ; j++)
{
if(strcmp(arrNames[i], arrNames[j]) > 0) // 
{
strcpy(Hold, arrNames[i]);
strcpy(arrNames[i], arrNames [j]);
strcpy(arrNames[j], Hold);
}
}

Hope it helped, bye.

This article has been dead for over six months. Start a new discussion instead.