My professor says A function can not access code within the main body. Where you
ID: 3863052 • Letter: M
Question
My professor says A function can not access code within the main body. Where you have your fixed point notation there is no effect on anything displayed in the function. Please help me to fix this.
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
void standard(double purchaseAmt);
void plusMemberShip(double purchaseAmt);
void premium(double purchaseAmt);
//Program begins with a main function
int main()
{
//Declare variables
char membershipType;
double purchaseAmt = 0.0;
//Prompt and read the membership type
cout << "Membership types: " << endl;
cout << " Standard = S " << endl;
cout << " Plus = P " << endl;
cout << " Premium = R " << endl;
cout << "Enter membership type(S/P/R): ";
cin >> membershipType;
//Prompt and read the total monthly purchase amount
cout << "Enter total monthly purchase amount: ";
cin >> purchaseAmt;
// fixed point notation with no decimal places
cout << std::fixed;
cout << std::setprecision(0); // zero means no decimal places
if (membershipType == 'S' || membershipType == 's')
{
//function calls
standard(purchaseAmt);
}
else if (membershipType == 'P' || membershipType == 'p')
{
plusMemberShip(purchaseAmt);
}
else if (membershipType == 'R' || membershipType == 'r')
{
premium(purchaseAmt);
}
else
cout << " Invalid member ship type" << endl;
cout << endl;
//Pause the system for a while
system("pause");
return 0;
}
double rewardPoints = 0;
//Method definition of standard
void standard(double purchaseAmt)
{
if (purchaseAmt < 75)
rewardPoints = 0.05 * purchaseAmt;
else if (purchaseAmt >= 75 && purchaseAmt <= 149.99)
rewardPoints = 0.075 * purchaseAmt;
else (purchaseAmt >= 150);
rewardPoints = 0.10 * purchaseAmt;
cout << " Reward points in Standard membership: " << rewardPoints << endl;
}
//Method definition of plusMemberShip
void plusMemberShip(double purchaseAmt)
{
if (purchaseAmt < 150)
rewardPoints = 0.06 * purchaseAmt;
else
rewardPoints = 0.13 * purchaseAmt;
cout << " Reward points in plus membership: " << rewardPoints << endl;
}
//Method definition of premium
void premium(double purchaseAmt)
{
if (purchaseAmt < 200)
rewardPoints = 0.04 * purchaseAmt;
else
rewardPoints = 0.15 * purchaseAmt;
cout << " Reward points in premium membership: " << rewardPoints << endl;
}
Explanation / Answer
Hi
I have fixed the issue and highlighted the code changes below
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
void standard(double purchaseAmt);
void plusMemberShip(double purchaseAmt);
void premium(double purchaseAmt);
//Program begins with a main function
int main()
{
//Declare variables
char membershipType;
double purchaseAmt = 0.0;
//Prompt and read the membership type
cout << "Membership types: " << endl;
cout << " Standard = S " << endl;
cout << " Plus = P " << endl;
cout << " Premium = R " << endl;
cout << "Enter membership type(S/P/R): ";
cin >> membershipType;
//Prompt and read the total monthly purchase amount
cout << "Enter total monthly purchase amount: ";
cin >> purchaseAmt;
if (membershipType == 'S' || membershipType == 's')
{
//function calls
standard(purchaseAmt);
}
else if (membershipType == 'P' || membershipType == 'p')
{
plusMemberShip(purchaseAmt);
}
else if (membershipType == 'R' || membershipType == 'r')
{
premium(purchaseAmt);
}
else
cout << " Invalid member ship type" << endl;
cout << endl;
//Pause the system for a while
//system("pause");
return 0;
}
double rewardPoints = 0;
//Method definition of standard
void standard(double purchaseAmt)
{
// fixed point notation with no decimal places
cout << std::fixed;
cout << std::setprecision(0); // zero means no decimal places
if (purchaseAmt < 75)
rewardPoints = 0.05 * purchaseAmt;
else if (purchaseAmt >= 75 && purchaseAmt <= 149.99)
rewardPoints = 0.075 * purchaseAmt;
else (purchaseAmt >= 150);
rewardPoints = 0.10 * purchaseAmt;
cout << " Reward points in Standard membership: " << rewardPoints << endl;
}
//Method definition of plusMemberShip
void plusMemberShip(double purchaseAmt)
{
// fixed point notation with no decimal places
cout << std::fixed;
cout << std::setprecision(0); // zero means no decimal places
if (purchaseAmt < 150)
rewardPoints = 0.06 * purchaseAmt;
else
rewardPoints = 0.13 * purchaseAmt;
cout << " Reward points in plus membership: " << rewardPoints << endl;
}
//Method definition of premium
void premium(double purchaseAmt)
{
// fixed point notation with no decimal places
cout << std::fixed;
cout << std::setprecision(0); // zero means no decimal places
if (purchaseAmt < 200)
rewardPoints = 0.04 * purchaseAmt;
else
rewardPoints = 0.15 * purchaseAmt;
cout << " Reward points in premium membership: " << rewardPoints << endl;
}
Output:
sh-4.2$ g++ -std=c++11 -o main *.cpp
sh-4.2$ main
Membership types:
Standard = S
Plus = P
Premium = R
Enter membership type(S/P/R): s
Enter total monthly purchase amount: 3333
Reward points in Standard membership: 333
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.