Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 16th, 2005, 2:17 PM   #1
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
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
HaCkeR is offline   Reply With Quote
Old Dec 16th, 2005, 2:21 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
First thing I noticed is how freakin' big your tabs are... in my opinion, it's very annoying
Polyphemus_ is offline   Reply With Quote
Old Dec 16th, 2005, 2:23 PM   #3
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
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.
Klipt is offline   Reply With Quote
Old Dec 16th, 2005, 2:24 PM   #4
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
Yea i did copy and paste, and for some reason the tabs seem much bigger when i copied the code into here
HaCkeR is offline   Reply With Quote
Old Dec 17th, 2005, 2:07 AM   #5
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Dec 17th, 2005, 10:31 AM   #6
Riddle
Programmer
 
Riddle's Avatar
 
Join Date: May 2005
Location: Nar Shaddaa
Posts: 42
Rep Power: 0 Riddle is on a distinguished road
Send a message via ICQ to Riddle Send a message via AIM to Riddle Send a message via MSN to Riddle
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
Riddle is offline   Reply With Quote
Old Dec 17th, 2005, 11:05 AM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Dec 17th, 2005, 11:07 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Dec 17th, 2005, 11:37 AM   #9
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by nnxion
What language is that Riddle? Pascal?
It's python. I guess he didn't realise this was the C++ forum.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Dec 17th, 2005, 12:45 PM   #10
Riddle
Programmer
 
Riddle's Avatar
 
Join Date: May 2005
Location: Nar Shaddaa
Posts: 42
Rep Power: 0 Riddle is on a distinguished road
Send a message via ICQ to Riddle Send a message via AIM to Riddle Send a message via MSN to Riddle
Quote:
Originally Posted by Jessehk
It's python. I guess he didn't realise this was the C++ forum.
:O Had too many tabs open at once, coulda sworn this was in the Python forum.. in fact, I thought this was a completely different thread.

<_<
>_>

My bad.
Riddle is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:23 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC