Thread: my program keeps dieing on me please help

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    33

    my program keeps dieing on me please help

    Hello, well this program involves 2 self made header files and the main program file. its supposed to perform mathmatical operations with fractions, but after it promps and scans in the numbers from the user it stops working please help here is all the code:

    First header file, the interface header file
    Code:
    #include <iostream.h>
    #ifndef fraction_h
    #define fraction_h
    
    class numbers
    {
     private:
      int num, den, x, y;
    
     public:
      numbers(int num = 0, int den = 1);
      void reduction();
      numbers add(numbers, numbers);
      numbers subtract(numbers, numbers);
      numbers multiply(numbers, numbers);
      numbers divide(numbers, numbers);
      void printfrac();
      void printdec();
      
    };
    #endif
    The second one, prints and peforms operations

    Code:
    #include<iomanip.h>
    #include<iostream.h>
    #include"16-6.h"
    
    numbers::numbers(int num, int den)
    {
      int x, y;
      x = num;
      y = den;
    }
    
    void numbers::printfrac()
    {
      cout << x << "/" << y; /* prints as fraction */
    }
    
    void numbers::printdec()
    {
      
      cout << dec /* prints in decimal form */
           << setprecision(3) << (float)x/y;
    }
    void numbers::reduction()		/* reduces fraction */
    {
      int i, small;
      if (x < y)
        small = x;
      else
        small = y;
      for (i = small; 1 >= 1; i--)
        if(x % i == 0 && y % i == 0)
          {
    	y = y / i;
    	x = x / i;
          }
    }
    numbers numbers::multiply(numbers one, numbers two)
    {
      int whole = 0;
      numbers d;
      d = one.x * two.y;
      d.reduction();
      return d;
    }
    
    numbers numbers::divide(numbers one, numbers two)
    {
      int whole = 0;
      numbers d;
      d = one.x * two.y; // invert and multiply
      d = two.x * one.y;
      d.reduction();
      return d;
    }
    
    numbers numbers::add(numbers one, numbers two)
    {
      int t1 = 0, t2 = 0, temp_num1 = 0, temp_num2 = 0, temp_den1 = 0,
        temp_den2 = 0, whole = 0;
      numbers d;
      t1 = one.y;
      t2 = two.y;
      temp_num1 = one.x * t2;
      temp_den1 = one.y * t2;
      temp_num2 = two.x * t1;
      temp_den2 = two.y * t1;
      d.x = temp_num1 + temp_num2;
      d.y = temp_den1;
      d.reduction();
      return d;
    }
    
    numbers numbers::subtract(numbers one, numbers two)
    {
      int t1 = 0, t2 = 0, temp_num1 = 0, temp_num2 = 0, temp_den1 = 0,
        temp_den2 = 0, whole = 0;
      numbers d;
      t1 = one.y;
      t2 = two.y;
      temp_num1 = one.x * t2;
      temp_den1 = one.y * t2;
      temp_num2 = two.x * t1;
      temp_den2 = two.y * t1;
      d.x = temp_num1 - temp_num2;
      d.y = temp_den1;
      d.reduction();
      return d;
    }
    Main program

    Code:
    #include <iostream.h>
    #include "student2n3.h"
    #include "16-6.h"
    
    
    int main()
    {
      int num, den;
      numbers three(num = 0, den = 1);
      cout << "Enter a fraction (ex. 4/5):  ";
      cin >> num;
      cin.ignore(80, '/');
      cin >> den;
      numbers one(num, den);
    
      cout << "Enter a second fraction:  ";
      cin >> num;
      cin.ignore(80, '/');
      cin >> den;
      numbers two(num, den);
      // This is where the program dies!!!!!!!!!!!!!!
      three.add(one, two);
      one.printfrac();
      cout << " + ";
      two.printfrac();
      cout << " = ";
      three.printfrac();
      cout << " = ";
      three.printdec();
    
      three = three.subtract(one, two);
      one.printfrac();
      cout << " - ";
      two.printfrac();
      cout << " = ";
      three.printfrac();
      cout << " = ";
      three.printdec();
    
      three = three.multiply(one, two);
      one.printfrac();
      cout << " * ";
      two.printfrac();
      cout << " = ";
      three.printfrac();
      cout << " = ";
      three.printdec();
    
      three = three.divide(one, two);
      one.printfrac();
      cout << " / ";
      two.printfrac();
      cout << " = ";
      three.printfrac();
      cout << " = ";
      three.printdec();
      
      one.printfrac();
      cout << " = ";
      one.printdec();
      
      two.printfrac();
      cout << " = ";
      two.printdec();
    }
    Thank you

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Here's one problem, you're updating local variables x and y and not the ones in the class, so the class x and y remain uninitialised.

    Remove int x, y;
    Code:
    numbers::numbers(int num, int den)
    {
      x = num;
      y = den;
    }

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Well first a the class numbers should contain a num and den as data-members. You have no use of x and y in your example.

    Also your constructor donīt initialize your data-members correctly.
    Code:
    numbers::numbers(int num, int den)
    {
      int x, y;//creates local variabels x and y
      x = num;
      y = den;
    }
    This code creates 2 local variabels that is destroyed after the constructor exits. What you need to do is initialize data-members y and y like this.
    Code:
    numbers::numbers(int num, int den):
    this.num(num),this.den(den)
    {}
    Also yours arithmaric operations need some fix (why not overloading appropriate operator(s)??) Insteed of
    Code:
    three.add(one, two);
    Isnīt this more intuitive??
    Code:
    three = one + two;
    Anyway this is a design issue, back to the errors .
    The add/subtract/multiply/division member-function needs some modification. Iīm gonna show you how to fix one of them and leave the rest for you (itīs the same logic to them all).

    The Add-function
    Code:
    numbers numbers::add(numbers one, numbers two)
    {
     numbers d;
     d.num = (one.num*two.den) +(two.num*one.den);
     d.den = one.den*two.den
    return d;
    }
    If something is unclear, just ask .
    Last edited by ripper079; 05-04-2003 at 08:16 AM.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    ok i did what you guys said, but it still dies right after i enter in both fractions, and i dont know why

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    whoa man, thanks, something so simple

    :-) thanks buddy

  6. #6
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    A saw some error in my previous code sorry
    The constructor dont accept a this-pointer in its initialization list (there probably is one way around it but donīt have my book to check it)
    Code:
    numbers::numbers(int num, int den):
    this.num(num),this.den(den)
    {}
    Compiles to an error, change it to something like
    Code:
    numbers::numbers(int num, int den)
    {
        this->num = num;
        this->den = den;
    }
    And your add-function should be (but I still prefer that you overload operator +)
    Code:
    /* Also one and two should be passes as const-refernses to avoid unneccesasy copies of objects - but I leave it out for demonstation purposes*/
    void numbers::add(numbers one, numbers two)
    {
        num = (one.num*two.den) +(two.num*one.den);
        den = one.den*two.den;
    }
    Here is some compensation for mine previous misstake
    Code:
    #include <iostream>
    #include <conio.h>//warining - nonstandard
    
    using namespace std;
    
    class numbers
    {
     private:
      int num, den;
    
     public:
      numbers(int num, int den);
      void add(numbers one, numbers two);
      int getNum() const
      {
        return num; 
      }
      int getDen() const
      {
        return den;
      }
    };
    
    numbers::numbers(int num, int den)
    {
        this->num = num;
        this->den = den;
    }
    
    void numbers::add(numbers one, numbers two)
    {
        num = (one.num*two.den) +(two.num*one.den);
        den = one.den*two.den;
    }
    
    int main()
    {
        numbers one(5, 5);
        numbers two(1, 25);
        numbers three(0,0);
        three.add(one, two);
        cout << three.getNum() << "/" << three.getDen() << endl;
        getch();
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM