473,394 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Fraction Program Error

41
Hi,

I'm did a fraction program for one of my programming classes and it did compile, however when I'm running the program it crashes for some reason that I do not know.

Expand|Select|Wrap|Line Numbers
  1.  
  2. // fraction.cpp
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include "fraction.h"
  7. using namespace std;
  8.  
  9. fraction mult(fraction f1, fraction f2)
  10. {
  11.     fraction f10;
  12.     int common;
  13.  
  14.     f10.numerator = (f1.numerator * f2.numerator);
  15.     f10.denominator = (f1.denominator * f2.denominator);
  16.  
  17.     common = gcd(f10.numerator, f10.denominator);
  18.     f10.numerator /= common;
  19.     f10.denominator /= common;
  20.  
  21.     return f10;
  22. }
  23.  
  24. fraction add(fraction f1, fraction f2)
  25. {
  26.     fraction f10;
  27.     int common;
  28.  
  29.     f10.numerator = (f1.numerator + f2.numerator);
  30.     f10.denominator = (f1.denominator + f2.denominator);
  31.  
  32.     common = gcd(f10.numerator, f10.denominator);
  33.     f10.numerator /= common;
  34.     f10.denominator /= common;
  35.  
  36.     return f10;
  37. }
  38.  
  39. fraction sub(fraction f1, fraction f2)
  40. {
  41.     fraction f10;
  42.     int common;
  43.  
  44.     f10.numerator = (f1.numerator - f2.numerator);
  45.     f10.denominator = (f1.denominator - f2.denominator);
  46.  
  47.     common = gcd(f10.numerator, f10.denominator);
  48.     f10.numerator /= common;
  49.     f10.denominator /= common;
  50.  
  51.     return f10;
  52. }
  53.  
  54. fraction div(fraction f1, fraction f2)
  55. {
  56.     fraction f10;
  57.     int common;
  58.  
  59.     f10.numerator = (f1.numerator / f2.numerator);
  60.     f10.denominator = (f1.denominator / f2.denominator);
  61.  
  62.     common = gcd(f10.numerator, f10.denominator);
  63.     f10.numerator /= common;
  64.     f10.denominator /= common;
  65.  
  66.     return f10;
  67. }
  68.  
  69. void read()
  70. {
  71.     int numerator;
  72.     int denominator;
  73.  
  74.     cout << "Enter your numerator: ";
  75.         cin >> numerator;
  76.     cout << "Enter your denominator: ";
  77.     cin >> denominator;
  78.  
  79. }    
  80.  
  81. void print(fraction a_fraction)
  82. {
  83.     cout << a_fraction.numerator << "/" << a_fraction.denominator << endl;
  84. }
  85.  
  86. int gcd (int u, int v)
  87. {
  88.     u = (u < 0) ? -u : u;
  89.     v = (v < 0) ? -v : v;
  90.  
  91. while (u > 0)
  92. {
  93.     if (u < v)
  94. {
  95.     int t = u;
  96.     u = v;
  97.     v = t;
  98. }
  99.     u -= v;
  100. }
  101. return v;
  102. }
  103.  
  104. // -------------------------------
  105.  
  106. // calc.cpp
  107.  
  108. #include <iostream>
  109. #include <string>
  110. #include "fraction.h" 
  111.  
  112. using namespace std;
  113.  
  114. int main() 
  115.     fraction f1;
  116.     fraction f2;
  117.     fraction f10;
  118.     fraction f;
  119.     fraction a_fraction;
  120.  
  121.     fraction answer;
  122.     char symbol = ' ';
  123.     string nome;
  124.  
  125.    while (true)
  126.    { 
  127.     cout << "+\tADD\n";
  128.     cout << "-\tSUBTRACT\n";
  129.     cout << "* or x\tMULTIPLY\n";
  130.     cout << "/\tDIVIDE\n";
  131.     cout << "E\tEXIT\n";
  132.     cout << "What operation do you want to use?: ";
  133.         cin >> symbol;
  134.         if (symbol == 'e' || symbol == 'E')
  135.             exit(0);
  136.  
  137.      switch (symbol) 
  138.      { 
  139.       case '+': 
  140.           read();
  141.           print(a_fraction);
  142.           answer = add(f1, f2);
  143.         nome = "sum"; 
  144.         break; 
  145.      case '-': 
  146.         read();
  147.         print(a_fraction);
  148.         answer = sub(f1, f2);
  149.         nome= "difference"; 
  150.         break; 
  151.      case '*': 
  152.         read();
  153.         print(a_fraction);
  154.         answer = mult(f1, f2);
  155.         break; 
  156.      case 'x': 
  157.         read();
  158.         print(a_fraction);
  159.         answer = mult(f1, f2);
  160.         nome = "product";
  161.         break;
  162.      case '/': 
  163.         read();
  164.         print(a_fraction);
  165.         answer = div(f1, f2);
  166.         nome = "quotient";
  167.         break; 
  168.  
  169.       } 
  170.  
  171.     void print(fraction a_fraction);
  172.  
  173.    }
  174.    return 0;
  175. }
  176.  
  177. // ------------------------
  178.  
  179. // fraction.h
  180.  
  181. struct fraction
  182. {
  183.     int numerator;
  184.     int denominator;
  185. };
  186.  
  187. fraction mult(fraction f1, fraction f2);
  188. fraction add(fraction f1, fraction f2);
  189. fraction sub(fraction f1, fraction f2);
  190. fraction div(fraction f1, fraction f2);
  191. void read();
  192. void print(fraction a_fraction);
  193. int gcd (int u, int v);
  194.  
  195.  
I got some WARNING but not ERRORS.
Here they are:

c:\documentos\programas\calculator\fraction\fracti on\calc.cpp(15) : warning C4101: 'f' : unreferenced local variable

c:\documentos\programas\calculator\fraction\fracti on\calc.cpp(14) : warning C4101: 'f10' : unreferenced local variable

c:\documentos\programas\calculator\fraction\fracti on\calc.cpp(38) : warning C4700: uninitialized local variable 'a_fraction' used

c:\documentos\programas\calculator\fraction\fracti on\calc.cpp(39) : warning C4700: uninitialized local variable 'f2' used

c:\documentos\programas\calculator\fraction\fracti on\calc.cpp(39) : warning C4700: uninitialized local variable 'f1' used


Thank you all,

Doug
Apr 9 '07 #1
1 2184
svlsr2000
181 Expert 100+
With Initial look i could see one error, your trying to read using cin but your not storing it any where. so f1 and f2 contains garbage value.
Apr 9 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
0
by: amarok | last post by:
Hello all. I'm a Software Engineering student, and I'm attempting to write a program in Java that does as follows: UML for the class: Fraction() Fraction(numerator: int) ...
6
evilmonkey
by: evilmonkey | last post by:
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input...
7
by: d0ugg | last post by:
Hi, Iam writing a code for my class and I cant figure out how to solve it. I will be glad if somebody can teach me how to do something to correct it. The program is divided it 3 parts. ...
2
by: d0ugg | last post by:
Hi, I'm doing a FRACTION program for one of my Programming classes and I'm getting some errors that I can't figure it out. Here is the Assignment: 1. Convert the fraction structure into a...
4
by: d0ugg | last post by:
Hello everyone, I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms. However, I'm not...
4
by: Semajthewise | last post by:
Hi All For those of you that helped me with the questions on this... Thank you! I believe I have solved all the bugs in the code and wanted to post the final product. This is a set of code to solve...
1
by: jrw133 | last post by:
So i was given this program in class. i am supposed to create a four-function calculator for fractions using a fraction class. Heres what the requirements are:create a member function for each of...
10
by: Jason | last post by:
I'm making a program that will convert decimal inputs (in this case, in inches) and output a fractional answer. At the moment, I'm only able to output the fractional answer in three parts: A whole...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.