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

Objectives: Perform C++ string object manipulation Understand how to manipulate

ID: 3727422 • Letter: O

Question

Objectives:

Perform C++ string object manipulation

Understand how to manipulate data using arrays

Handle input errors and invalid values

Design and create a well-structure program using C++ basic programming constructs

Description:

Write a menu-driven program that provides the following options:

Show All

Spend

Search expenses containing this string

Search expenses with greater than or equal to this amount

Exit

It allows the user to select a menu option to display all expenses, add new entry, search for a substring and find the list of entries greater a given amount.

Requirements:

The program must produce the same output as provided. The output should be formatted nicely as given.

The program must use array of structs

The program must not use global variables. In another words, it must use local variables and pass-by-value or pass-by-reference parameters.

The program must define the maximum number of entries such as 100 and keeps track of the actual count of the current number of expenses entered by the user

You should not use data file to save or read from. All operations should be done through the use of arrays and array indices.

You must write at least 2 functions.

Required error handling:

The program MUST perform the following checks:

1.Check for invalid amount (negative or 0 number)

2. Description cannot be empty.

3. Search is case-insensitive

sample (please check this)...!

D:>TrackExpensesUsingArray.exe

Welcome to my expense tracker.

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 1

There is no expense entry available.

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 2

Please enter the description for the expense: Monthly telephone and Internet services

Please enter the amount: 45.25

AMOUNT(45.25) DESC(Monthly telephone and Internet services)

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 2

Please enter the description for the expense: Monthly electric, water and gas

Please enter the amount: 200.20

AMOUNT(200.2) DESC(Monthly electric, water and gas)

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 2

Please enter the description for the expense: Rent

Please enter the amount: 1200

AMOUNT(1200) DESC(Rent)

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 2

Please enter the description for the expense: Netflix membership

Please enter the amount: 12.90

AMOUNT(12.9) DESC(Netflix membership)

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 2

Please enter the description for the expense: Amazon membership

Please enter the amount: 99

AMOUNT(99) DESC(Amazon membership)

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 2

Please enter the description for the expense: Monthly gym membership

Please enter the amount: 50

AMOUNT(50) DESC(Monthly gym membership)

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 1

Expenses:

AMOUNT(45.25) DESC(Monthly telephone and Internet services)

AMOUNT(200.2) DESC(Monthly electric, water and gas)

AMOUNT(1200) DESC(Rent)

AMOUNT(12.9) DESC(Netflix membership)

AMOUNT(99) DESC(Amazon membership)

AMOUNT(50) DESC(Monthly gym membership)

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 3

Please enter the search string: membership

AMOUNT(12.9) DESC(Netflix membership)

AMOUNT(99) DESC(Amazon membership)

AMOUNT(50) DESC(Monthly gym membership)

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 3

Please enter the search string: MEMBERSHIP

AMOUNT(12.9) DESC(Netflix membership)

AMOUNT(99) DESC(Amazon membership)

AMOUNT(50) DESC(Monthly gym membership)

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 4

Please enter the amount: 50

AMOUNT(200.2) DESC(Monthly electric, water and gas)

AMOUNT(1200) DESC(Rent)

AMOUNT(99) DESC(Amazon membership)

AMOUNT(50) DESC(Monthly gym membership)

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 4

Please enter the amount: 200

AMOUNT(200.2) DESC(Monthly electric, water and gas)

AMOUNT(1200) DESC(Rent)

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 4

Please enter the amount: 1000

AMOUNT(1200) DESC(Rent)

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 2

Please enter the description for the expense: Home repair and improvement

Please enter the amount: -1

Invalid amount. Amount cannot be negative or string. Please try it again.

Please enter the amount: -100

Invalid amount. Amount cannot be negative or string. Please try it again.

Please enter the amount: -1000

Invalid amount. Amount cannot be negative or string. Please try it again.

Please enter the amount: 175.75

AMOUNT(175.75) DESC(Home repair and improvement)

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 1

Expenses:

AMOUNT(45.25) DESC(Monthly telephone and Internet services)

AMOUNT(200.2) DESC(Monthly electric, water and gas)

AMOUNT(1200) DESC(Rent)

AMOUNT(12.9) DESC(Netflix membership)

AMOUNT(99) DESC(Amazon membership)

AMOUNT(50) DESC(Monthly gym membership)

AMOUNT(175.75) DESC(Home repair and improvement)

Expense Tracking Menu:

1. show all

2. spend

3. search expenses containing this string

4. search expenses with greater than or equal to this amount

5. exit

Enter your option: 5

D:>

Explanation / Answer

#include <iostream>

#include <cstdlib>

using namespace std;

struct expenses

{

string desc;

double amount;

};

void show_all(expenses *exp, int n)

{

int i = 0;

if (n == 0)

cout << " There is no expense entry available "

<< endl;

else

{

cout << "Expenses: " << endl;

while (i < n)

{

cout << "AMOUNT(" << exp[i].amount << ") DESC(" << exp[i].desc << ")" << endl;

i++;

}

}

}

void spend(expenses *exp, int n)

{

double amount;

string desc;

getline(cin, exp[n].desc);

while (1)

{

cout << "Please enter the description for the expense: ";

getline(cin, exp[n].desc);

if (exp[n].desc != "")

break;

cout << "Invalid Description. Description cannot be empty" << endl;

}

char am[100];

while (1)

{

cout << "Please enter the amount: ";

cin >> am;

exp[n].amount = atof(am);

if (exp[n].amount > 0)

break;

cout << "Invalid amount. Amount cannot be negative or string. Please try it again." << endl;

}

cout << "AMOUNT(" << exp[n].amount << ") DESC(" << exp[n].desc << ")" << endl;

}

void search_amount(expenses *exp, int n)

{

int i = 0, search;

cout << "Please enter the amount:";

cin >> search;

while (i < n)

{

if (exp[i].amount >= search)

{

cout << "AMOUNT(" << exp[i].amount << ") DESC(" << exp[i].desc << ")" << endl;

}

i++;

}

}

void search_desc(expenses *exp, int n)

{

string search;

cout << "Please enter the search string: ";

cin >> search;

int i = 0, j, k;

j = 0;

while (j < search.length())

{

search[j] = tolower(search[j]);

j++;

}

while (i < n)

{

string d_str = exp[i].desc;

j = 0;

while (j < exp[i].desc.length())

{

d_str[j] = tolower(d_str[j]);

j++;

}

if (exp[i].desc.find(search) != string::npos)

{

cout << "AMOUNT(" << exp[i].amount << ") DESC(" << exp[i].desc << ")" << endl;

}

i++;

}

}

int main()

{

int option, n = 0;

expenses exp[100];

cout<<"Welcome to my expense tracker. "<<endl;

do

{

cout << " Expense Tacking Menu:" << endl;

cout << " 1. show all 2. spend 3. search expenses containing this string 4. search expenses with greater than or equal to this amount 5. exit" << endl;

cout << "Enter your option: ";

cin >> option;

switch (option)

{

case 1:

show_all(exp, n);

break;

case 2:

spend(exp, n);

n++;

break;

case 3:

search_desc(exp, n);

break;

case 4:

search_amount(exp, n);

break;

case 5:

exit(0);

break;

default:

cout << "Invalid choice" << endl;

}

} while (option != 5);

}