I am trying to use a series of nested if/then/else statements in C++ to interpret a user's input. I'm getting the user's input just fine, but I'm not able to convert anything except the first loop (the one the furthest to the outside). How can I change this code so that if the first loop's condition is not fulfilled, then it moves down to the next loop, and so on and so forth until the last loop? Note: I don't want to use any other include directives if I don't have to. Thanks in advance.
#include <iostream>
#include <cmath>
#include <unistd.h>
#include <fstream>
... (some functions go here) ...
int main() {
int menuselection, inputNo;
cout << "Please select your preferred conversion, or 0 to quit." << endl;
cout << "1 for feet to meters (length)" << endl;
cout << "2 for meters to feet (length)" << endl;
cout << "3 for degrees Centigrade to degrees Fahrenheit" << endl;
cout << "4 for degrees Fahrenheit to degrees Centigrade" << endl;
cout << "5 for cubic meters to cubic feet (volume)" << endl;
cout << "6 for cubic feet to cubic meters (volume)" << endl;
cin >> menuselection;
...
if (menuselection=1) {
cout << inputNo << " feet (foot) converted to meters is: ";
footToMeter(inputNo);
cout << "\n";
} else if (menuselection=2) {
cout << inputNo << " meter(s) converted to feet is: ";
meterToFoot(inputNo);
cout << "\n";
} else if (menuselection=3) {
cout << inputNo << " degree(s) Centigrade converted to degree(s) Fahrenheit is: ";
CtoF(inputNo);
cout << "\n";
} else if (menuselection=4) {
cout << inputNo << "degree(s) Fahrenheit converted to degree(s) Centigrade is: ";
FtoC(inputNo);
cout << "\n";
} else if (menuselection=5) {
cout << inputNo << " cubic meter(s) converted to cubic feet (foot) is: ";
cMtocF(inputNo);
cout << "\n";
} else if (menuselection=6) {
cout << inputNo << " cubic feet (foot) converted to cubic meter(s) is: ";
cFtocM(inputNo);
cout << "\n";
} else if (menuselection=0) {
return 0;
}
}
Aucun commentaire:
Enregistrer un commentaire