Help required

I keep getting errors while trying to write this program, the code is below. Help on what I am doing wrong would greatly be appreciated.

#include <iostream>
using namespace std;
int food()
{int meat, fruits, drinks, total;
cout<<"Enter cost of Meat \n";
cin>>meat;
cout<<"Enter cost of Fruits \n";
cin>>fruits;
cout<<"Enter cost of Drinks \n";
cin>>drinks;
total=meat+fruits+drinks;
if(total>50000)
{cout<<"Total exceeds kshs 50000 \n";
cout<<"Please reduce the cost for one of the items in this category \n";
}
else
{return total;
}
return 0;
}
int rent()
{
int living_house, shop, total;
cout<<"Enter cost of Living House \n";
cin>>living_house;
cout<<"Enter cost of Shop \n";
cin>>shop;
total=living_house+shop;
if(total>50000)
{cout<<"Total exceeds kshs 50000 \n";
cout<<"Please reduce the cost for one of the items in this category \n";
}
else
{return total;
}
int medical()
{int dental, ent, general_checkup, total;
cout<<"Enter cost of Dental \n";
cin>>dental;
cout<<"Enter cost of ENT \n";
cin>>ent;
cout<<"Enter cost of general_checkup \n";
cin>>general_checkup;
total=dental+ent+general_checkup;
if(total>50000)
{cout<<"Total exceeds kshs 50000 \n";
cout<<"Please reduce the cost for one of the items in this category \n";
}
else
{return total;
}
int charity()
{int childrens_home, church, hospitals, total;
cout<<"Enter cost of Childrens Home \n";
cin>>childrens_home;
cout<<"Enter cost of Church \n";
cin>>church;
cout<<"Enter cost of Hospitals \n";
cin>>hospitals;
total=childrens_home+church+hospitals;
if(total>50000)
{cout<<"Total exceeds kshs 50000 \n";
cout<<"Please reduce the cost for one of the items in this category \n";
}
else
{return total;
}
int main()
int grand_total=food()+medical()+rent()+charity;
return 0;
}
I have managed to get the program to compile and run without errors. I just need help calculating the total expenditure of all items using the main function at the bottom. Your assistance would be greatly appreciated.

here is the improved code


#include <iostream>
using namespace std;
int food()
{int meat, fruits, drinks, total;
cout<<"Enter cost of Meat \n";
cin>>meat;
cout<<"Enter cost of Fruits \n";
cin>>fruits;
cout<<"Enter cost of Drinks \n";
cin>>drinks;
total=meat+fruits+drinks;
if(total>50000)
{cout<<"Total exceeds kshs 50000 \n";
cout<<"Please reduce the cost for one of the items in this category \n";
}
else
{return total;
}
return 0;
}
int rent()
{
int living_house, shop, total;
cout<<"Enter cost of Living House \n";
cin>>living_house;
cout<<"Enter cost of Shop \n";
cin>>shop;
total=living_house+shop;
if(total>50000)
{cout<<"Total exceeds kshs 50000 \n";
cout<<"Please reduce the cost for one of the items in this category \n";
}
else
{return total;
}
return 0;
}
int medical()
{int dental, ent, general_checkup, total;
cout<<"Enter cost of Dental \n";
cin>>dental;
cout<<"Enter cost of ENT \n";
cin>>ent;
cout<<"Enter cost of general_checkup \n";
cin>>general_checkup;
total=dental+ent+general_checkup;
if(total>50000)
{cout<<"Total exceeds kshs 50000 \n";
cout<<"Please reduce the cost for one of the items in this category \n";
}
else
{return total;
}
return 0;
}
int charity()
{int childrens_home, church, hospitals, total;
cout<<"Enter cost of Childrens Home \n";
cin>>childrens_home;
cout<<"Enter cost of Church \n";
cin>>church;
cout<<"Enter cost of Hospitals \n";
cin>>hospitals;
total=childrens_home+church+hospitals;
if(total>50000)
{cout<<"Total exceeds kshs 50000 \n";
cout<<"Please reduce the cost for one of the items in this category \n";
}
else
{return total;
}
return 0;
}
int main()
{
int grandtotal=food()+medical()+rent()+charity();
return 0;
}
Use code tags.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
 using namespace std;
 int food()
 {int meat, fruits, drinks, total;
 cout<<"Enter cost of Meat \n";
 cin>>meat; 
 cout<<"Enter cost of Fruits \n";
 cin>>fruits; 
 cout<<"Enter cost of Drinks \n";
 cin>>drinks; 
 total=meat+fruits+drinks;
 if(total>50000)
 {cout<<"Total exceeds kshs 50000 \n";
 cout<<"Please reduce the cost for one of the items in this category \n";
 }
 else
 {return total;
 }
 return 0;
 }
 int rent()
 {
 int living_house, shop, total;
 cout<<"Enter cost of Living House \n";
 cin>>living_house; 
 cout<<"Enter cost of Shop \n";
 cin>>shop; 
 total=living_house+shop;
 if(total>50000)
 {cout<<"Total exceeds kshs 50000 \n";
 cout<<"Please reduce the cost for one of the items in this category \n";
 }
 else
 {return total;
 }
 return 0;
 }
 int medical()
 {int dental, ent, general_checkup, total;
 cout<<"Enter cost of Dental \n";
 cin>>dental; 
 cout<<"Enter cost of ENT \n";
 cin>>ent; 
 cout<<"Enter cost of general_checkup \n";
 cin>>general_checkup; 
 total=dental+ent+general_checkup;
 if(total>50000)
 {cout<<"Total exceeds kshs 50000 \n";
 cout<<"Please reduce the cost for one of the items in this category \n";
 }
 else
 {return total;
 }
 return 0;
 }
 int charity()
 {int childrens_home, church, hospitals, total;
 cout<<"Enter cost of Childrens Home \n";
 cin>>childrens_home; 
 cout<<"Enter cost of Church \n";
 cin>>church; 
 cout<<"Enter cost of Hospitals \n";
 cin>>hospitals; 
 total=childrens_home+church+hospitals;
 if(total>50000)
 {cout<<"Total exceeds kshs 50000 \n";
 cout<<"Please reduce the cost for one of the items in this category \n";
 }
 else
 {return total;
 }
 return 0;
 }
 int main()
 {
 int grandtotal=food()+medical()+rent()+charity();
 return 0;
 }
I don't see anything out of the ordinary and your main adds up each value returned from the functions.
add a print statement to see the result of grandtotal and verify if it is what you were expecting.
Worked fine for me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
using namespace std;
int food()
{double meat, fruits, drinks, total;
cout<<"Enter cost of Meat \n";
cin>>meat;
cout<<"Enter cost of Fruits \n";
cin>>fruits;
cout<<"Enter cost of Drinks \n";
cin>>drinks;
total=meat+fruits+drinks;
if(total>50000)
{cout<<"Total exceeds $50000 \n";
cout<<"Please reduce the cost for one of the items in this category \n";
}
else
{return total;
}
return 0;
}

int rent()
{
double living_house, shop, total;
cout<<"Enter cost of Living House \n";
cin>>living_house;
cout<<"Enter cost of Shop \n";
cin>>shop;
total=living_house+shop;
if(total>50000)
{cout<<"Total exceeds $50000 \n";
cout<<"Please reduce the cost for one of the items in this category \n";}
else{return total;}}

int medical()
{
double dental, ent, general_checkup, total;
cout<<"Enter cost of Dental \n";
cin>>dental;
cout<<"Enter cost of ENT \n";
cin>>ent;
cout<<"Enter cost of general_checkup \n";
cin>>general_checkup;
total=dental+ent+general_checkup;
if(total>50000)
{cout<<"Total exceeds $50000 \n";
cout<<"Please reduce the cost for one of the items in this category \n";
}
else
{return total;
}}

int charity()
{double childrens_home, church, hospitals, total;
cout<<"Enter cost of Childrens Home \n";
cin>>childrens_home;
cout<<"Enter cost of Church \n";
cin>>church;
cout<<"Enter cost of Hospitals \n";
cin>>hospitals;
total=childrens_home+church+hospitals;
if(total>50000)
{cout<<"Total exceeds $50000 \n";
cout<<"Please reduce the cost for one of the items in this category \n";}
else{return total;}}

int main(){
double grand_total=food()+medical()+rent()+charity();
return 0;}
Last edited on
Topic archived. No new replies allowed.