• Log in or sign up for an account here: https://www.sythe.org/login

Easy question, I'm just starting

Demonten

Newcomer
Joined
Sep 14, 2010
Sythe Gold
0
Messages
5
Location
Toronto, ON, CAN
So I'm just starting to program, and I'm writing VERY simple conversion programs running out of console, and I'm curious as to how I could get it to output decimals, because 12 inches is not exactly 30 centimeters, it's 30.48 centimeters. I've got a bunch of these, but I'll use the inches to centimeters one for this post.

Code:
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    int inch;
    cout << "Enter the distance in inches:";
    cin >> inch;

    int cm;
    cm = inch * 2.54;


    cout << "Distance in centimeters:";
    cout << cm << endl;
    system("PAUSE");
    return 0;
}
 
instead of using the command "int" infront of inch, and cm, use float.
Code:
 #include<iostream>
using namespace std;

int main()
{
    float inch, cm;
    cout << "Enter the distance in inches:";
    cin >> inch;

    cm = inch * 2.54;

    cout << "Distance in centimeters:";
    cin >> cm << endl;
    system("PAUSE");
    return 0;
}

Try that.
Edit: Companies/Teachers rather shorter/less lines of code, as it takes up less space and runs quicker.
Just a heads up.
 
^ What that guy said, and except having more code lines doesn't really affect it.
 
You can also use double instead of int.

double inch, cm;
 
Runescape Gamblingrunescape gambling
Back
Top