In: Computer Science
Please change this code to follow the rules.
The program must not use global variables. In another words, it must use local variables and pass-by-value or pass-by-reference parameters.
#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;
}
The changed code containing no global variable and parameters passed by call by value method is given below.
Code :
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct expense
{
string Desc;
double exp;
};
void menu(struct expense *arr,int c);
//1. show all
void showArray(struct expense *arr,int c){
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(arr,c);
}
//2. spend
void addArray(struct expense *arr,int c){
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(arr,c);
}
menu(arr,c);
}
//3. search expenses containing this string
void searchDesc(struct expense *arr,int c){
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(arr,c);
}
//4. search expenses with greater than or equal to this amount
void searchExp(struct expense *arr,int c){
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(arr,c);
}
//Menu
void menu(struct expense *arr,int c)
{
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(arr,c);
break;
case 2:
addArray(arr,c);
break;
case 3:
searchDesc(arr,c);
break;
case 4:
searchExp(arr,c);
break;
case 5:
return;
default:
cout<<"Enter Vailid Option"<<endl;
menu(arr,c);
}
}
int main()
{
int c = 0;
expense arr[100];
cout<<"Welcome to my expense tracker.";
menu(arr,c);
return 0;
}
Note : In function definitions where you are declaring array as parameter, you can also use struct expense arr[100] in place of struct expense *arr as both the syntax are correct.
If you still have any doubt regarding the solution then let me know in comment. If it helps, kindly give an upVote to this answer.