Help with user validation statement in a function.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 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>
using namespace 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;
}
closed account (L6b7X9L8)
1
2
3
4
5
6
7
8
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.
I figured it out. And know I have it working properly thank you for your help
Topic archived. No new replies allowed.