// This program calculates the charges for DVD rentals.
// Every third DVD is free.
#include <iostream>
#include <iomanip>
usingnamespace 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;
}
// This program calculates the charges for DVD rentals.
// Every third DVD is free.
#include <iostream>
#include <iomanip>
usingnamespace 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;
}
#include <cctype> // <--- Added.
#include <iostream>
#include <iomanip>
usingnamespace 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;
}
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.