Informatics - Currency Change Calc Version 2 (Week 17)

 Hello! How was your holiday? Have you gone to the country that you want? I'm back with Currency Change Calc Version 2! 

Don't worry! I only add some addition here but not changing the main code. Here, I only add the do-while loop. A do-while loop is a type of loop that always runs at least once, regardless of the condition. It first executes the code inside the loop, then checks if the condition is true. If it is, the loop repeats; if not, it stops. This is useful in situations where you need to ensure that something happens at least once, such as prompting a user for input until they enter a valid value.

Here's the code after some adjustment!

//program lengthConverter.cpp

#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;

double rupiah, won;
void rupiah2won(){
    do {
    cout << "Enter money in rupiah:";
    cin >> rupiah;
    if (rupiah < 0) {
        cout << "ERROR" << endl;
        break;
    }
    won = rupiah*0.089;
    cout << fixed << setprecision(4) << rupiah << " rupiah is equal to " << won << " won." << endl;
} while (rupiah < 0);
}

void won2rupiah(){
    do {
    cout << "Enter money in won:";
    cin >> won;
    if (won < 0){
        cout << "ERROR" << endl;
        break;
    }
    rupiah = won*11.26;
    cout << fixed << setprecision(4) << won << " won is equal to " << rupiah << " rupiah." << endl;
    }while (won < 0);
}

int main () {
int choice;
cout << "Choose procedure type: \n";
cout << "1. Convert rupiah to won \n";
cout << "2. Convert won to rupiah \n";
cout << "Choose between 1 or 2! \n";
cout << "Enter your choice: my choice is ";
cin >> choice;

switch (choice) {
        case 1:
            rupiah2won();
            break;
        case 2:
            won2rupiah();
            break;
        default:
            cout << "!!!YOU PUT THE WRONG INPUT!!!" << endl;
            cout << "-------!!!ERROR!!!--------" << endl;
    }
}

In this code, the do-while loop is used inside the rupiah2won() and won2rupiah() functions to handle currency conversion while ensuring valid input. The loop starts by asking the user to enter an amount. If the user enters a negative number, an error message is displayed, and the loop breaks, preventing further calculations. If the input is valid, the program converts the currency and displays the result. Although the condition (while (rupiah < 0) or while (won < 0)) suggests the loop should continue for negative numbers, the break statement stops it immediately, making the loop effectively run only once. This structure helps catch incorrect input while ensuring the conversion happens at least once before exiting.

As seen in the picture above, I input -80 Rupiah, and the program stop running. And that's how do-while loop work! Easy right. Anyway, good luck!







Komentar

Postingan populer dari blog ini

Informatics - Making a Main C++ Program to Run all the Calculator Program (week 11)

Informatics - Triangles (week 18)

Informatics - Making Minimarket Discount Program in C++ version 1 (Week 12)