get rid of a statment without affecting the program

how can I rewrite the program so that the continue statement is no longer needed.

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
  // This program calculates the charges for DVD rentals.
// Every third DVD is free.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int dvdCount = 1;	//	DVD counter
int numDVDs;	//	Number of DVDs rented
double total = 0.0;	//	Accumulator
char current;	//	Current release, Y or
// Get the number of DVDs.
cout <<"How many DVDs are being rented? ";
cin >> numDVDs;
// Determine the charges,
do
{
   if ((dvdCount % 3) == 0)

    {

cout << "DVD #" << dvdCount << " is free!\n";
continue; // Immediately start the next iteration GET RID OF THIS WITHOUT 
// HAVING PROBLEMS

    }

    cout <<"Is DVD #" << dvdCount;
    cout << " a current release? (Y/N) ";
    cin >> current;
    if  (current == 'Y'	|| current == 'y')
        total += 3.50;

    else

    total += 2.50;

} while (dvdCount++ < numDVDs);
// Display the total
cout <<fixed<< showpoint<<setprecision(2);
cout<<"The total is $" <<total <<endl;
return 0;
}
Last edited on
someone said change outer if()statement into an if () else statement
but how is that will help?
You have in loop:
1
2
3
4
5
if ( cond ) {
  statementA;
  continue;
}
statementB;

The statementA is executed only if cond is true.
The statementB is executed only if cond is false.

You have on lines 32-37:
1
2
3
4
5
6
if ( cond ) {
  total += 3.5;
}
else {
  total += 2.5;
}

The +=3.5 is executed only if cond is true.
The +=2.5 is executed only if cond is false.
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
// This program calculates the charges for DVD rentals.
// Every third DVD is free.
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int dvdCount = 1;    //    DVD counter
    int numDVDs;    //    Number of DVDs rented
    double total = 0.0;    //    Accumulator
    char current;    //    Current release, Y or
    
    // Get the number of DVDs.
    cout <<"How many DVDs are being rented? ";
    cin >> numDVDs;
    
    // Determine the charges,
    do
    {
        if ((dvdCount % 3) == 0)
            cout << "DVD #" << dvdCount << " is free!\n";
        else
        {
            cout <<"Is DVD #" << dvdCount;
            cout << " a current release? (Y/N) ";
            cin >> current;
            if  ( toupper(current) == 'Y')
                total += 3.50;
            else
                total += 2.50;
        }
        
    } while (dvdCount++ < numDVDs);
    
    // Display the total
    cout << fixed << showpoint << setprecision(2);
    cout<<"The total is $" <<total <<endl;
    return 0;
}
 
total += toupper(current) == 'Y' ? 3.5 : 2.5;

Hello Leonardo797,

Something to consider:
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
#include <cctype>  // <--- Added.
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int dvdCount = 1;	//	DVD counter
    int numDVDs;	//	Number of DVDs rented
    double total = 0.0;	//	Accumulator
    char current;	//	Current release, Y or

    // Get the number of DVDs.
    cout << "How many DVDs are being rented? ";
    cin >> numDVDs;

    // Determine the charges,
    do
    {
        //if ((dvdCount % 3) == 0)
        if (!(dvdCount % 3))  // <--- Could be shortened to.
        {
            cout << "DVD #" << dvdCount << " is free!\n";
        }
        else
        {
            cout << "Is DVD #" << dvdCount;
            cout << " a current release? (Y/N) ";
            cin >> current;

            if (std::toupper(current) == 'Y')  // <--- Or you could use "tolower".
                total += 3.50;
            else
                total += 2.50;
        }
    } while (dvdCount++ < numDVDs);

    // Display the total
    cout << fixed << showpoint << setprecision(2);  // <--- If "setprecision" is > zero "showpoint" is optional as you will always be printing 2 decimal places.
    cout << "The total is $" << total << endl;

    return 0;
}

Did this before I had a chance to test it.

With the ""else" statement 1 or the other will happen, but not both.
Andy
break and continue are tools that you WILL need on occasion; that is why they exist. There is no reason to change what you have if it works properly, but in your case, as noted, an else does the trick. (you can always avoid break/continue if you are willing to make complicated code to go around the issue, of course).

I personally prefer that even for 1 line if/else/loop/etc statements you go ahead and put in {} and indent them. Its visual bloat which I normally complain about, but it makes all your blocks consistent looking, which I prefer. Its a choice, though. If you ever edit to add a line and forget to add the {} you will see another reason to do this.
Last edited on
Topic archived. No new replies allowed.