The program works when non negative numbers are entered. I am trying to use user validation and I can get it to display the message about negative numbers and asks to try again but it just repeats the question over when you hit y and aborts when you hit n. This only happens in the calculateRetail function. It works perfectly in the main function. Everything else in the program works.
This program will us a function to calculate the reatial price. The function will
receive the wholesale cost and the markup percent as arguments and return the
retail price. It will aslo check user input and accept no value less than O.*/
#include <iostream>
usingnamespace std;
// Function prototype.
double calculateRetail(double wholesale, double markup);
int main()
{
double wholesale, markup;
char answer;
// Do while loop- do
do
{
cout << "Please enter the wholesale price: ";
cin >> wholesale;
cout << endl;
cout << "Please enter the markup percent as a decimal: ";
cin >> markup;
cout << endl;
calculateRetail(wholesale, markup);
//Asks if user wants to try again.
cout << "Would you like to try again? (y or n)";
cin >> answer;
}while (answer == 'Y' || answer == 'y');
system("pause");
return 0;
}
/*******************************************************************************
* calualteRetail Function *
* This function will receive wholesale price and markup percent as arguments *
* and return the retail price. *
********************************************************************************/
double calculateRetail(double wholesale,double markup)
{
double retail;
char answer;
if (wholesale < 0 || markup < 0)
{
do
{
cout << "You cannot use negative numbers." << endl;
//Asks if user wants to try again.
cout << "Would you like to try again? (y or n)";
cin >> answer;
} while (answer == 'Y' || answer == 'y');
}
else
retail = wholesale + (wholesale*markup);
cout << "The retail price is $ " << retail << endl;
return retail;
}
do
{
cout << "You cannot use negative numbers." << endl;// <----------------------------- Here
// |
//Asks if user wants to try again.// |
cout << "Would you like to try again? (y or n)";// |
cin >> answer; // |
} while (answer == 'Y' || answer == 'y'); // If you press 'Y' || 'y' to try again it jumps back------------->|
This is a Do/While loop, it keeps doing something is something, so while ( Answer == Y || y ) it will keep displaying that message.