Thread: Unit converter calculator help

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    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.

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    17
    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

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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;
    	}
    Doesn't look terrible to me.
    And btw, why use TRUE/FALSE when we can use true/false?
    Welcome to C++, Yarin

    Quote Originally Posted by ravens199 View Post
    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
    I think you have the answer to your question, and a little help on your way too
    Last edited by Elysia; 01-13-2008 at 05:31 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Jan 2008
    Posts
    17
    Muchos Grassy-ass

  6. #21
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    switch statements are SOP for this kind of question. I think the teacher likely had in mind a switch rather than an if-else.

  7. #22
    Registered User
    Join Date
    Jan 2008
    Posts
    17
    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

  8. #23
    Registered User
    Join Date
    Jan 2008
    Posts
    17

    OK, need help looping this switch statement

    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;
    }

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The new question in this thread is now at Need help looping this switch statement.
    Last edited by laserlight; 01-13-2008 at 09:32 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating C/C++ Unit Test Cases
    By chiefmonkey in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2009, 08:29 PM
  2. newbie here.. pls help me in this program!
    By rothj0hn in forum C Programming
    Replies: 2
    Last Post: 02-01-2006, 10:40 PM
  3. 2-d object avoidance. Help please! (basic stuff, I think)
    By charbach007 in forum Game Programming
    Replies: 4
    Last Post: 06-15-2004, 03:49 PM
  4. Unit Actions
    By DavidP in forum Game Programming
    Replies: 20
    Last Post: 05-28-2004, 09:18 PM
  5. Unit Theory - input requested
    By Jeremy G in forum Game Programming
    Replies: 9
    Last Post: 11-18-2003, 10:54 AM