Thread: need help with fractions in C++

  1. #31
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    far too many uninitialised variables, set things to 0 as often as you can, at least then you have a known value to work with. and at least as a basic protection against bad user input in this case., this thread is littered with examples of you just not taking any notice of good advice given.

    and ay caramba :

    Code:
    int simplify(int x2 ,int y2 ,int gcd ,int a ,int b )
    {
         
        a= x2/gcd;
        b= y2/gcd;
    
    
        return simplify;
    }
    What on earth do you think simplify will be?
    Last edited by rogster001; 02-19-2012 at 08:55 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  2. #32
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
     
     
    int simplify(int x2 ,int y2 ,int gcd ,int a ,int b )
    {
         
        a= x2/gcd;
        b= y2/gcd;
    
    
        return simplify( x2, y2, gcd, a,b);
    }
     
     
     int gcd(int x2, int y2)
    {
        int i;
        int gcd2;
        for (i=1; i<=x2; i++)
        {
            if((x2%i==0) && (y2%i==0))
                gcd2 = i;
        }
        return gcd2;    
    }
    int multiply() 
    {
        int numerator,denominator,numerator2,denominator2 ;
        int x;
    	 int x2, t2;
    	 int c;
    	 int d;
    
    
        cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
        cin>>numerator;
        cin>>denominator;
      
        cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
        cin>>numerator2;
        cin>>denominator2;
      
        cout<<"Input instruction*_" << endl;
        x2 = numerator*numerator2;
        t2 =denominator*denominator2;
        
        x=gcd( x2, t2);
        simplify(  x2 ,  t2 ,x , c ,  d);
         
      
        cout<<"("<< numerator << "/"<<denominator <<") * ("<< numerator2 <<"/"<< denominator2 <<") = " << c << "/" << d;
        cout << "\n";
        return 0;
    } 
     
     
     
    int main()           
    {
        multiply();
        system ("PAUSE");
    }

  3. #33
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    right erm, yea.

    If you want good help, its a two way thing, are you really so unbelievably lazy and rude that you think its ok to just dump wedges of code by way of a response every time? And then expect to get expert help from the board members, when its a bloomin sunday too.

    Why not add some words too?????
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #34
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    At this point, you are just guessing.
    Work with us - what is it that you fail to understand?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #35
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    rogster Im not rude... I sitting on this project and really have enough . im dont want to be a programmer its only one of my subjects which i need to pass.. I dont even like it. u think for me its easy to ask like an idiot 10 times this same.. ..
    i will be never a programmer..
    i really need help to finish it..

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, even more than you not wanting to be a programmer, we don't want to be robots

    So, when you post your code, tell us: what's wrong?
    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

  7. #37
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Regardless, the subject is there for a reason. Therefore, you need to make sure you acquire the abilities needed to pass the course. It doesn't matter whether you like it or not.
    Tell us what you are having problems with - in details, or just forget about getting any help. We cannot help you if you do not work with us, and we will not do your homework for you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #38
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    why return simplify( x2, y2, gcd, a,b);
    didnt return the values to multiply

  9. #39
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What do you expect it to do? That's a function call.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #40
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    well its a bit of a shaky one, but an oft quoted:

    "To ask may be but a moment's shame, not to ask and remain ignorant is a lifelong shame"

    We don't mind how often you ask questions, that is good, and it is true that by teaching you learn, so like i say it is a two way thing, I would not bother answering at all otherwise.

    You just have to consider how it looks when code is posted without any other message, It comes across very badly, no one is paid here remember.
    Fair enough you dont like programming, but as Elysia points out it is a pre requisite of your course, I hated half the crap i had to learn too,( my study was nothing to do with IT or technical by the way) but it is a case of shifting your mindset and concentrating on the long term goal.
    Last edited by rogster001; 02-19-2012 at 09:34 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  11. #41
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To be honest, the problem you are facing is that you are not reading.
    You want to return two values from a function. We say: "use references." I even gave you an example in post #18: need help with fractions in C++
    But nowhere in your code do I see you using references.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #42
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
     
    
    int simplify(int x2 ,int y2 ,int gcd ,int a ,int b )
    {
         
        a= x2/gcd;(simplify its counting a and b)
        b= y2/gcd;
    
    
        return (x2,y2,gcd,a,b); now its returning the x2,y2,gcd,a,b but the value of and b its different
    }
     
     
     int gcd(int x2, int y2)
    {
        int i;
        int gcd2;
        for (i=1; i<=x2; i++)
        {
            if((x2%i==0) && (y2%i==0))
                gcd2 = i;
        }
        return gcd2;    
    }
    int multiply() 
    {
        int numerator,denominator,numerator2,denominator2 ;
        int x;
         int x2, t2;
         int c;
         int d;
    
    
        cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
        cin>>numerator;
        cin>>denominator;
      
        cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
        cin>>numerator2;
        cin>>denominator2;
      
        cout<<"Input instruction*_" << endl;
        x2 = numerator*numerator2;
        t2 =denominator*denominator2;
        
        x=gcd( x2, t2);
        simplify(  x2 ,  t2 ,x , c ,  d); here im sending arguments  to simplify(1)                 3)its returning the values here  
     
        cout<<"("<< numerator << "/"<<denominator <<") * ("<< numerator2 <<"/"<< denominator2 <<") = " << c << "/" << d;
        cout << "\n";
        return 0;
    } 
     
     
     
    int main()           
    {
        multiply();
        system ("PAUSE");
    }
    Why c and d its 0??????

  13. #43
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why are you not listening?
    Go read up on the command operator, passing by value and passing by reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #44
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    its working thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fractions are just = to 0
    By Macca in forum C Programming
    Replies: 1
    Last Post: 05-10-2011, 05:41 AM
  2. Fractions
    By madmax2006 in forum C Programming
    Replies: 4
    Last Post: 06-09-2010, 11:58 PM
  3. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  4. Fractions
    By Aakash Datt in forum C++ Programming
    Replies: 8
    Last Post: 04-30-2003, 07:36 PM
  5. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 07:51 AM