Struct HELP!

This is a discussion on Struct HELP! within the C++ Programming forums, part of the General Programming Boards category; im learning by my own how to code with C++, i got to the topic of Structures. The book has ...

  1. #1
    -Beginners
    Guest

    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: ";
    cin>>fraction.firstInteger;



    if (fraction.firstInteger= 1/2)
    equivalent=.5;
    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
    Posts
    1,348
    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.

    Kuphryn

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

    and for parsing user input... (a very crude method
    Code:
    char buf[256];
    char num[256];
    char den[256];
    int j=0;
    cin.getline(buf);
    while(buf[j]!='/'&&j<strlen(buf))
    {
    	strcat(num,buf[j]);
    	j++;
    }
    j++;
    while(buf[j]!='\0'&&j<strlen(buf))
    {
    	strcat(den,buf[j]);
    	j++;
    }
    and then convert char num[] and den[] to the string.

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


    -LC
    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
    beginnerss
    Guest
    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
    then
    half =0.5

    half = fraction

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

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

    //edit2:
    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
    Posts
    8
    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:
    Code:
    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:
    Code:
    bool compareFractions( fraction& firstFraction, fraction& secondFraction )
    {
        if( firstFraction.denom == secondFraction.denom )
            if( firstFraction.num == secondFraction.num)
                return true;
                // They are totally equal!
            else
                return false;
        else
            // 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
    Posts
    291
    This is wrong,
    Code:
    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, 02: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 Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM

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