Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I\'m working on a C++ project and am getting a few errors here, mostly ne

ID: 3887968 • Letter: H

Question

Hello, I'm working on a C++ project and am getting a few errors here, mostly need help with the loadArray function and the look up taxes function. Here is my code so far:

#include
#include
#include
#include
using namespace std;

//Functions
void displayAddressesAmtDue(string[], double[], int);
void sortDisplayAddressTaxDue(string[], double[], int);
void lookUpTaxes(string[], double[], int);
void displayLargestDelTaxBill(string[], double[], int);
void loadArrays(string[], double, int &);
//globals
const int SIZE = 7;
string address[7];
double amtDue[7];

int loadArrays(string address[], double amtDue[], int length)
{
    int count = 0;
    ifstream loadTaxes;
    loadTaxes.open("prog3.txt");
   if (loadTaxes.fail()) {
        // this will alert user if there is a problem opening the file
        cout << "Error opening the file ";
        return 0;
    }
    for (int i = 0; i < length; i++)
    {
        std::getline(loadTaxes >> std::ws ,address[i]);
        loadTaxes >> amtDue[i];
        cout << address[i] << endl;
        cout << "$" << amtDue[i] << endl;
    }
}
//Show menu function
void showMenu()
{
int choice; //menu choice
do
{
//display menu
cout << "----------------------------------------------------------- ";
cout << "County Auditor Database Search Menu: ";
cout << endl;
cout << "Enter 1 = All Taxes and Properties "
<< "Enter 2 = Due taxes (Ascending) "
<< "Enter 3 = Display largest balance "
<< "Enter 4 = Address look-up "
<< "Enter 5 = Close County Auditor Search Program "
<< "Selection:   ";
cout << "----------------------------------------------------------- ";
cin >> choice;
cout << endl;

//validate user's choice
while (choice < 1 || choice > 5)
{
cout << "Enter a valid selection: ";
cin >> choice;
}

//process user's choice
if (choice != 5)
{
//respond with user's menu selection
switch (choice)
{
   case 1: displayAddressesAmtDue(address, amtDue, SIZE);
break;
   case 2: sortDisplayAddressTaxDue(address, amtDue, SIZE);
break;
   case 3: displayLargestDelTaxBill(address, amtDue, SIZE);
break;
   case 4: lookUpTaxes(address, amtDue, SIZE);

}
}

} while (choice != 5);
}

int main()
{

showMenu();

return 0;
}


//Function to Display all properties and tax bills
void displayAddressesAmtDue(string address[], double due[], int properties)
{
int i = 0;
cout << "All current accounts (address & amount due): ";

for (i = 0; i < properties; i++)
{
cout << address << setw(4) << " = $" << setprecision(2) << fixed << due;
cout << endl;
}
system("pause");
cout << " ";
}


//Function to sort taxes and addresses in ascending order
void sortDisplayAddressTaxDue(string address[], double due[], int properties)
{
int minIndex, index = 0;
string minValue2;
double minValue1 = 0;

//sort taxes due
for (int startScan = 0; startScan < (properties - 1); startScan++)
{
minIndex = startScan;
minValue1 = due[startScan];

for (index = startScan + 1; index < properties; index++)
{
if (due[index] < minValue1)
{
minValue1 = due[index];
minIndex = index;
minValue2 = address[index];
}
}
due[minIndex] = due[startScan];
due[startScan] = minValue1;
address[minIndex] = address[startScan];
address[startScan] = minValue2;
}

//display addresses and taxes due
int i = 0;

cout << "These are the sorted addresses and their tax bills due ";

for (i = 0; i < properties; i++)
{
cout << address << setw(4) << " = $ " << setprecision(2) << fixed << due;
cout << endl;
}
system("pause");
cout << " ";
}

//Function to Display largest tax bill
void displayLargestDelTaxBill(string address[], double due[], int properties)
{
int minIndex, index = 0;
string minValue2;
double minValue1 = 0;

//This sorts taxes due
for (int startScan = 0; startScan < (properties - 1); startScan++)
{
minIndex = startScan;
minValue1 = due[startScan];

for (index = startScan + 1; index < properties; index++)
{
if (due[index] < minValue1)
{
minValue1 = due[index];
minIndex = index;
minValue2 = address[index];
}
}
due[minIndex] = due[startScan];
due[startScan] = minValue1;
address[minIndex] = address[startScan];
address[startScan] = minValue2;
}

cout << "The property with the largest tax bill due is " << address[properties - 1] << " = $ " << setprecision(2) << fixed << due[properties - 1];
cout << endl;
system("pause");
cout << " ";
}

//Function to Look up Taxes by user input

void lookUpTaxes(int amtDue[], string address[]){
    string searched;
    cout<<"Enter Address to search: ";
   cin.ignore();
   getline(cin, searched);
  
  
    int i;
    bool notFound=true;
    for(i=0;i     {
        if(searched == address[i])
        {
            cout<< fixed <             cout<<"Tax details ";
            cout<<"Address: "<             cout<<"Taxes: $"<             notFound=false;
        }
    }
    if(notFound)
    {
        cout<<"Tax details not found ";
        system("pause");
}

    }

Explanation / Answer

Given below is the fixed code.

1. Removed all globals since it is not a good practice. Used those variables inside showMenu()

2. Also instead of fixing the number of records to 7, made changes to program so that it will figure out how many records were present when EOF was reached

3. Fixed sorting and finding largest due code

Hope it helps. If it does , please don't forget to rate the answer. Thank you very much.


#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
//Functions
void displayAddressesAmtDue(string[], double[], int);
void sortDisplayAddressTaxDue(string[], double[], int);
void lookUpTaxes(string[], double[], int);
void displayLargestDelTaxBill(string[], double[], int);
int loadArrays(string[], double []); //returns the number of records loaded
void showMenu();

int main()
{
cout << fixed << setprecision(2) ; //set once for the full program
showMenu();
return 0;
}


int loadArrays(string address[], double amtDue[])
{
int count = 0;
ifstream loadTaxes;
loadTaxes.open("prog3.txt");
if (loadTaxes.fail()) {
// this will alert user if there is a problem opening the file
cout << "Error opening the file " << endl;
return 0;
}
  
while(true)
{
getline(loadTaxes, address[count]);
if(loadTaxes.eof()) //we can check for eof only after we read some data
break;
  
loadTaxes >> amtDue[count];
loadTaxes.ignore();
count++;
}
  
loadTaxes.close();
return count;
}

//Show menu function
void showMenu()
{
int choice; //menu choice
  
//store upto maximum 20 records
string address[20];
double amtDue[20];
int count;
  
count = loadArrays(address, amtDue);
  
do
{
//display menu
cout << "----------------------------------------------------------- ";
cout << "County Auditor Database Search Menu: ";
cout << endl;
cout << "Enter 1 = All Taxes and Properties "
<< "Enter 2 = Due taxes (Ascending) "
<< "Enter 3 = Display largest balance "
<< "Enter 4 = Address look-up "
<< "Enter 5 = Close County Auditor Search Program " ;
  
cout << "----------------------------------------------------------- ";
cout << "Selection: ";
cin >> choice;
cout << endl;
//validate user's choice
while (choice < 1 || choice > 5)
{
cout << "Enter a valid selection: ";
cin >> choice;
}
//process user's choice
if (choice != 5)
{
//respond with user's menu selection
switch (choice)
{
case 1: displayAddressesAmtDue(address, amtDue, count);
break;
case 2: sortDisplayAddressTaxDue(address, amtDue, count);
break;
case 3: displayLargestDelTaxBill(address, amtDue, count);
break;
case 4: lookUpTaxes(address, amtDue, count);
}
}
} while (choice != 5);
}


//Function to Display all properties and tax bills
void displayAddressesAmtDue(string address[], double due[], int properties)
{
int i = 0;
cout << "All current accounts (address & amount due): ";
for (i = 0; i < properties; i++)
{
cout << left << setw(40) << address[i] << "$" << setw(6) << due[i];
cout << endl;
}
system("pause");
cout << " ";
}

//Function to sort taxes and addresses in ascending order
void sortDisplayAddressTaxDue(string address[], double due[], int properties)
{
int minIndex, index = 0;
  
double minValue = 0;
//sort taxes due
for (int startScan = 0; startScan < (properties - 1); startScan++)
{
minIndex = startScan;
minValue = due[startScan];
for (index = startScan + 1; index < properties; index++)
{
if (due[index] < minValue)
{
minValue = due[index];
minIndex = index;
  
}
}
if(startScan != minIndex) //swap only if needed
{
//swap due amount
double temp = due[minIndex];
due[minIndex] = due[startScan];
due[startScan] = temp;
  
//swap address
string tempstr = address[minIndex];
address[minIndex] = address[startScan];
address[startScan] = tempstr;
}
}
//display addresses and taxes due
int i = 0;
cout << "These are the sorted addresses and their tax bills due ";
for (i = 0; i < properties; i++)
{
cout << left << setw(40) << address[i] << "$" << setw(6) << due[i];
cout << endl;
}
system("pause");
cout << " ";
}
//Function to Display largest tax bill
void displayLargestDelTaxBill(string address[], double due[], int properties)
{
int maxIdx = 0;
  
//This sorts taxes due
for (int startScan = 0; startScan < properties; startScan++)
{
if(due[startScan] > due[maxIdx])
maxIdx = startScan;
}
  
cout << "The property with the largest tax bill due is " << address[maxIdx ] << " = $ " << due[maxIdx];
cout << endl;
system("pause");
cout << " ";
}
//Function to Look up Taxes by user input

void lookUpTaxes(string address[], double amtDue[], int properties){
string searched;
cout<<"Enter Address to search: ";
cin.ignore();
getline(cin, searched);
  
  
int i;
bool notFound=true;
for(i = 0; i < properties; i++)
{
if(searched == address[i])
{
cout << "Tax details ";
cout << "Address: " << address[i] << endl;
cout << "Taxes: $" << amtDue[i] << endl;
notFound = false;
break;
}
}
  
if(notFound)
{
cout<<"Tax details not found ";
system("pause");
}
}

input file: prog3.txt

Address1
500
Address2
350
Address3
450
Address4
200

output

-----------------------------------------------------------
County Auditor Database Search Menu:


Enter 1 = All Taxes and Properties
Enter 2 = Due taxes (Ascending)
Enter 3 = Display largest balance
Enter 4 = Address look-up
Enter 5 = Close County Auditor Search Program
-----------------------------------------------------------
Selection: 1

All current accounts (address & amount due):

Address1 $500.00
Address2 $350.00
Address3 $450.00
Address4 $200.00
sh: pause: command not found

-----------------------------------------------------------
County Auditor Database Search Menu:


Enter 1 = All Taxes and Properties
Enter 2 = Due taxes (Ascending)
Enter 3 = Display largest balance
Enter 4 = Address look-up
Enter 5 = Close County Auditor Search Program
-----------------------------------------------------------
Selection: 2

These are the sorted addresses and their tax bills due

Address4 $200.00
Address2 $350.00
Address3 $450.00
Address1 $500.00
sh: pause: command not found

-----------------------------------------------------------
County Auditor Database Search Menu:


Enter 1 = All Taxes and Properties
Enter 2 = Due taxes (Ascending)
Enter 3 = Display largest balance
Enter 4 = Address look-up
Enter 5 = Close County Auditor Search Program
-----------------------------------------------------------
Selection: 3

The property with the largest tax bill due is Address1 = $ 500.00
sh: pause: command not found

-----------------------------------------------------------
County Auditor Database Search Menu:


Enter 1 = All Taxes and Properties
Enter 2 = Due taxes (Ascending)
Enter 3 = Display largest balance
Enter 4 = Address look-up
Enter 5 = Close County Auditor Search Program
-----------------------------------------------------------
Selection: 4

Enter Address to search:
Address3
Tax details
Address: Address3
Taxes: $450.00
-----------------------------------------------------------
County Auditor Database Search Menu:


Enter 1 = All Taxes and Properties
Enter 2 = Due taxes (Ascending)
Enter 3 = Display largest balance
Enter 4 = Address look-up
Enter 5 = Close County Auditor Search Program
-----------------------------------------------------------
Selection: 5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote