This forum is in archive mode. You will not be able to post new content.

Author Topic: [C++]Conversion example  (Read 3135 times)

0 Members and 1 Guest are viewing this topic.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
[C++]Conversion example
« on: April 09, 2011, 12:14:44 AM »
this was my last exercise of the chapter:

Write a program that asks you to enter an automobile gasoline consumption figure in the
European style (liters per 100 kilometers) and converts to the U.S. style of miles per gallon.
Note that in addition to using different units of measurement, the U.S approach
(distance / fuel) is the inverse of the European approach (fuel / distance). Note that 100
kilometers is 62.14 miles, and 1 gallon is 3.875 liters. Thus, 19 mpg is about 12.4 l/100
km, and 27 mpg is about 8.7 l/100 km.


Code: [Select]
#include <iostream>

using namespace std;

int main()
{
    int us;
    int eu;
   
    cout << "Enter the european gasoline consumption(liters): ";
    cin >> us;
    cout << endl;
    int kilometers = us * 100;
    float miles =  kilometers * 62.14;
    float gallon = us * 3.875;
   
    cout << "In the US consumption it would be like: " << miles << " miles and " << gallon << "gallons";
    cout << endl;
    system("pause");
    return 0;
}

is like this how someone would do a conversion look where I declare the variable using float and there I make a math operation at the same time I hope that it was useful thanx for watching...

more about variables: http://en.wikipedia.org/wiki/C_variable_types_and_declarations
« Last Edit: April 13, 2011, 04:43:28 PM by gh0st »

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: Conversion example in C++
« Reply #1 on: April 10, 2011, 05:59:38 PM »
Why do you keep using system("pause");  that will only work on windows otherwise you'll get:

Code: [Select]
sh: Unknown command "pause"

on Mac OS X for example.  And you could just replace system("pause"); with sleep(-1); to be more portable.
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.