Come on, this is perfect for a switch. Everyone recommended a switch too.
Multiple ifs is only needed for multiple conditions or if you are trying to do a switch on which the type is a string (or with a cases that aren't constant values).
Printable View
Come on, this is perfect for a switch. Everyone recommended a switch too.
Multiple ifs is only needed for multiple conditions or if you are trying to do a switch on which the type is a string (or with a cases that aren't constant values).
I'm just thinking how long it'll take to code. I figured it'd be easier to link to functions through ifs rather than do the equasions in a big main() switch.
By skeleton I mean saying "Use a switch statement to direct the user's input choice to calculate the value of the conversion" or "I would set up a function table, although it is somewhat more complicated, so If-then statements would work as a back-up". I need to know how to do this myself, but i totally appreciate all of the help. Thanks again everyone
Doesn't look terrible to me.Code:bool bAbort = false;
switch (option)
{
case 1: Length(true); break;
case 2: Weight(true); break;
case 3: Length(false); break;
case 4: Weight(false); break;
default: bAbort = true; break;
}
And btw, why use TRUE/FALSE when we can use true/false?
Welcome to C++, Yarin :p
I think you have the answer to your question, and a little help on your way too ;)
Muchos Grassy-ass ;)
switch statements are SOP for this kind of question. I think the teacher likely had in mind a switch rather than an if-else.
I think a switch seems the best, because if I add 5 more options, i can just add 5 more cases to the switch and not have to worry about changing all of the if statements
OK, so I wrote the program the way Elysia suggested, and it works great. Now, after the number is converted, i want to ask the user if they want to convert another value. If they type 'y' or 'Y', it prompts them again. 'n' or 'N' ends the program. Any suggestions are welcome, thanks. Here is my code:
Code:#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double num1, meters, feet, kilos, pounds, liters, gallons, kms, miles;
int option;
cout << "Please select the conversion type you want to perform:" << endl;
cout << "1. Convert feet to meters" << endl;
cout << "2. Convert meters to feet" << endl;
cout << "3. Convert lbs. to kilograms" << endl;
cout << "4. Convert kilograms to lbs." << endl;
cout << "5. Convert gallons to liters" << endl;
cout << "6. Convert liters to gallons" << endl;
cout << "7. Convert square miles to square kilometers" << endl;
cout << "8. Convert square kilometers to square miles" << endl << endl;
cin >> option;
switch(option)
{
case 1:
cout << endl << "Enter the number of feet to convert to meters:" << endl;
cin >> num1;
meters = num1 * 0.3048;
cout << num1 << " feet is equal to " << meters << " meters." << endl;
break;
case 2:
cout << endl << "Enter the number of meters to convert to feet:" << endl;
cin >> num1;
feet = num1 * 3.2808399;
cout << num1 << " meters is equal to " << feet << " feet." << endl;
break;
case 3:
cout << endl << "Enter the number of pounds to convert to kilograms:" << endl;
cin >> num1;
kilos = num1 * 0.45359237;
cout << num1 << " lbs. is equal to " << kilos << " kilograms." << endl;
break;
case 4:
cout << endl << "Enter the number of kilograms to convert to pounds:" << endl;
cin >> num1;
pounds = num1 * 2.20462262;
cout << num1 << " kilograms is equal to " << pounds << " lbs." << endl;
break;
case 5:
cout << endl << "Enter the number of gallons to convert to liters:" << endl;
cin >> num1;
liters = num1 * 3.7854118;
cout << num1 << " gallons is equal to " << liters << " liters." << endl;
break;
case 6:
cout << endl << "Enter the number of liters to convert to gallons:" << endl;
cin >> num1;
gallons = num1 * 0.264172051;
cout << num1 << " liters is equal to " << gallons << " gallons." << endl;
break;
case 7:
cout << endl << "Enter the number of square miles to convert into square kilometers:" << endl;
cin >> num1;
kms = num1 * 2.58998811;
cout << num1 << " square miles is equal to " << kms << " square kilometers." << endl;
break;
case 8:
cout << endl << "Enter the number of square kilometers to convert into square miles:" << endl;
cin >> num1;
miles = num1 * 0.386102159;
cout << num1 << " square kilometers is equal to " << miles << " square miles." << endl;
break;
default:
break;
}
return 0;
}
The new question in this thread is now at Need help looping this switch statement.