hi im new at C++ program and i would like some feedback on how to make this better please here is my HW assignment
Markup:
Write a program that asks the user to enter an item's wholesale cost and it's markup percentage. It should then display the item's retail price. For example
* if an item's wholesale cost is $5.00 and its markup percentage is 100%, then the item's retail price is 10.00
* if an item's wholesale cost is $5.00 and its markup percentage is 50%, then the item's retail price is 7.50
The program should have a function name - calculateRetail - that receives the wholesale cost and the markup percentage as arguments, and return the retail price of the item.
*** Input Validation: Do not accept negative values for either the wholesale cost of the item or the markup percentage.***
i need help also on what to change the if statement into because my professor doesn't want me to use loop or the if statements in the main only main function consists of only function calls, but i need to keep the error messages since its part of the homework. also need help on where to put like void at cause i forget how or where to work it
void goes in place of a return value. I don't think you need it here.
Put the pre-conditions in the function calculateRetail. Also don't declare the function header calculateRetail inside of main (another function.) In fact, here you don't need to declared the function header at all.
think of the program as a tree. you start at the trunk and move up. so you call your first function which would request the variables, than you need to calculate the total. so make another function for that. then output the value in before you close the first function or in a new function, depending on what you want. also, removing the system pause is a great idea and define your functions like so:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
int addition(int i, int j);
int main(){
std::cout << addition(1,2);
std::cin.ignore(); //possible replacement for system("pause");
return 0;
}
int addition(int i, int j){
return i+j;
}
this is the standard way of doing functions and comes with several benefits such as easier function referencing within functions(you will see if you make a program with alot of functions and see that some of the function references do not work and you get an undeclared error, this is because the function is declared before defining the function, remember scope of declaration or whatever your teacher called it. doing it this way defines the function and then executed the functions after all the definitions have been executed so that the function is always declared before you reference it.)
//Error Message when entering in a negative number
while(wholesaleCost < 0 )
{
cout << "Error invalid wholesale cost...Enter a postive number: $";
cin >> wholesaleCost;
}// end getwholesale
return wholesaleCost;
}
int markupPercent()
{
//Function - Entering a Markup Percent to see how much percent was taken off
cout << "Enter The Markup Percent: ";
cin >> markupPercent;
//Error Message when enter a number that is not positive
while(markupPercent < 0)
{
cout << "Error invalid markup percentage...enter a postive number ";
cin >> markupPercent;
}
}//End Percentage
//Function Displaying the Total Retail Price
void display(double total)
{
cout << fixed << setprecision(2);
cout << "The retail price is $" << endl;
using namespace std;
//calculatedRetail function prototype
double calculatedRetail (double cost, double markUp);
int main()
{
double retail, cost, markUp; //variables set to double data type
cout<<fixed<<showpoint<<setprecision(2); //set outformat for retail price
cout<<"Enter the wholesale cost for the item: $";
cin >>cost;
//validate the cost price
while (cost<0)
{
cout<<"Please enter a positive number. $"<<endl;
cin >>cost;
}
cout<<"Now please enter its markup percentage: ";
cin >>markUp;
//validate the markup percentage
while (markUp<0)
{
cout<<"Please enter a positive number for markup: ";
cin >>markUp;
}
//convert markup percentage to two decimal point
markUp = (markUp/100);
/*************************************************
*****Call calculatedRetail fuction to calculate***
******************retail price********************
*************************************************/
cout<<"The retail price for the item is: "<<endl;
cout<<"$ " <<calculatedRetail(cost, markUp)<<endl;
return 0;
}
double calculatedRetail (double cost, double markUp)
{
return (cost*markUp)+cost; //calculate retail price and return value to main fuction
}