mercredi 24 juin 2020

Unexpected break during an if statement C++

The program is to convert yen, euro or pounds (depending on user input) to a dollar.
I am asking 2 user inputs - double amount (money to convert) and char currency (to determine what currency to convert to dollar).
Sample Input: 1y
Sample Output: 1 yen(s) = 0.0094 dollar(s).
The problem is in the if-else block when I am trying to convert euros to dollars, it breaks the while loop. Here, is my code:

double amount; // the amount of money to be converted
char currency; // to determine the currency in which the money is being entered in.
    while (cin >> amount >> currency) {
        if (currency == 'y' || currency == 'Y') {
            cout << amount << " yen(s) = " << (amount * 0.0094) << " dollar(s).\n";
        }
        else if (currency == 'e' || currency == 'E') {
            cout << amount << " euro(s) = " << (amount * 1.13) << " dollar(s).\n";
        }
        else if (currency == 'p' || currency == 'P') {
            cout << amount << " pound(s) = " << (amount * 1.25) << " dollar(s).\n";
        }
        else {
            cout << "Sorry I did not recognize the currency! Please enter 'y','e' or 'p'.\n";
        }
        cout << "Please enter the amount of money and corresponding currency to covert to  dollars: ";
    }

As evidence here are the images of my input and output:

Conversion of euros to dollar error one

Conversion of euros to dollar error one

Conversion of euros to dollar error two

Conversion of euros to dollar error two

Aucun commentaire:

Enregistrer un commentaire