User - Defined Functions The cost to become a member of a fitness center is as f
ID: 3875683 • Letter: U
Question
User - Defined Functions The cost to become a member of a fitness center is as follows: The senior citizens discount is 30% If the membership is bought and paid for 12 or more months, the discount is 15% If more than five personal training sessions are bought and paid for, the discount on each session is 20%. Write a menu-driven program that determines the cost of a new membership. Your program must contain a function that displays the general information about the fitness center and its charges, a function to get all of the necessary information to determine the membership cost, and a function to determine the membership cost. Use appropriate parameters to pass information in and out of a function. (Do not use any global variables.)
Please use this code, I'm trying to figure out where and what to input to solve the problem. What am I missing?
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
void displayInfo();
int readData();
int memCost(int, int, int);
//the main function
int main ()
{
int cost;
displayInfo();
cost = readData();
cout << "The cost of the membership is :$" << cost << endl;
system ("PAUSE");
return 0;
}
//the display function
void displayInfo()
{
cout<<" Fitness Center - General Information " << endl;
cout<<"Membership for month: $20 ";
cout<<"Personal training sessions: $15 ";
cout<<"Discount for Senior citizens: 30% ";
cout<<"Discount for purchasing 12 or more months 15% ";
cout<<"Discount for purchasing 5 or more personal training sessions 20% ";
}
//the read data function
int readData ()
{
int srCitizen, proChoice, months, sessions, cost;
cout<<"Enter your purchase details... ";
cout<<"Enter 1 if you are a senior citizen or 0 if not : ";
cin>>srCitizen;
if(srCitizen != 1)
{
srCitizen =0;
cout<<" Choose your fitness program... ";
cout<<"1. Monthly membership ";
cout<<"2. Personal Training ";
cout<<" Enter your choice : ";
}
do
{
cin>>proChoice;
if (proChoice<1 || proChoice>2)
cout<<"Enter a valid choice :";
}
while(proChoice<1 || proChoice>2);
if(proChoice==1)
{
cout<<"Enter number of months you want : ";
cin>>months;
}
if(proChoice==2)
{
cout<<"Enter number of sessions you want : ";
cin>>sessions;
cost = memCost(srCitizen, months, sessions);
}
return cost;
}
//the memCost function
int memCost(int srCitizen, int months, int sessions)
{
int cost;
if(sessions<=5)
{
cost = sessions*20;
}
else
{
cost = sessions - (sessions * 0.20);
}
if(months<=12)
{
cost = months * 20;
}
else
{
cost = months * (months * 0.15);
}
if(srCitizen==1)
{
cost = cost - (cost * 0.30);
}
return cost;
}
Explanation / Answer
if(proChoice==1)
{
cout<<"Enter number of months you want : ";
cin>>months;
}
if(proChoice==2)
{
cout<<"Enter number of sessions you want : ";
cin>>sessions;
cost = memCost(srCitizen, months, sessions);
}
In this code given above in function readData (): you are missing if-else statement. The statements for a good and valid code should have if-else instead of if-if. Try convertig to else if(proChoice==2).
Also, i have made bold the edits in the code I suggest below:
int memCost(int srCitizen, int months, int sessions)
{
int cost;
if(sessions<=5)
{
cost = sessions*20;
}
else
{// discount on each session is 20% so 1 session becomes .20*20
cost = 0.20*20*sessions;
}
if(months<=12)
{
cost = months * 20;
}
else
{ // membership for 1 month is 20$ so total cost will be 20*months -15%
cost = months *20 * (1-0.15);
}
if(srCitizen==1)
{
cost = cost - (cost * 0.30);
}
return cost;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.