Knowing what loops to use

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.

Run your code multiple times using the input values I am giving on the next page 

Part A ( Continued)

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 my code please let me know what I am doing wrong. I do not understand what to do

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
  #include <iostream>


using namespace std;

int main ()

{

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

	// Variable Declarations
	float Wholesale_Cost;
	float Mark_up_percentage;
	float Sales_tax_rate;
	 int prompt;
	
	
		while ( (Wholesale_Cost < 0) && ( Mark_up_percentage > 0.100) && ( Sales_tax_rate < 0.15) )
		
		{
			cout << "Wholesale Cost:";
			cin >> Wholesale_Cost > 0;
			cout << "Mark-up percentage:";
			cin  >> Mark_up_percentage ;
			cout << "Salestax Rate:";
			cin >> Sales_tax_rate ;
			cout << "Value entered not good";
			cin >> prompt ;
			cout << endl;
	    } // Assuming the values are good now
		    cout << "Wholesale Cost:";
			cin >> Wholesale_Cost;
			cout << "Mark-up percentage:";
			cin  >> Mark_up_percentage;
			cout << "Salestax Rate:";
			cin >> Sales_tax_rate;
		
		 return 0 ;
		 
}
Line 13-15: These are all uninitialized variables (they contain garbage).

Line 19: The first time through the loop, what value is being compared to 0 for WholesaleCost? Is the comparison true or false? Ditto for Mark_up_percentage and Sales_tax_rate.

Line 23: You can't do a comparison in a cin statement. You don't check here for valid values. If Wholesale_Cost is negative or 0, you continue on as if the number was valid.

Line 25: Ditto for Mark_up_percentage.

Line 27: Ditto for Sales_tax_rate.

Lines 32-37: Why are you asking for the values again?


How are lines 13-15 garbage?
For line 19: I'm just confused as to how to write the while statement. The wholesale cost must be positive only, the mark up percentage must be positive but less than or equal to 100%, and the sales tax rate must be positive but strictly less than 15%. I was told that the invalid input data goes here?

For 32-37 that is not my intention I can't figure out how to make it so that when I put the correct data in my code so that it doesn't say value entered not correct

How are lines 13-15 garbage?

What do you think those variables contain?
Hint: Try doing a cout of them or look at them using a debugger before you reach line 19.

What you want to do is prompt and test each variable before going on to the next. The normal idiom for this is a do while loop.
1
2
3
4
5
6
  do
  {  cout << "Enter blah";
      cin >> blah;
      if (blah <= 0)  // or whatever condition
        cout << "blah must be > 0" << endl;
  } while (blah <= 0);

Note the difference here. Unlike your code, blah is not being tested until it has actually been entered.

The above code fragment is repeated for each of your variables.





Last edited on
for lines 13-15 i thought you always had to declare some kind of variable and i dont know how to use a debugger we never used them in class.

anyway okay so would this be the correct way to do this assignment?
[code][
#include <iostream>
using namespace std;

int main ()

{

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

do
{
if ( Wholesale_Cost > 0) && (Mark_up_percentage > 0.100) && ( Sales_tax_rate >0.15)
cout << "Wholesale Cost:";
cout << "Mark-up percentage:";
cout << "Salestax Rate:";
cout << "Value entered not good";
cin >> prompt ;
cout << endl;
} while (Wholesale_Cost < 0) && (Mark_up_percentage <=0.100) && (Sales_tax_rate < 0.15) // Assuming the variables are good now


return 0 ;

}]
Last edited on
Your code tags are not right.
Please see this article for the proper use of code tags:
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit you post and apply code tags.

I won't respond to your post until your code has line numbers.

Edit: Corrected link.
Last edited on
your link does not work. I tried to edit my post to use for the code but its not working! I clicked on the <> icon inside the format option...
Corrected link for code tag instructions.

so would this be the correct way to do this assignment?

No. I showed you the correct do/while idiom above.
You need to repeat that for each of your variables.

Problems with your latest code post:

Wholesale_Cost, Mark_up_percentage, Sales_tax_rate and prompt are not declared anywhere.

Only the following statement is within the scope of the if statement:
 
cout << "Wholesale Cost:";


You're testing undefined variables. You also haven't initialized or input these variables anywhere. You also have missing parens on your if statement.
 
if ( (Wholesale_Cost > 0) && (Mark_up_percentage > 0.100) && (Sales_tax_rate >0.15) )


Here's the proper way to input your variables (assuming you define them).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
do
  {  cout << "Enter Wholesale Cost";
      cin >> Wholesale_cost;
      if (wholesale_cost <= 0)  // or whatever condition
        cout << "Wholesale cost must be > 0" << endl;
  } while (Wholesale_cost <= 0);
do
  {  cout << "Enter Mark up percentage";
      cin >> Markup_percentage;
      if (Markup_percentage <= 0)  // or whatever condition
        cout << "Markup percentage must be > 0" << endl;
  } while (Markup_percentage <= 0);
do
  {  cout << "Enter sales tax rate";
      cin >> Sales_tax_rate;
      if (Sales_tax_rate <= 0)  // or whatever condition
        cout << "Sales tax rate must be > 0" << endl;
  } while (Sales_tax_rate <= 0);
  // Only now are the variables good 

Last edited on
i thought i would only need to use the while statement since i have to run the program multiple times?
i am sorry if i am being annoying i just dont know what i am doing
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
#include <iostream>
using namespace std;

int main ()
{
// Variable Declarations
float wholesale_cost;
float Mark_up_percentage ;
float Sales_tax_rate ;


        cout <<"Rebecca Carolina Katz,lab #6 part A"<< endl;
        
       while (wholesale_cost <= 0)
  {  cout << "Enter Wholesale Cost:";
      cin >> wholesale_cost;
      if (wholesale_cost <= 0) 
        cout << "Invalid.Wholesale cost must be > 100 and a positive number" << endl;
  } 
  
    while ((Mark_up_percentage <= 1.0) && (Mark_up_percentage > 0.0))
  {  cout << "Enter Mark up percentage:";
      cin >> Mark_up_percentage;
      if ((Mark_up_percentage  > 0.01) && (Mark_up_percentage < 0.0))
        cout << "Invalid.Markup percentage must be < or equal to 100 and must be positive" << endl;
  } 
  
    while ((Sales_tax_rate <= 0.15) && (Sales_tax_rate > 0.00))
  {  cout << "Enter sales tax rate:";
      cin >> Sales_tax_rate;
      if ((Sales_tax_rate > 0.15) && ( Sales_tax_rate < 0.00)) 
        cout << "Invalid.Sales tax rate must be > 0.15 and must be positive" << endl;
  } 
  
  // Only now are the variables good
  
  return 0;
}
Why did you change the do/while to a while statement? I showed you the correct way to do this.

Line 7: Wholesale_cost is an uninitialized variable. Hint: It contains garbage.

Line 14: What do you think the value of Wholesale_cost is the first time through the loop. Hint: You haven't initialized it.

Line 21: ditto for Markup_up_percentage.

Line 28: ditto for Sales_tax_rate.

When i am initializing the wholesale cost, is it suppose to be float wholesale_cost=100?
I have to run this program until i get a good value...i thought in this case you would use a while statement and do while when you have to execute the program at least once? Sorry i am just confused!
it suppose to be float wholesale_cost=100?

Exactly where did you initialize Wholesale_cost?

You didn't answer my question:
AbstractionAnon wrote:
What do you think the value of Wholesale_cost is the first time through the loop. Hint: You haven't initialized it.


You can't use a variable in a while condition before that variable has been initialized and expect it to work. That is why I used do/while loops. The value is used until AFTER it has been input.

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
#include <iostream>
using namespace std;

int main ()
{
// Variable Declarations
float wholesale_cost=100.00;
float Mark_up_percentage=0;
float Sales_tax_rate =0;


        cout <<"Rebecca Carolina Katz,lab #6 part A"<< endl;
        
       do
      { 
	   cout << "Enter Wholesale Cost:";
      cin >> wholesale_cost;
      if (wholesale_cost <= 0) 
        cout << "Invalid.Wholesale cost must be > 100 and a positive number" << endl;
    } while (wholesale_cost >=100)
  
    do
     {  
	   cout << "Enter Mark up percentage:";
      cin >> Mark_up_percentage;
      if ((Mark_up_percentage  > 0.01) && (Mark_up_percentage < 0.0))
        cout << "Invalid.Markup percentage must be < or equal to 100 and must be positive" << endl;
     } while ((Mark_up_percentage <= 1.0) && (Mark_up_percentage > 0.0))
  
   do
    {
	  cout << "Enter sales tax rate:";
      cin >> Sales_tax_rate;
      if ((Sales_tax_rate > 0.15) && ( Sales_tax_rate < 0.00)) 
        cout << "Invalid.Sales tax rate must be > 0.15 and must be positive" << endl;
     } while ((Sales_tax_rate <= 0.15) && (Sales_tax_rate > 0.00))
  
  // Only now are the variables good
  
  return 0;
}
Then i guess i am not doing this assignment the right way?I dont know how to set it up right because i was told to use the while loop..
Last edited on
That looks a lot better except you have the conditions reversed on the while clauses.

Line 18,20: The conditions in your if and while should be the same. You want the loop to continue if the conditions have not been met. You don't want to continue the loop if wholesale_cost is > $100.

Lines 26,28: ditto

Lines 34,36: ditto

it was suppose to be a while statement...she took off points
Topic archived. No new replies allowed.