Home > Mobile >  Basic Currency Converter
Basic Currency Converter

Time:04-11

I am a beginner coder and I have a problem with my code. Every time I run it, near the end it always goes to the else statement. I tried including break statements to see if that would help, but that doesn't seem to work. I also tried just making all of them if statements instead of using else if. This code was written in C . Here is my code:

#include <iostream>
using namespace std;

class A
{

public:
    int num;

    void number()
    {
        cout << "1 for British Pound " << endl;
        cout << "2 for Mexican Pesos " << endl;
        cout << "3 for Japanese Yen " << endl;
        cout << "4 for Chinese Yen " << endl;
        cout << "5 for Australian Dollar "
             << "\n\n";
        cout << "Enter number here: ";
        cin >> num;
    }
};

class B : public A
{
private:
    float money;
    float res;

public:
    void ifs()
    {
        number();

        if (num == 1)
        {
            cout << "Enter how much US money you have:  ";
            cin >> money;
            res = money * 0.77; // British pound is 0.77 for every US dollar.
            cout << money << "$ in US is " << res << " in British pounds";
        }

        if (num == 2)
        {
            cout << "Enter how much US money you have:  ";
            cin >> money;
            res = money * 20.08; // Mexican Pesos is 20.08 for every US dollar.
            cout << money << "$ in US is " << res << " in Mexican Pesos. ";
        }
    }
};

class C : public B
{
private:
    float money, res;

public:
    void ifs2()
    {
        ifs();
        if (num == 3)
        {
            cout << "Enter how much US money you have:  ";
            cin >> money;
            res = money * 125.31; // Japansese Yen is 125.31 for every US dollar.
            cout << money << "$ in US is " << res << " in Japanese Yen. ";
        }

        if (num == 4)
        {
            cout << "Enter how much US money you have:  ";
            cin >> money;
            res = money * 6.37; // Chinese Yen is 6.37 for every US dollar.
            cout << money << "$ in US is " << res << " in Chinese Yen. ";
        }

        if (num == 5)
        {
            cout << "Enter how much US money you have: ";
            cin >> money;
            res = money * 1.35; // Australian Dollar is 1.35 for every US dollar.
            cout << money << "$ in US is " << res << " in Australian Dollar. ";
        }

        else
        {
            cout << "Please enter a number that is listed above! ";
        };
    }
};

int main()
{
    C obj;
    obj.ifs2();
}

// This code is a US currency converter.

// It contains class and methods.

CodePudding user response:

Looking at your code, you're always re-evaluating your num input.

If you want to use if-statements, consider using if/else constructs:

void MyClass::foo() {
    if (num == 1) {

    } else if (num == 2) {

    } /*...*/ {

    } else {
        // handle other cases here
    }
}

The more suitable approach, however, is to use a switch/case construct here. It's similar to if/else in the way it works, but offers cleaner code and more performance.

void MyClass::foo() {
    switch (num) {
        case 1: // handle first case
            break;
        case 2: // handle second case
            break;
        // add more cases as needed
        default: // this would be your "else" block
             break;
    }
}

CodePudding user response:

To run a control statement between multiple conditions, you have to use else if. When using else if, it will sequentially evaluate the if conditions. Currently, the else only works for the last if (num == 5).

The correct answer would be:

public:
    void ifs2()
    {
        ifs();
        if (num == 3)
        {
            cout << "Enter how much US money you have:  ";
            cin >> money;
            res = money * 125.31; // Japansese Yen is 125.31 for every US dollar.
            cout << money << "$ in US is " << res << " in Japanese Yen. ";
        }

        else if (num == 4)
        {
            cout << "Enter how much US money you have:  ";
            cin >> money;
            res = money * 6.37; // Chinese Yen is 6.37 for every US dollar.
            cout << money << "$ in US is " << res << " in Chinese Yen. ";
        }

        else if (num == 5)
        {
            cout << "Enter how much US money you have: ";
            cin >> money;
            res = money * 1.35; // Australian Dollar is 1.35 for every US dollar.
            cout << money << "$ in US is " << res << " in Australian Dollar. ";
        }

        else
        {
            cout << "Please enter a number that is listed above! ";
        };
    }
};

int main()
{
    C obj;
    obj.ifs2();
}

CodePudding user response:

    if (num == 5)
    {
        cout << "Enter how much US money you have: ";
        cin >> money;
        res = money * 1.35; // Australian Dollar is 1.35 for every US dollar.
        cout << money << "$ in US is " << res << " in Australian Dollar. ";
    }

    else
    {
        cout << "Please enter a number that is listed above! ";
    };

This else branch is connected to if statement "num == 5". Compiler reads code line by line. It reads first if then second then third. If first if statement is true or not still it goes to next statement. Meanwhile if you enter number other than 5 then it will go to this else statement. If you test this program with number 5 it will work fine. Otherwise it will show this else message. Use if else if instead or switch case to remove ambiguity or re evaluation.

  •  Tags:  
  • c
  • Related