Thread: Very basic C++ I need help D:

  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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    4,183
    Variables. Data Types. - C++ Documentation

    Read "Fundamental data types" section.

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

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    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-05-2011, 12:50 AM
  2. help with the basic's
    By clb2003 in forum C Programming
    Replies: 2
    Last Post: 02-09-2009, 02: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, 08: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