Menu trouble

+2 Alexxis Lee · January 24, 2015
This program runs ok except when you choose selection 3- the user types in an address and the tax amount is displayed.  When the address is entered by the user, the program goes into an endless loop.  Anyone have any time to check it out and see what I am doing wrong? I thought to add a getline/ cin.ignore, but can't seem to get it to work:



//This program is for the county auditor who will sort tax data,
//search for specific addresses, and sort the data in ascending order

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

//Function Prototypes
void displayAddressesAmtDue(string[], double[], int);
void sortDisplayAddressTaxDue(string[], double[], int);
void lookUpFunction(string[], double[], int);
void displayLargestDelTaxBill(string[], double[], int);

//Function Display Menu
void displayMenu()
{
const int PROPERTIES = 7;
double amountDue[PROPERTIES] = { 500.00, 1500.00, 15000.00,
25000.00, 6000.00, 1000.00, 20000.00 };

string addresses[PROPERTIES] = { "151 Acorn", "161 Acorn", "200 Main", "500 Arcade",
"162 Acorn", "120 Xenia", "200 Acorn" };

//constants for menu choices
const int ALL_PROPERTIES_AND_TAX_BILL = 1,
LOW_TO_HIGH_ADDRESS_AND_AMOUNT_DUE = 2,
SEARCH_FOR_HOME = 3,
LARGEST_DELINQUENT_TAX_BILL = 4,
EXIT_PROGRAM = 5;

int choice; //menu choice

do
{
//display menu
cout << "-----------------------------------------------------------\n";
cout << "This is the County Auditor's Property Tax Search and Lookup\n\n";
cout << endl;
cout << "Enter 1 = Display all properties and tax bills\n"
<< "Enter 2 = Sort taxes due and addresses in ascending order\n"
<< "Enter 3 = Look up taxes for a specific address\n"
<< "Enter 4 = Display property with largest tax bill\n"
<< "Enter 5 = Exit program\n"
<< "Enter your choice now:   ";
cin >> choice;
cout << endl;

//validate user's choice
while (choice < ALL_PROPERTIES_AND_TAX_BILL || choice > EXIT_PROGRAM)
{
cout << "Please enter a valid menu choice:  ";
cin >> choice;
}

//process user's choice
if (choice != EXIT_PROGRAM)
{
//respond with user's menu selection
switch (choice)
{
case ALL_PROPERTIES_AND_TAX_BILL: displayAddressesAmtDue(addresses, amountDue, PROPERTIES);
break;

case LOW_TO_HIGH_ADDRESS_AND_AMOUNT_DUE: sortDisplayAddressTaxDue(addresses, amountDue, PROPERTIES);
break;

case SEARCH_FOR_HOME: lookUpFunction(addresses, amountDue, PROPERTIES);
break;

case LARGEST_DELINQUENT_TAX_BILL: displayLargestDelTaxBill(addresses, amountDue, PROPERTIES);

}
}

} while (choice != EXIT_PROGRAM);
}

int main()
{

displayMenu();

return 0;
}


//1-Function to Display all properties and tax bills
void displayAddressesAmtDue(string address[], double due[], int properties)
{
int i = 0;
cout << "These are the addresses and their tax bills due\n\n";

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


//2-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\n\n";

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


//3-Function to Look up Taxes by user input 
void lookUpFunction(string address[], double due[], int properties)
{
string home;

cout << "What home would you like to see the taxes for?\n";
getline (cin, home);
cin.ignore();

int i = 0;
for (i = 0; i < properties; i++)
{

if (home == address) cout << "The taxes due =$:  " << setprecision(2) << fixed << due;

}



}


//4-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;
}

//This displays which address has the largest tax bill due

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


Post a Reply

Replies

Oldest  Newest  Rating
+2 Jake Scaife · January 25, 2015
+1 for beautiful code.. not perfect...but beautiful...

Anyway, home is a string and address is an array, they are never going to equate unless they are both null.
0 Alexxis Lee · January 25, 2015
Duh, hey thanks to you both! appreciate it so much.
  • 1

C++

107,004 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator