-
C++ Problems...
The basic formula of my program works.
Now I'm just trying to get it to only accept positive input numbers.
And when it's over, I want it to ask the user if they want to do it again, and if
they put a Y then do it again, and if they put a N, then quit. This is what I have so far, and I have a few errors. Please help?
Code:
#include <iostream>
#include <iomanip>
using namespace std;
double retail (double, double);
int main()
{
double cost;
double markup;
char answer = 'y';
while (answer = 'y')||(answer = 'Y')
{
cout << "What is the item's wholesale cost? ";
cin >> cost;
cout << endl;
cout << "What is the markup percentage? ";
cin >> markup;
cout << endl;
if (cost >= 0) && (markup >= 0)
{
cout << "The retail price of the item is $ " << retail (cost, markup) << fixed << setprecision(2) << endl;
}
else
{
cout << "Please re-enter values using positive numbers only." << endl;
}
}
cout << "Do you want to try again? (y/n) ";
cin >> answer;
cout << endl;
return (0);
}
double retail (double wholesaleCost, double markupPercentage)
{
return (wholesaleCost + (wholesaleCost * (markupPercentage/100)));
}
-
The compiler does tell you what the errors are, you know: you're only allowed one set of parentheses after while. You should also use any reasonable style of indentation, to see where your loops end visually, which will (should) fix all your loop errors.
-
If you always want a loop to be entered, then rather specifically setting things up so that the condition will always be true the first time around, you're supposed to use a do..while loop.
More parentheses!
-
Instead of:
Code:
while (answer = 'y')||(answer = 'Y')
Do this:
Code:
while ( (answer == 'y') || (answer == 'Y') )
-
>>Now I'm just trying to get it to only accept positive input numbers.
So, you want the "int cost" variable, to make sure it is positive, at all times? Right?
I would have created a new function that looks like this:
Code:
int PositivieInteger(char* s, int minimum, int maximum)
{
int returnvalue = 0;
do
{
cout << s; //Writes out the text you sent with it
cin >> returnvalue; //Takes input from the user
}
while(minimum >= returnvalue || returnvalue >= maximum) ;
//If input (returnvalue) is less than minimum value or bigger than maximum, force user
// to input a new number
return returnvalue;
}
int main()
{
int cost = PositiveInteger("Hello, what is the cost of this item?: ", 0, 1000);
//Now the integer "cost" holds the value from the "returnvalue" (input) from the function above...
return 0;
}
You can simply copy paste this code, its working... The function "PositiveInteger", place it above "int main".
-
Quote:
Originally Posted by
ManyTimes
Code:
while(minimum >= returnvalue || returnvalue >= maximum) ;
//If input (returnvalue) is less than minimum value or bigger than maximum, force user
// to input a new number
so min and max values are not permitted by your code?
your string should be passed as const char* at least