Friday, November 23, 2012

CPP 08 - Do While Loop

Previous | C++ Home | Next


There is another way we can use to execute a part of code number of time. Look at the following syntax:

do {
    run_1st_statement;
    run_2nd_statement;
    etc. 
} while (expression);

The difference between this code and 'while' loop presented in the previous post is that this one will run the statements in the braces '{}' at least once. After they have been executed, the program checks the ' while (expression)'. If it's true (non-zero value), the program jumps back to the keyword 'do' and executes the code within the braces (1st and 2nd statement in my case). Then the ' while (expression)' is checked for true (non-zero value) or false (value of zero) again. If false, program continues and executes the statements that follow the 'do while' loop.

Let's use of this type of loop in our new program. It will be designed to convert USD and EUR currencies (homework in post CPP 06). Here are our assumptions:

  1. Program must present the user with a simple menu offering conversions of USD-to-EUR and EUR-to-USD. User must be able to pick one or the other.
  2. Use the currency rate: EUR 1 = USD 1.28921, and conversely USD 1 = EUR 0.775512
  3. Program should display the result and ask the user if he/she wants to do another conversion. If not, the program should terminate.
Here's my code (can be downloaded here):

Source Code 8.1

/* Program Converting Currencies 
version: 1.0
name: ex008.cpp
compilation: g++ -o ex008 ex008.cpp
author: Jarek Rek
*/

#include <iostream>

using namespace std;

int main () {

// Variables
float eur_is_usd = 1.28921;
float usd_is_eur = 0.775512;
float users_number;
float result;
int choice;
char users_answer;

/* ************ Here the Code Begins ************ */

do {
// Banner
cout << endl << endl;
cout << "      *******************************************\n";
cout << "             Program Converting Currencies       \n"; 
cout << "      *******************************************\n";
cout << endl;

// User's interface
cout << "\tYour options:\n";
cout << "\t---------------\n";
cout << "\t[1]\tConvert USD to EUR\n";
cout << "\t[2]\tConvert EUR to USD\n";
cout << "\nPlease choose the option [1]|[2]: ";

// Accept user's option
cin >> choice;

// Run code according to users choice
switch (choice)
{
case 1: 
cout << "\nNumber of USD to convert: ";
cin >> users_number;
result = users_number * usd_is_eur;
cout << "USD " << users_number << " is EUR "
<< result << endl;
break;
case 2:
cout << "\nNumber of EUR to convert: ";
cin >> users_number;
result = users_number * eur_is_usd;
cout << "EUR " << users_number << " is USD "
<< result << endl;
break;
default:
cout << "\nYour choice is not listed.\n";
}
cout << "Would you like to try another conversion [y|n]?";
cin >> users_answer;
} while (users_answer == 'y' || users_answer == 'Y');

cout << "\n\nThank you for using Currency Converter. See you next time!\n\n";
return 0;
}

Explanation
In the variables, I have created new data type called 'char'. It wil remember a single character you type on your keyboard. We will use it in our final 'while' statement. Now, all the code is enclosed in the 'do while' loop. Depending on the users' last answer 'y' or 'n', the code will either display menu all over again asking user for his/her choice, or it will terminate displaying the last thanks.

In the while statement I have used logical 'or' statement looking like to vertical lines '||' (one of lines like this is called 'pipe'). Take a look at this:
while (users_answer == 'y' || users_answer == 'Y');

The program interprets this as: if your answer is 'y' OR your answer is 'Y' (assumes either upper or lower case of your character). More on those later.

Homework
Re-write your previous program converting temperatures to ask the user if they want to do another conversion.