![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
Unit Convertor
over lst couple of days ive been working on a Unit Convertor. It converts Distances, Temperature, Volume and Wieght. I know it could be shorter by using my own functions thanks to coldDeath. #include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int selection, selection2, selection3, selection4, selection5, loop, loop2, loop3, loop4, loop5;
float c, f, k, temp, meas, meas2, meas3, dis, dis2, dis3;
loop = 1;
while (loop < 2){
cout << "~~~Welcome to the unit convertor~~~\n\n";
cout << "1. Temperature Convertor\n2. Metric and Imperial(Length)\n3. Metric and Imperial(Wieght)\n4. Metric and Imperial(Liquid Units)\n5. Exit\n\n";
cout << "Selcect an option(1,2,3,4,5): ";
cin >> selection;
if (selection == 1, 2, 3, 4, 5)
{
switch ( selection ) {
case 1:
system("cls");
loop2 = 1;
while (loop2 < 2){
cout << "~~~Welcome to the temperature convertor~~~\n\n";
cout << "1.Celsius\n2.Fahrenheit\n3.Kelvin\n4.Exit\n";
cout << "\nSelect starting measurment(1,2,3,4): ";
cin >> selection2;
if ( selection2 == 1, 2, 3, 4)
{
switch ( selection2 ) {
case 1:
cout << "\nEnter Temperature in Celsius: ";
cin >> temp;
f = temp * 1.8 + 32;
k = temp + 273.15;
cout << "\n" << temp << " Celsius In Fahrenheit is: " << f << "\n";
cout << "\n" << temp << " Celsius In Kelvin is: " << k << "\n\n";
cout << "Press Enter to return to the menu";
cin.clear();
cin.sync();
cin.get();
break;
case 2:
cout << "\nEnter Temperature in Fahrenheit: ";
cin >> temp;
c = (temp - 32) / 1.8;
k = (temp + 459.67) / 1.8;
cout << "\n" << temp << " Fahrenheit In Celsius is: " << c << "\n";
cout << "\n" << temp << " Fahrenheit In Kelvin is: " << k << "\n\n";
cout << "Press Enter to return to the menu";
cin.clear();
cin.sync();
cin.get();
break;
case 3:
cout << "\nEnter Temperature in Kelvin: ";
cin >> temp;
c = temp - 273.15;
f = temp * 1.8 - 459.67;
cout << "\n" << temp << " Kelvin In Celsius is: " << c << "\n";
cout << "\n" << temp << " Kelvin In Fahrenheit is: " << f << "\n\n";
cout << "Press Enter to return to the menu";
cin.clear();
cin.sync();
cin.get();
break;
case 4:
cout << "\nThank you for using the convertor!";
cin.clear();
cin.sync();
cin.get();
system("cls");
loop2 = 2;
break;
default:
cout << "Error, Closing";
cin.clear();
cin.sync();
cin.get();
return 0;
break;
}
}
else {
cout << "Error, Closing";
cin.get();
return 0;
}
system("cls");
}
break;
case 2:
system("cls");
loop3 = 1;
while (loop3 < 2){
cout << "~~~Welcome to the measurement convertor~~~\n\n";
cout << "1.Metric(Metre's)\n2.Imperial(Feet)\n3.Exit\n";
cout << "\nSelect starting measurment(1,2,3): ";
cin >> selection3;
if ( selection3 == 1, 2, 3)
{
switch ( selection3 ) {
case 1:
cout << "\nEnter Measurement in Metre's(eg. 5.32): ";
cin >> meas;
dis = meas * 3.281;
cout << "\n" << meas << " Metre's In Feet is: " << dis << "\n\n";
cout << "Press Enter to return to the menu";
cin.clear();
cin.sync();
cin.get();
break;
case 2:
cout << "\nEnter Measurement in Feet(eg. 5.32): ";
cin >> meas;
dis = meas * 0.3048;
cout << "\n" << meas << " Feet In Metre's is: " << dis << "\n\n";
cout << "Press Enter to return to the menu";
cin.clear();
cin.sync();
cin.get();
break;
case 3:
cout << "\nThank you for using the convertor!";
cin.clear();
cin.sync();
cin.get();
system("cls");
loop3 = 2;
break;
default:
cout << "Error, Closing";
cin.clear();
cin.sync();
cin.get();
return 0;
break;
}
}
else {
cout << "Error, Closing";
cin.get();
return 0;
}
system("cls");
}
break;
case 3:
system("cls");
loop4 = 1;
while (loop4 < 2){
cout << "~~~Welcome to the measurement convertor~~~\n\n";
cout << "1.Metric(Kilogram's)\n2.Imperial(Pounds)\n3.Exit\n";
cout << "\nSelect starting measurment(1,2,3): ";
cin >> selection4;
if ( selection4 == 1, 2, 3)
{
switch ( selection4 ) {
case 1:
cout << "\nEnter Measurement in Kilogram's(eg. 5.32): ";
cin >> meas2;
dis2 = meas2 * 2.205;
cout << "\n" << meas2 << " Kilogram's In Pounds is: " << dis2 << "\n\n";
cout << "Press Enter to return to the menu";
cin.clear();
cin.sync();
cin.get();
break;
case 2:
cout << "\nEnter Measurement in Pounds(eg. 5.32): ";
cin >> meas2;
dis2 = meas2 * 0.45359;
cout << "\n" << meas2 << " Pounds In Kilogram's is: " << dis2 << "\n\n";
cout << "Press Enter to return to the menu";
cin.clear();
cin.sync();
cin.get();
break;
case 3:
cout << "\nThank you for using the convertor!";
cin.clear();
cin.sync();
cin.get();
system("cls");
loop4 = 2;
break;
default:
cout << "Error, Closing";
cin.clear();
cin.sync();
cin.get();
return 0;
break;
}
}
else {
cout << "Error, Closing";
cin.get();
return 0;
}
system("cls");
}
break;
case 4:
system("cls");
loop5 = 1;
while (loop5 < 2){
cout << "~~~Welcome to the measurement convertor~~~\n\n";
cout << "1.Metric(Litre's)\n2.Imperial(Pints)\n3.Exit\n";
cout << "\nSelect starting measurment(1,2,3): ";
cin >> selection5;
if ( selection5 == 1, 2, 3)
{
switch ( selection5 ) {
case 1:
cout << "\nEnter Measurement in Litre's(eg. 5.32): ";
cin >> meas3;
dis3 = meas3 / 0.568;
cout << "\n" << meas3 << " Litre's In Pints is: " << dis3 << "\n\n";
cout << "Press Enter to return to the menu";
cin.clear();
cin.sync();
cin.get();
break;
case 2:
cout << "\nEnter Measurement in Pints(eg. 5.32): ";
cin >> meas3;
dis3 = meas3 * 0.568;
cout << "\n" << meas3 << " Pints In Litre's is: " << dis3 << "\n\n";
cout << "Press Enter to return to the menu";
cin.clear();
cin.sync();
cin.get();
break;
case 3:
cout << "\nThank you for using the convertor!";
cin.clear();
cin.sync();
cin.get();
system("cls");
loop4 = 2;
break;
default:
cout << "Error, Closing";
cin.clear();
cin.sync();
cin.get();
return 0;
break;
}
}
else {
cout << "Error, Closing";
cin.get();
return 0;
}
system("cls");
}
break;
case 5:
cout << "\nThank You for using the unit convertor.";
cin.clear();
cin.sync();
cin.get();
return 0;
break;
}
}
}
return 0;
}Tabbing looks mad on here. I didnt actual make the tabs that big |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
First thing I noticed is how freakin' big your tabs are... in my opinion, it's very annoying
![]() |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
Egads, big tabs. I hope you copied and pasted to produce most of that code; it's very repetitive. Exactly why you should use functions.
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
Yea i did copy and paste, and for some reason the tabs seem much bigger when i copied the code into here
|
|
|
|
|
|
#5 |
|
Expert Programmer
|
Yeah, they have got bigger than the one he sent me.
Try looking in your text editors options for something like "tab size" or "swap tabs for spaces"
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#6 |
|
Programmer
|
What about.. file size units? I only used bytes, kilobytes, and megabytes, to ease the work on myself, but it could go up to yottabytes or down to bits.
![]() print "File size conversation tool\nType 'help' for help."
prompt = 'Enter a conversion such as "3 mb to kb" or "8 b to kb": '
def start():
line= raw_input(prompt).upper()
if 'HELP' in line:
print "Valid file size units:\nby (byte)\nkb (kilobyte)\nmb (megabyte)
print "\nExample conversions:\n3 mb to b\n3 kb to mb"
start()
t, fromto = None, []
for token in line.split(' '):
try:
t = float(token)
except ValueError:
pass
if token[:1] in 'MBKB':
fromto.append(token[0])
if t is None or len(fromto) != 2:
print 'Unsuccessful parse. Try again, using the format: 15 kb to b'
start()Yeah.. I'll let you finish that, because the last stretch of code has taken up enough of my time. x.x |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
What language is that Riddle? Pascal?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I would suggest setting up your editor so that it converts tabs to spaces -- far fewer spaces.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
Quote:
![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
|
#10 | |
|
Programmer
|
Quote:
<_< >_> My bad. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|