Question

In: Computer Science

Please change this code to follow the rules. The program must not use global variables. In...

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;

}

Solutions

Expert Solution

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.


Related Solutions

CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
Write a program to validate Canadian Postal Codes. A postal code must follow the pattern of...
Write a program to validate Canadian Postal Codes. A postal code must follow the pattern of L9L9L9 where: L is a letter 9 is a digit Your program should continue accepting postal codes until the user enters the word “exit”. Sample run (user input is shown in bold underline): Enter a postal code: T2T-3X7 Postal code wrong length, format L9L9L9 Enter a postal code: T2T3AA Postal code has letters where there are supposed to be digits Enter a postal code:...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
C++ code Write a program to illustrate how to use the temporary class. Your program must...
C++ code Write a program to illustrate how to use the temporary class. Your program must contain statements that would ask the user to enter data of an object and use the setters to initialize the object. Use three header files named main.cpp, temporary.h, and temporaryImp.cpp An example of the program is shown below: Enter object name (rectangle, circle, sphere, or cylinder: circle Enter object's dimensions: rectangle (length and width) circle (radius and 0) sphere (radius and 0) rectangle (base...
Simple code please thats easy to follow. C++ Write a program that can be used to...
Simple code please thats easy to follow. C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick Sort. Program must: Read an array size from the user, dynamically an array of that size, and fill the array with random numbers Sort the array with the Insertion Sort, MergeSort and QuickSort algorithms studied in class, doing a time-stamp on each sort. Use your program to measure and record the time needed to sort random arrays...
Use a style sheet to define the following rules and implement the given HTML code. Please...
Use a style sheet to define the following rules and implement the given HTML code. Please put your style information within the same file as the HTML code. Rules • Hyperlinks using the nodec class should display no decoration. • Hyperlinks should display text in white with a green background color when the mouse pointer is held over the link. (use the hover pseudo-class) • Unordered lists not nested within any other lists should be displayed in blue text and...
What are global variables, how are global variables used in Arm architecture. Please feel free to...
What are global variables, how are global variables used in Arm architecture. Please feel free to use your own examples.
C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes....
C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes. Example Program Session (implement some linefeed ‘\n’ formatting): Enter the limit: 1000 Primes up to 1000    2    3    5    7   11   13   17   19   23   29   31   37   41   43   47   53 59   61   67   71   73   79   83   89   97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
*Answer must be in C++ and please use a Windows machine NOT IOS! Write a program...
*Answer must be in C++ and please use a Windows machine NOT IOS! Write a program as follows: Ask the user for an integer representing a number of integers to be sorted. Create an array of integers of the size provided by user. Initialize the array to zeros. Ask the user for and populate the array with user input. Output the array elements on one line separated by a space. Write a function name “supersort” that takes an integer pointer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT