Loops

Here is the scenario:
Modify your code to check for valid input values. Specifically:
The Wholesale Cost must be positive. { I.e. not zero or negative }
The Mark-up Percentage must be positive but less than or equal to 100%.
The Sales Tax Rate ( Percentage ) must be positive but strictly less than 15%.

Use the most appropriate LOOP construct to get user input until a good value is given.

Be sure your code tells the user that the value entered was wrong, when it is invalid.
If a good value is given, do NOT say anything to the user. I.e. when a good value is given – move on.
Use these input values for each run:

First run should enter all correct values:
Wholesale Cost: $100.00
Mark-up percentage: 8%
Sales tax rate: 3%

Second run
First enter an incorrect value for the wholesale cost – enter a NEGATIVE number,
then enter another incorrect value for the wholesale cost – enter a ZERO,
then enter another incorrect value for the wholesale cost – enter another NEGATIVE number,
then enter a correct wholesale cost of $50.00

Then give a NEGATIVE value for Mark-up percentage
then give a Mark-up percentage value of 200%
then a Mark-up percentage value of 10%

Finally a good value for Sales Tax Rate of 5%.

Third run
First you should enter a correct wholesale cost of $25.00
then a Mark-up percentage value of 100%
then a Sales Tax Rate of -5% ( NEGATIVE 5% )
then a Sales Tax Rate of 0% ( zero )
then a Sales Tax Rate of 15%
then a Sales Tax Rate of 20%
and finally a good value for Sales Tax Rate of 10%.

Here is what I did. I am not sure what I am doing wrong here:
#include <iostream>
#include <iomanip>
using namespace std;

int main ()

{

float wholesale_cost, retail_price, customer_price;
float mark_up_percentage;
float sales_tax_rate;

cout <<"Rebecca Carolina Katz,lab #6 part A"<< endl;

// Variable Declarations
int wholesale_cost;
int retail_price ;
int customer_price;
int mark_up_percentage;
int sales_tax_rate;

// values for the first run.
while (( wholesale_cost being 0 or negative))
{
cout <<"Wholesale Cost:";
cout << wholesale_cost << 100 << endl;
cin >> wholesale_cost
cout <<"Mark_up_ percentage:"
cout << mark_up_percentage << 8 << endl;
cin >> mark_up_percentage
cout <<"Salestax rate:";
cout << salestax_rate << 3 << endl;
cin >> salestax_rate
cout << endl;
}


// values for the second run
while (( wholecost_sale being 0 or negative))
{

cout <<-100;
cout << 0;
cout <<-101;
cout << "Value entered not good";
cout << prompt;
cin >> wholesale_cost;
cout << int retail_price << endl;
cout << -5;
cout << 200:
cout << 10;
cin >> Mark-up percentage
cout << "Value entered not good";
cout << 5;
cin >> Sales Tax Rate: >> endl;

}

// values for the third run
while (( int mark_up_percent <=100) && ( int sales_tax_rate < 15))
{

cout << 25;
cin >> wholesale cost;
cout << 100;
cout << -5;
cout << 0;
cout << 15;
cout << 20;
cin >> Salestax rate:;
cout "Value entered not good" << endl;
cout << 10;
cin >> Salestax rate: >> endl;


}
return 0;
}
The first thing you are doing wrong is not using code tags to make your code legible - see the following for how to add them:

http://www.cplusplus.com/forum/articles/42672/

This makes it far easier for someone to quickly look through and analyse your code.

Second thing is...

while (( wholesale_cost being 0 or negative))

What is this supposed to do? As pseudo code to try to plan out your ideas, I can understand, but you are supposed to implement the ( wholesale_cost being 0 or negative) expression.

You might benefit from reading & trying to understand the concepts outlined on the following page:

http://www.cplusplus.com/doc/tutorial/control/
Last edited on
I am very new to programing so I really do not know what I am suppose to do...do I have to put that while (( wholesale_cost being 0 or negative)) when I am doing the first run? this is what I did now:
// Variable Declarations
int Wholesale_Cost=100;
int Retail_price = 50;
int Customer_Price =25;
int Mark_up_percentage;
int Sales_tax_rate;

// values for the first run.

{
cout <<"Wholesale Cost:";
cout << wholesale_cost << 100 << endl;
cin >> wholesale_cost
cout <<" Mark-up_ percentage:"<< 8 << endl;
cin >> mark_up_percentage
cout <<"Salestax rate:";
cout << salestax_rate << 3 << endl;
cin >> salestax_rate
cout << endl;
}


// values for the second run
while (( wholecost_sale being 0 or negative))
{

cout <<-100;
cout << 0;
cout <<-101;
cout << "Value entered not good";
cout << prompt;
cin >> wholesale_cost;
cout << int retail_price << endl;
cout << -5;
cout << 200:
cout << 10;
cin >> Mark-up percentage
cout << "Value entered not good";
cout << 5;
cin >> Sales Tax Rate: >> endl;

}

// values for the third run
while (( int mark_up_percent <=100) && ( int sales_tax_rate < 15))
{

cout << 25;
cin >> wholesale cost;
cout << 100;
cout << -5;
cout << 0;
cout << 15;
cout << 20;
cin >> Salestax rate:;
cout "Value entered not good" << endl;
cout << 10;
cin >> Salestax rate: >> endl;


}
return 0;
}
Please use code tags:

http://www.cplusplus.com/forum/articles/42672/


while (( wholecost_sale being 0 or negative))
is meaningless in C++.

while (( wholecost_sale <= 0))
has meaning in C++.

while (( int mark_up_percent <=100) && ( int sales_tax_rate < 15))
is incorrect syntax.

while (( mark_up_percent <=100) && ( sales_tax_rate < 15))
is correct syntax.

You have also misunderstood your assignment. 1st run, 2nd run, 3rd run does not mean write 3 different bits of code.
It means write 1 bit of code & run it 3 times, inputting different values each time.

I keep on getting errors! I am so confused:
// Variable Declarations
float Wholesale_Cost;
float Mark_up_percentage;
float Sales_tax_rate;



while (( Wholesale_Cost >=0) && ( Mark_up_percent: <=0.100) && ( Sales_tax_rate < 0.15))
{
cout << "Wholesale Cost:";
cin >> Wholesale_Cost;
cout << "Mark-up percentage:" ;
cin >> Mark_up_percentage;
cout << "Salestax Rate:";
cin >> Sales_tax_rate;


}





return 0 ;

}
Last edited on
Topic archived. No new replies allowed.