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
The second one, prints and peforms operationsCode:#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
Main programCode:#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; }
Thank youCode:#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(); }



LinkBack URL
About LinkBacks



.
.