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;
}
#include <iostream>
usingnamespace 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.
#include <iostream>
usingnamespace 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;}