+1 (315) 557-6473 

Create a Program to Create Expense Tracking System in C++ Assignment Solution.


Instructions

Objective
Write a C++ assignment to create expense tracking system in C++ language.

Requirements and Specifications

Lab 5B: Track Expenses Using Dynamic Array
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:
  1. Show All
  2. Spend
  3. Search expenses containing this string
  4. Search expenses with greater than or equal to this amount
  5. 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:
  1. The program must produce similar output as example below. The output should be formatted nicely as given.
  2. The program must use array of structs
  3. The program must not use global variables. In another words, it must use local variables and pass-by-value or pass-by-reference parameters.
  4. The program initially allocates a dynamic array of 2 elements. Whenever all elements are used up, the program must allocate double the current capacity. For example, when 2 elements are used up, the program must allocate 4 element-array. If the user enters more than 4 expenses, then the program must allocate 8 element-array.
  5. You should not use data file to save or read from. All operations should be done through the use of arrays and array indices.
  6. 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 (ignore case, but the user may type in any case).
Sample run:
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:\>
Source Code
/*
* Ali Adabi
* Professor An Lam
* Comsc 165
* lab 5
* clion
*
* Dear Professor I hope you take this time to read this Comment. I would like to thank a lot for pushing us to our limits.
* never in my mind I thought i could code this program without outside help. I know it took me 2 weeks to finish it and it is late
* but I promise you I've never been more proud of my code ever and that's because of you and pushing on for better.
* thank you for making me believe it is possible for me to code Like this and do it on my own.
*
* */
/*
* our includes will go down here
* */
#include
#include /// to add string
#include
#include /// for atof and atoi
#include
using namespace std; ///to not use std anymore;
/*
* here is our struct and it has 2 attributes. description and price.
* */
/**/
struct menuItems {
string description;
float price;
};
/*
* our function prototypes will go down in here
* */
string toUpperCase(string);
string toLowerCase(string);
void displayMenu();
void menu(vector array, int maxCap);
void showList(vector items);
void listTaker(vector &items,int maxCap);
void searchBynumber(vector items);
bool search(string s, string target);
void searchByString(vector items);
/*
* main Function
* take ins : -
* returns 0*/
int main() {
cout << "Welcome to my expense tracker.\n" << endl;
int maxCapacity = 100;
//menuItems items[maxCapacity];
vector items;
menu(items, maxCapacity);
return 0;
}
/* toLowerCase Function
* parameters : a string named S
*
*
* returns : A lower cased string named S
*
* this function takes a string s and lower cases each character in it including space and returns it lowercased
* */
string toLowerCase(string s){
string wordLower = s;
for (int i = 0; i < wordLower.length(); i++){
wordLower[i] = tolower(wordLower[i]);
}
return wordLower;
}
/*
* parameters : a string named S
*
*
* returns : An upper cased string named S
*
* this function takes a string s and upper cases each character in it including space and returns it Uppercased
*/
string toUpperCase(string s){
string wordUpper = s;
for (int i = 0; i < wordUpper.length(); i++){
wordUpper[i] = toupper(wordUpper[i]);
}
return wordUpper;
}
/*displayMenu function
* only displays menu adn does nothing else
*
* */
void displayMenu(){
cout << "Expense Tracking Menu: " << endl;
cout << " 1. show all" << endl;
cout << " 2. spend " << endl;
cout << " 3. search expenses containing this string " << endl;
cout << " 4. search expenses with greater than or equal to this amount" << endl;
cout << " 5. exit" << endl;
}
/*
* menu Function
* parameters : an array of struct named items, a passed bby reference size and and integer max
* this function does show our menu and passes all the necessary parameters to other fucntions needed also it has a
* do and switch loop so we can return to menu after finishing with each task
*
* returns : null
* */
void menu(vector items, int maxCap){
int choice;
string buf1;
string buf2;
do {
displayMenu();
cout << "Please enter your option: ";
cin >> choice;
switch(choice){
case 1:
showList(items);
cout << "thank you\n";
break;
case 2:
listTaker(items, maxCap);
break;
case 3:
searchByString(items);
break;
case 4:
searchBynumber(items);
break;
}
}
while(choice != 5);
}
/* showList function
* parameters : takes the items Array of struct / takes referenced size
*
* this functions job is to show whats in our array and also if it's empty print thank you
* */
void showList(vector items){
if(items.size() == 0){
cout << "our list is empty" ;
} else{
for(int i = 0; i < items.size() ; i++){
cout << items[i].price << " " << "for ";
cout << items[i].description << endl;
}
}
}
/* listTaker function
* parameters : takes array of structs / size as reference and maxCap
* this is our function for taking in data for out array. this fucntion does that
* return : null
* */
void listTaker(vector &items, int maxCap){
string buf;
if(items.size() < maxCap) {
cout << "Please enter amount: ";
cin >> buf;
menuItems item; // create item
item.price = atof(buf.c_str());
cin.ignore(100, '\n');
cout << "Please enter the description for the expense: ";
getline(cin, item.description);
// cin.getline(items[i].description, 100 );
items.push_back(item);
}else cout << "No more room in our list";
}
/*
* search by number
* this function searches the array by using nnu bers we give it and returns whatever matches or is greater than the number we
* ask it to
* */
void searchBynumber(vector items){
string buf1;
float amount;
cout << "Please enter the amount: ";
cin >> buf1; amount = atof(buf1.c_str());
for(int i = 0; i < items.size(); i++){
if(items[i].price >= amount){
cout << items[i].description + " "; cout << items[i].price << endl;
}else if(amount <= 0){
cout << "please enter an amount greater than 0" << endl;
}
else
cout << "no match expense for this amount or lower" << endl;
}
}
/*
* this function returns nothing by searches for a specific entry we give it and searches in description and whichever
* matches our data given is printed out
* */
void searchByString(vector items){
// string takeIn;
// cout << "Please Enter the word you want to search in description with: ";
// getline(cin, takeIn);
// toLowerCase(takeIn);
// for(int i = 0; i < size ; i++ ){
// toLowerCase(item[i].description);
// toLowerCase(takeIn);
// if(search(takeIn, item[i].description)){
// cout << takeIn << " was found in " << item[i].description << endl;
// break;
// }else {
// cout << "Sorry we couldn't find " << takeIn << " in descriptions" ;
//
// }
//
// }
// cin.ignore(256, '\n');
string takeIn;
cout << "please enter your word: " ;
cin >> takeIn;
takeIn = toLowerCase(takeIn);
cout << takeIn << endl;
string takeOut;
for(int i = 0; i < items.size(); i++){
takeOut = items[i].description;
takeOut = toLowerCase(takeOut);
size_t found = takeOut.find(takeIn);
if(found != string::npos)
cout << items[i].price << " for " << items[i].description << endl;
}
}
bool search(string s, string target)
{
int index;
index = s.find(target);
// Test for match
if (index != string::npos)
return true;
else
return false;
}