Easy question, I'm just starting

Discussion in 'Programming General' started by Demonten, Sep 14, 2010.

Easy question, I'm just starting
  1. Unread #1 - Sep 14, 2010 at 1:23 PM
  2. Demonten
    Joined:
    Sep 14, 2010
    Posts:
    5
    Referrals:
    0
    Sythe Gold:
    0

    Demonten Newcomer

    Easy question, I'm just starting

    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;
    }
    
     
  3. Unread #2 - Sep 14, 2010 at 7:45 PM
  4. Influenza
    Joined:
    Aug 2, 2008
    Posts:
    698
    Referrals:
    0
    Sythe Gold:
    0

    Influenza Apprentice
    Banned

    Easy question, I'm just starting

    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.
     
  5. Unread #3 - Sep 16, 2010 at 9:39 PM
  6. clawed
    Joined:
    Jan 29, 2010
    Posts:
    230
    Referrals:
    0
    Sythe Gold:
    0

    clawed Active Member

    Easy question, I'm just starting

    ^ What that guy said, and except having more code lines doesn't really affect it.
     
  7. Unread #4 - Sep 19, 2010 at 4:53 AM
  8. Demonten
    Joined:
    Sep 14, 2010
    Posts:
    5
    Referrals:
    0
    Sythe Gold:
    0

    Demonten Newcomer

    Easy question, I'm just starting

    Alright, thanks.
     
  9. Unread #5 - Sep 21, 2010 at 3:56 AM
  10. Dome106
    Joined:
    Dec 9, 2009
    Posts:
    383
    Referrals:
    0
    Sythe Gold:
    57

    Dome106 Forum Addict
    Banned

    Easy question, I'm just starting

    You can also use double instead of int.

    double inch, cm;
     
< Need help with easy java hw problem | C# Game Help... >

Users viewing this thread
1 guest


 
 
Adblock breaks this site