In: Computer Science
Please Please fulfill the requirements and error handling and make this in basic C++ construct Thank you!!
Objectives:
Description:
Write a menu-driven program that provides the following options:
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:
Required error handling:
The program MUST perform the following checks:
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:\>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct expense
{
string Desc;
double exp;
};
expense arr[100];
int c = 0;
void menu();
//1. show all
void showArray(){
if (c>0){
for(int i=0;i<c;i++){
cout<<"AMOUNT("<<arr[i].exp<<") DESC"<<arr[i].Desc<<")"<<endl;
}
}else{
cout<<"There is no expense entry available";
}
menu();
}
//2. spend
void addArray(){
string desc;
double exp;
cout<<"Please enter the description for the expense: ";
cin>>desc;
cout<<"Please enter the amount: ";
cin>>exp;
if(desc!="" && exp>0){
c++;
arr[c]= {desc,exp};
}else{
cout<<"Please Enter valid entries";
addArray();
}
menu();
}
//3. search expenses containing this string
void searchDesc(){
string s1;
cout<<"Please enter the search string:";
cin>>s1;
transform(s1.begin(),s1.end(),s1.begin(),::tolower);
for(int k=0;k<c;k++){
string s2 = arr[k].Desc;
transform(s2.begin(),s2.end(),s2.begin(),::tolower);
int x = s1.length();
int y = s2.length();
for(int i=0;i<=y-x;i++){
int j;
for(j=0;j<x;j++){
if(s2[i+j] != s1[j]){
break;
}
}
if(j==x){
cout<<"AMOUNT("<<arr[k].exp<<") DESC"<<arr[k].Desc<<")"<<endl;
}
}
}
menu();
}
//4. search expenses with greater than or equal to this amount
void searchExp(){
double exp1;
cout<<"Please enter the amount:";
cin>>exp1;
for(int i=0; i<c; i++){
if(exp1<=arr[i].exp){
cout<<"AMOUNT("<<arr[i].exp<<") DESC"<<arr[i].Desc<<")"<<endl;
}
}
menu();
}
//Menu
void menu()
{
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;
int input;
cout << "Enter your option:";
cin >> input;
switch (input)
{
case 1:
showArray();
break;
case 2:
addArray();
break;
case 3:
searchDesc();
break;
case 4:
searchExp();
break;
case 5:
return;
default:
cout<<"Enter Vailid Option"<<endl;
menu();
}
}
int main()
{
cout<<"Welcome to my expense tracker.";
menu();
return 0;
}