Very basic C++ I need help D:

This is a discussion on Very basic C++ I need help D: within the C++ Programming forums, part of the General Programming Boards category; First and foremost, I do not know anything about C++. I am currently trying to learn it so please go ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Very basic C++ I need help D:

    First and foremost, I do not know anything about C++. I am currently trying to learn it so please go very easy on me.

    My script is taking two fractions which the user defines, then it takes the fractions and makes them into decimals.

    Here's what I have:

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main ()
    {
        int num1, num2, denom1, denom2, dec1, dec2 ;
        
    
    
            cout << "Please enter first numerator: " ;
            cin >> num1 ;
            cout << "Please enter first denominator: " ;
            cin >> denom1 ;
            cout << "Please enter second numerator: " ;
            cin >> num2 ;
            cout << "Please enter second denominator: " ;
            cin >> denom2 ;
    
    
            dec1 = num1 / denom1 ;
            dec2 = num1 / denom1 ;
    
    
            cout << "Decimal of Fraction 1 & 2: " ;
            cout << dec1 ;
            cout << dec2 "/n" ;
    
    
    return 0;
    }


    Help? And please, understand I am VERY VERY new to this.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    What kind of help you need, that people will write the code for you?

  3. #3
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by RockyMarrone View Post
    What kind of help you need, that people will write the code for you?
    Probably not, considering that he has posted his code already?

    @OP, the int data type only stores integers (whole numbers). There are other data types that can be used to represent floating point numbers.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Quote Originally Posted by DeadPlanet View Post
    Probably not, considering that he has posted his code already?

    @OP, the int data type only stores integers (whole numbers). There are other data types that can be used to represent floating point numbers.
    So I should change int main

    to

    float main?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    Quote Originally Posted by monzetsu
    So I should change int main

    to

    float main?
    No, since that would have no effect on the variables that you are concerned with (besides being wrong in itself).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Quote Originally Posted by laserlight View Post
    No, since that would have no effect on the variables that you are concerned with (besides being wrong in itself).

    Okay, please enlighten me, I am trying to get the decimal of the two fractions, but I don't know what I'm doing wrong.

    As stated before I'm really bad at this and it's a miracle I've gotten this far..

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,577
    Variables. Data Types. - C++ Documentation

    Read "Fundamental data types" section.

    Look at "double" and "float"; if not sure which one use "double".

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    integer division disregards any values < 1.

    e.g:

    1/2 = 0.5, which, as in int, is 0
    4/3 = 1.3333, as an int = 1

    the process is equivalent to dividing and rounding down to the nearest whole number (integers are whole numbers, as you hopefully remember).

    to retain data "after the decimal" use floating point types both in executing the division operations and in storing the result.

    e.g.

    Code:
    int i=1;
    int j=2;
    float iOverJ = i/j; // here, iOverj = 0, since the division operation is carried out with integers
    
    iOverJ = (float)i/j; // iOverj = 0.5, since we instructed the computer to treat i as a floating point number
    ;

    the following is usually preferable:
    Code:
    float i=1;
    float j=2;
    float iOverJ = i/j; // iOverj = 0.5
    ;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C
    By toddeeeEEee in forum C Programming
    Replies: 7
    Last Post: 12-04-2011, 11:50 PM
  2. help with the basic's
    By clb2003 in forum C Programming
    Replies: 2
    Last Post: 02-09-2009, 01:40 AM
  3. I'm new to C++ and need basic help please
    By SkinnieMinnie in forum C++ Programming
    Replies: 28
    Last Post: 09-11-2007, 12:01 PM
  4. basic
    By srinu in forum C Programming
    Replies: 3
    Last Post: 02-13-2003, 07:21 AM
  5. VB vs. BASIC
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 07-05-2002, 08:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21