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.
>Solution :
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;
}
}