CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Classes help

  1. #1
    Join Date
    Mar 2011
    Posts
    9

    Classes help

    I wasn't sure what to name the thread, but I have a few questiong. I've written a program to add, subtract, multiply, and divide for class. It all works well except I need to add a few odds and ends. I've added the reduction part of the code but it's not working. I also need to add in a way top automatically input a 1 if the user hits enter w/o entering a number.

    The only thing left is I need to ensure the denom is not a 0 or negative. I was thinking something like

    if(denom >= 0)
    cout<<"please enter a valid denominator\n";
    cin>>denom;
    a.set_den(denom);

    which works but I need to make it recursive or add in a check. I'm trying to keep my code from becoming bloated as well, so can I write that as a single code also w/o needing to have the code twice, once for each input of the denom. I'm listing my code below, thanks in advance.

    //Define class Fraction Num\Denom int only, no float***
    //Program operations +,-,*,/ to (overload)***
    //When a user inputs a blank input build a constructor that enters "1"
    //Research Euclidean Algorithm to find GCD for reduction.
    //Class checks, no 0 or "-" in denominator.
    //Minimal code in "main***

    #include <iostream>
    #include <cassert>
    #include <cmath>
    using namespace std;


    class Fraction

    {
    int num;
    int den;


    public:

    //public function can access private function's and set variables

    void set_num(int numpub)
    {num=numpub;};

    void set_den(int denpub)
    {den=denpub;};

    int get_num()
    {return num;};

    int get_den()
    {return den;};


    int gcd(int set_num, int set_den)
    {
    if(set_den == 0) return set_num;
    else
    {return gcd(set_den, set_num % set_den);};
    }


    Fraction(int a, int b)
    {set_num(a); set_den(b);};
    Fraction(){};

    //Fraction x is the b input and the default w/ no x. is the a input
    Fraction operator +(Fraction x)
    {
    Fraction result;
    result.set_num(num*x.get_den()+den*x.get_num());
    result.set_den(den*x.get_den());
    return result;
    }

    Fraction operator -(Fraction x)
    {
    Fraction result;
    result.set_num(num*x.get_den()-den*x.get_num());
    result.set_den(den*x.get_den());
    return result;
    }
    Fraction operator *(Fraction x)
    {
    Fraction result;
    result.set_num(num*x.get_num());
    result.set_den(den*x.get_den());
    return result;
    }
    Fraction operator /(Fraction x)
    {
    Fraction result;
    result.set_num(num*x.get_den());
    result.set_den(den*x.get_num());
    return result;
    }



    };



    int main()

    {
    Fraction a;
    Fraction b;
    Fraction c;
    Fraction out;
    int numer;
    int denom;
    char oper;


    cout<<"Please enter your FIRST Numerator and Denominator:\n";
    cin>>numer >> denom;
    a.set_num(numer); a.set_den(denom);


    if(denom >= 0)
    cout<<"please enter a valid denominator\n";
    cin>>denom;
    a.set_den(denom);


    cout<<"Please enter the SECOND Numerator Denominator:\n";
    cin>>numer >> denom;
    b.set_num(numer); b.set_den(denom);

    if(denom >= 0)
    cout<<"please enter a valid denominator\n";
    cin>>denom;
    b.set_den(denom);

    //operation, c is the new output after the operation is complete.
    cout<<"Choose operation of '+,-,*,/'\n";
    cin>>oper;
    switch(oper)
    {
    case '+':c=a+b;
    break;
    case '-':c=a-b;
    break;
    case '*':c=a*b;
    break;
    case '/':c=a/b;
    break;
    default: cout<<"Invalid Operator!\n";
    break;
    }




    cout<<"Success!\n"<<c.get_num()<<"/"<<c.get_den()<<endl;

    return 0;

    };

    /*
    int gcd(int set_num, int set_den)
    {
    if(set_den == 0) return set_num;
    else
    {return gcd(set_den, set_num % set_den);};
    }
    */
    Last edited by Belcross; March 6th, 2011 at 12:37 AM.

  2. #2
    Join Date
    Aug 2009
    Posts
    440

    Re: Classes help

    First order of business is to use code tags.

  3. #3
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: Classes help

    you say you want denominator greater than zero, but in your code you ask for just the opposite.

    you need to do something like
    Code:
    int denom = 0;
    while (denom <= 0)
    {
         cout << "Enter denom: ";
         cin >> denom;
    }
    a.set_den(denom);

  4. #4
    Join Date
    Mar 2011
    Posts
    9

    Re: Classes help

    Ahhh ok I see what your saying. Does anyone know why it's not reducing or how to input a 1 automaticly on someone pressing "Enter" w/o inputing a number?

    Also how do you use code tags?

  5. #5
    Join Date
    Apr 2008
    Posts
    725

    Re: Classes help

    Quote Originally Posted by Belcross View Post
    Ahhh ok I see what your saying. Does anyone know why it's not reducing or how to input a 1 automaticly on someone pressing "Enter" w/o inputing a number?

    Also how do you use code tags?
    [code]somecode[/code]

  6. #6
    Join Date
    Mar 2011
    Posts
    9

    Re: Classes help

    Hmmm, I did that and all it did was display them on the page.... oh well I might have typoed or something.

  7. #7
    Join Date
    Apr 2008
    Posts
    725

    Re: Classes help

    Quote Originally Posted by Belcross View Post
    Hmmm, I did that and all it did was display them on the page.... oh well I might have typoed or something.
    try again

  8. #8
    Join Date
    Mar 2011
    Posts
    9

    Re: Classes help

    ok, lets try w/ the new code then

    Code:
    //Define class Fraction Num\Denom int only, no float***
    //Program operations +,-,*,/ to (overload)***
    //When a user inputs a blank input build a constructor that enters "1"
    //Research Euclidean Algorithm to find GCD for reduction.
    //Class checks, no 0 or "-" in denominator.***
    //Minimal code in "main***
    
    #include <iostream>
    #include <cassert>
    #include <cmath>
    using namespace std;
    
    
    class Fraction
    
    {
        int num;
        int den;
    
    
    public:
    
    //public function can access private function's and set variables
    
      void set_num(int numpub)
      {num=numpub;};
    
      void set_den(int denpub)
      {den=denpub;};
    
      int get_num()
      {return num;};
    
      int get_den()
      {return den;};
    
    
    
    
    Fraction(int a, int b)
      {set_num(a); set_den(b);};
     Fraction(){};
    
     //Fraction x is the b input and the default w/ no x. is the a input
    Fraction operator +(Fraction x)
    {
      Fraction result;
      result.set_num(num*x.get_den()+den*x.get_num());
      result.set_den(den*x.get_den());
    return result;
    }
    
    Fraction operator -(Fraction x)
    {
      Fraction result;
      result.set_num(num*x.get_den()-den*x.get_num());
      result.set_den(den*x.get_den());
    return result;
    }
    Fraction operator *(Fraction x)
    {
      Fraction result;
      result.set_num(num*x.get_num());
      result.set_den(den*x.get_den());
    return result;
    }
    Fraction operator /(Fraction x)
    {
      Fraction result;
      result.set_num(num*x.get_den());
      result.set_den(den*x.get_num());
    return result;
    }
    
    int gcd(int set_num, int set_den)
    {
    if(set_den == 0) return set_num;
    else
    {return gcd(set_den, set_num % set_den);};
    }
    
    
    
    };
    
    
    
    int main()
    
    {
        Fraction a;
        Fraction b;
        Fraction c;
        Fraction out;
        int numer=0;
        int denom=0;
        char oper;
    
    
    
    
        cout<<"Please enter your FIRST Numerator and Denominator:\n";
        cin>>numer >> denom;
        a.set_num(numer); a.set_den(denom);
    
    while (denom <= 0)
    {
         cout << "Denominator may not be a 0 or negative number, please enter a valid denominator:\n";
         cin >> denom;
    }
    a.set_den(denom);
    
        cout<<"Please enter the SECOND Numerator and Denominator:\n";
        cin>>numer >> denom;
        b.set_num(numer); b.set_den(denom);
    
            while (denom <= 0)
    {
         cout << "Denominator may not be a 0 or negative number, please enter a valid denominator:\n";
         cin >> denom;
    
    }
    b.set_den(denom);
    
    
    
    
        //operation, c is the new output after the operation is complete.
        cout<<"Choose operation of '+,-,*,/'\n";
        cin>>oper;
        switch(oper)
        {
        case '+':c=a+b;
          break;
        case '-':c=a-b;
          break;
        case '*':c=a*b;
          break;
        case '/':c=a/b;
          break;
        default: cout<<"Invalid Operator!\n";
          break;
        }
    
    
    
    
    cout<<"Success!\n"<<c.get_num()<<"/"<<c.get_den()<<endl;
    
    
    
    //calculate
    
    //display
    return 0;
    
    };
    
    /*
    int gcd(int set_num, int set_den)
    {
    if(set_den == 0) return set_num;
    else
    {return gcd(set_den, set_num % set_den);};
    }
    */

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Classes help

    Quote Originally Posted by Belcross View Post
    Ahhh ok I see what your saying. Does anyone know why it's not reducing
    Use the debugger that comes with your compiler.

    When you write a program, you are responsible for debugging it. You accomplish that by using your program's debugger and stepping through the program when it is running. You then get a chance to watch the variables and execution path of your program.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Apr 2008
    Posts
    725

    Re: Classes help

    Code:
    void set_num(int numpub)
      {num=numpub;}; <- these
    
      void set_den(int denpub)
      {den=denpub;}; <- are pointless
    Code:
    void set_num(int numpub)
      {num=numpub;}
    
      void set_den(int denpub)
      {den=denpub;}

  11. #11
    Join Date
    Mar 2011
    Posts
    9

    Re: Classes help

    I see, I didn't know that you could watch the program run like that.

    I've noticed people seem to not like some of the ";" I've added, but it seems they don't make a diffrence if they are there or not in some cases, is this a point of being easier to read? I've only been at this for a short time so still learning.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Classes help

    Quote Originally Posted by Belcross View Post
    I see, I didn't know that you could watch the program run like that.
    The debugger is a mandatory tool that you must learn to use. It basically lets you run the program one line at a time, under your control. Then you can see what values your variables are, what path your program is taking, set breakpoints (run the program full speed until it gets to a line you chose in your code) etc.

    Unless the problem is so obvious that it can be spotted by just eyeballing the code, no one runs the computer in their head. If we were to answer your question, and the program is not trivial, then guess what tool we use? The debugger. So if we use this tool to answer your question, then it's a good idea for you to learn how to use this tool for yourself.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 6th, 2011 at 09:22 PM.

  13. #13
    Join Date
    Mar 2011
    Posts
    9

    Re: Classes help

    Not going to lie, I hit F8 and it complies and runs the .EXE, I didnt know the debugger was seperate. We use GNU 4.3 on Linux at school and we never used the debugger just tried to run it and see if it kicks back any errors. I realize the programs are small so maybe they just havnt taught that but seems like an oversite that's pretty big. I'll have to figure it out tonight or tomorrow.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Classes help

    Quote Originally Posted by Belcross View Post
    Not going to lie, I hit F8 and it complies and runs the .EXE, I didnt know the debugger was seperate. We use GNU 4.3 on Linux at school and we never used the debugger just tried to run it and see if it kicks back any errors.
    The debugger that comes with GNU C++ is GDB. By itself, GDB is a command-line based debugger, but there are graphical user interfaces to GDB that makes using GDB very simple. Type in "gdb" from the Linux command-line and see what you get.

    Another option is to get the free Visual Studio 2010 Express and when you are at home, use that to develop your programs. Then you have the Visual Studio debugger at your disposal, which is one of the best debuggers out there.
    I realize the programs are small so maybe they just havnt taught that but seems like an oversite that's pretty big. I'll have to figure it out tonight or tomorrow.
    Usually teachers do not mention debuggers to help you develop and understand your programs. Why is that? -- maybe tradition, as students way back when were supposed to go through their code "by hand" and figure out the problems, or use output statements to see what the value of the variables are. In this day and age, that is all automated by using interactive debuggers.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Mar 2011
    Posts
    9

    Re: Classes help

    Well ATM I'm useing codeblocks on my windows machine, I tried Microsoft, but found I liked this better, I found it simpler to use. Still not figured the debugging out, it says it ran, but doesno't return anything at the end. Working on figuring it out now

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured