CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2012
    Posts
    1

    Do {} while errors

    Hey all,
    Started learning C++ a few days ago, and just finished my first half-useful program. It's basically a converter for weight, distance and currency.
    My question is, is there any better way to do this? Any general tips?
    And for some reason, when I did the do { code } while, I got an error code about the compiler not recognising the system and return commands. Then, suddenly, it worked. Why?

    The code:

    Code:
    #include <iostream>
    double pounds, kilograms, feet, metres, centimetres, inches, kilometres, miles, yards, EUR, USD, GBP, CNY;
    using namespace std;
     
    int choice;
    
    int main()
    
    {
    	cout << "Weight: \n" << endl;
    	cout << "Pounds      --> Kilograms:   type 1. " << endl;
    	cout << "Kilograms   --> Pounds:      type 2. " << endl;
    	cout << "\nDistance:\n" << endl;
    	cout << "Feet        --> Metres:      type 3. " << endl;
    	cout << "Metres      --> Feet:        type 4. " << endl;
    	cout << "Inches      --> Centimetres: type 5. " << endl;
    	cout << "Centimetres --> Inches:      type 6. " << endl;
    	cout << "Miles       --> Kilometres:  type 7." << endl;
    	cout << "Kilometres  --> Miles:       type 8." << endl;
    	cout << "Yards       --> Metres:      type 9." << endl;
    	cout << "Metres      --> Yards:       type 10." << endl;
    	cout << "\nCurrency:\n" << endl;
    	cout << "EUR --> USD: type 11." << endl;
    	cout << "USD --> EUR: type 12." << endl;
    	cout << "GBP --> USD: type 13." << endl;
    	cout << "USD --> GBP: type 14." << endl;
    	cout << "EUR --> GBP: type 15." << endl;
    	cout << "GBP --> EUR: type 16." << endl;
    	cout << "CNY --> EUR: type 17." << endl;
    	cout << "EUR --> CNY: type 18." << endl;
    	cout << "CNY --> USD: type 19." << endl;
    	cout << "USD --> CNY: type 20.\n" << endl;
    
    	do
    	{
    	
    	cin >> choice;
    
        switch (choice)
    	{
    	case 1:
    		cout << "\nEnter the number of pounds: ";
    		cin >> pounds;
    		cout << "\n" << pounds << " lbs is " << pounds * 0.45 << " kg." << endl;
    		break;
    	case 2:
    		cout << "\nEnter the number of kilos: ";
    		cin >> kilograms;
    		cout << "\n" << kilograms << " kg is " << kilograms * 2.2 << " pounds." << endl;
    		break;
    	case 3:
    		cout << "\nEnter the number of feet: ";
    		cin >> feet;
    		cout << "\n" << feet << " ft is " << feet * 0.3 << " m." << endl;
    		break;
    	case 4:
    		cout << "\nEnter the number of meters: ";
    		cin >> metres;
    		cout << "\n" << metres << " m is " << metres * 3.2 << " ft." << endl;
    		break;
    	case 5:
    		cout << "\nEnter the number of inches: ";
    		cin >> inches;
    		cout << "\n" << inches << " inches is " << inches * 2.55 << " ft." << endl;
    		break;
    	case 6:
    		cout << "\nEnter the number of centimetres: ";
    		cin >> centimetres;
    		cout << "\n" << centimetres << " cm is " << centimetres * 0.4 << " inches." << endl;
    		break;
    	case 7:
    		cout << "\nEnter the number of miles: ";
    		cin >> miles;
    		cout << "\n" << miles << " miles is " << miles * 1.6 << " km." << endl;
    		break;
    	case 8: 
    		cout << "\nEnter the number of kilometers: ";
    		cin >> kilometres;
    		cout << "\n" << kilometres << " km is " << kilometres * 0.62 << " miles." << endl;
    		break;
    	case 9: 
    		cout << "\nEnter the number of yards: ";
    		cin >> yards;
    		cout << "\n" << yards << " yards is " << yards * 0.91 << " meters." << endl;
    		break;
    	case 10:
    		cout << "\nEnter the number of meters: ";
    		cin >> metres;
    		cout << "\n" << metres << " m is " << metres * 1.1 << " yards." << endl;
    		break;
    	case 11: 
    		cout << "\nEnter the amount of EUR: ";
    		cin >> EUR;
    		cout << "\n" << EUR << " EUR is " << EUR * 1.26 << "USD. " << endl;
    		break;
    	case 12:
    		cout << "\nEnter the amount of USD: ";
    		cin >> USD;
    		cout << "\n" << USD << " USD is " << USD * 0.79 << " EUR." << endl;
    		break;
    	case 13:
    		cout << "\nEnter the amount of GBP: ";
    		cin >> GBP;
    		cout << "\n" << GBP << " GBP is " << GBP * 1.59 << " USD." << endl;
    		break;
    	case 14:
    		cout << "\nEnter the amount of USD: ";
    		cin >> USD;
    		cout << "\n" << USD << " USD is " << USD * 0.63 << " GBP." << endl;
    		break;
    	case 15:
    		cout << "\nEnter the amount of EUR: ";
    		cin >> EUR;
    		cout << "\n" << EUR << " EUR is " << EUR * 0.79 << " GBP." << endl;
    		break;
    	case 16:
    		cout << "\nEnter the amount of GBP: ";
    		cin >> GBP;
    		cout << "\n" << GBP << " GBP is " << GBP * 1.26 << " EUR." << endl;
    		break;
    	case 17:
    		cout << "\nEnter the amount of CNY: ";
    		cin >> CNY;
    		cout << "\n" << CNY << " CNY is " << CNY * 0.125 << " EUR." << endl;
    		break;
    	case 18:
    		cout << "\nEnter the amount of EUR: ";
    		cin >> EUR;
    		cout << "\n" << EUR << " EUR is " << EUR * 7.99 << " CNY." << endl;
    		break;
    	case 19:
    		cout << "\nEnter the amount of CNY: ";
    		cin >> CNY;
    		cout << "\n" << CNY << " CNY is " << CNY * 0.16 << " USD." << endl;
    		break;
    	case 20:
    		cout << "\nEnter the amount of USD: ";
    		cin >> USD;
    		cout << "\n" << USD << " USD is " << USD * 6.35 << " CNY." << endl;
    		break;
    	default: 
    		cout << "\nPlease enter a valid choice.\n " << endl;
    		}
    		}
    		while ( choice !=1 && choice !=2 && choice !=3 && choice !=4 && choice !=5 && choice !=6 && choice !=7 && choice !=8 && choice !=9 && choice !=10 && choice !=11 && choice !=12
    			    && choice !=13 && choice !=14 && choice !=15 && choice !=16 && choice !=17 && choice !=18 && choice !=19 && choice !=20);
    			
    	system("pause");
    	return 0;
    	}

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Do {} while errors

    Code:
    while ( choice !=1 && choice !=2 && choice !=3 && choice !=4 && choice !=5 && choice !=6 && choice !=7 && choice !=8 && choice !=9 && choice !=10 && choice !=11 && choice !=12 && choice !=13 && choice !=14 && choice !=15 && choice !=16 && choice !=17 && choice !=18 && choice !=19 && choice !=20);
    while (choice < 1 && choice > 20)

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Do {} while errors

    I'd do the whole thing with arrays and get rid of the big case statement. Set up arrays that store the input and output unit names and the multiplier values and then you can replace the entire case statement with just one cin and one cout statement - two lines of code.

  4. #4
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Do {} while errors

    Quote Originally Posted by Skizmo View Post
    while (choice < 1 && choice > 20)
    Guess you haven't had your morning coffee! perhaps you meant
    Code:
    choice < 1 || choice > 20
    since choice cannot be less than one and greater than 20 at the same time.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Do {} while errors

    Quote Originally Posted by B00MJUICE View Post
    Hey all,
    Started learning C++ a few days ago, and just finished my first half-useful program. It's basically a converter for weight, distance and currency.
    So I'll ask you --

    what if there were 100 conversions available? Would you write a case statement that has 100 sections, or a gigantic while loop condition with 100 tests? What if that number were upped to 300 different conversions? You would quit C++ as fast as anything if you really had to write 300 case statements and one heck of a gigantic while loop condition at the end.

    This is a time to take a step back, look at your code, and determine if this is the way to write something like this. As GCDEF stated, learn to use arrays

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jan 2009
    Posts
    596

    Re: Do {} while errors

    To add to what GCDEF said, if you store the unit names and multipliers in an array it is then easy to modify the program to store the conversion data externally, rather than hard-coded within the source code. That is, you store the names and multipliers in a text file which is read in and stored in arrays when the program starts. This would allow the user to add/remove items, or modify the multipliers (in the case of the currency exchange rates) without having to modify the program.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured