Thread: Struct HELP!

  1. #1

    Question Struct HELP!

    im learning by my own how to code with C++, i got to the topic of Structures. The book has some exercises and i am atempting to complete them..

    it asks for a program that would be able to add fractions..
    i have somehow an idea of how it would be, but my here's my problem.

    I ask for the fraction, i would like to have that fraction compared to a variable thus making it decimal, so i can perform any operation (+, -, x, /) with decimals, and if possible that most common fractions be displayed (1/2, 1/4, 1/8) but not like1/3453.

    here's the code of what i been working..
    __________________________________________________ __

    #include <iostream>
    using namespace std;
    struct math //structure tag

    float firstInteger;

    void main()
    float equivalent;
    math fraction; //define the structure object

    cout<<"Enter the first integer: ";

    if (fraction.firstInteger= 1/2)
    cout<<"___this is the equivalent" <<equivalent<<endl;

    cout<<"Your 1st Integer is: \n["<<fraction.firstInteger<<" ]"<<endl;

    //End of code

    any help would be appreciated, THANK YOU!

  2. #2
    Registered User
    Join Date
    Nov 2001
    The logic looks correct. The problem has to do with I/O. One solution is to rend and entire line using std::getline() and a string object. Afterward, parse the input string accordingly.


  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    for programming fractions try something like this:
    struct fraction
    	int num;
    	int den;
    and work from there.

    and for parsing user input... (a very crude method
    char buf[256];
    char num[256];
    char den[256];
    int j=0;
    and then convert char num[] and den[] to the string.

    At least this is a rough idea... I'm sure there are better methods.

    Last edited by Lynux-Penguin; 07-22-2003 at 04:54 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Can you be more specific with the

    struct fraction
    int num;
    int den;

    is there a function that can compare the string with a variable

    if fraction == 1/2
    half =0.5

    half = fraction

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    istringstream in("3/4");
    in >> num >> den;

    This doesn't check for a "/", but that requires more sophisticated code.

    To check to see if fractions equal, the easiest way is to
    1) convert a fraction to num and den
    2) divide the two and store the result in a float or double. (cast num or den to a float or double.)
    3) compare the double or float with another one
    Last edited by ygfperson; 07-22-2003 at 05:05 PM.

  6. #6
    Registered User
    Join Date
    Jul 2003
    I'm working on representing fractions as well, but it seems that doing it with classes is easier, then you can overload the input (std::cin) function, and it works quite smoothly...

    But when you want to learn structures:
    struct fraction
        int num; // numerator
        int denom; // denominator
    Basically, this declears the struct fraction to contain these two integers.

    Then make some function to compare/add/multiply/divide two structures, something like this:
    bool compareFractions( fraction& firstFraction, fraction& secondFraction )
        if( firstFraction.denom == secondFraction.denom )
            if( firstFraction.num == secondFraction.num)
                return true;
                // They are totally equal!
                return false;
            // Multiply up to the common denominator
            // Then compare the numerator
    As you can see, compareFractions takes two fraction references and compares them.

    As far as input is concerned, I have no idea of a good solution to that, other than creating classes, as I mentioned.

    Hope some o' this helped

    -- Placid

  7. #7
    Registered User
    Join Date
    Oct 2002
    This is wrong,
    if (fraction.firstInteger= 1/2)
    use == when comparing and = when assigning values to variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM