Fraction calculator

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 11
Reputation: DragonReborn225 is an unknown quantity at this point 
Solved Threads: 0
DragonReborn225 DragonReborn225 is offline Offline
Newbie Poster

Fraction calculator

 
0
  #1
Oct 22nd, 2008
hey, this program is supposed to calculate fractions, and I feel that I have most of the code down, but I can't get past the

fractions.cpp:50: error: expected primary-expression before 'char'

error, its probably something misicule, but I can't get it for the life of me. The error is in the main function. heres my code. THANK YOU SO MUCH IN ADVANCE!!!

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void menu (char symbol);
  6. void addFractions(int& a,int& b,int& c,int& d,int& numerator,int&
  7. denominator);
  8.  
  9. void subtractFractions(int& a,int& b,int& c,int& d,int& numerator,int&
  10. denominator);
  11.  
  12. void multiplyFractions (int& a,int& b,int& c,int& d,int& numerator,int&
  13. denominator);
  14.  
  15. void divideFractions (int& a,int& b,int& c,int& d,int& numerator,int&
  16. denominator);
  17.  
  18. int main()
  19. {
  20. int a, b, c, d;
  21. int numerator;
  22. int denominator;
  23. char symbol;
  24. cout << "first, we will need your numerators/denominators." << endl;
  25. cout << "What is your first fractions numerator?" << endl;
  26. cin>> a;
  27. cout << "What is your first fractions denominator?" << endl;
  28. cin >> b;
  29. cout << "What is your second fractions numerator?" << endl;
  30. cin >> c;
  31. cout << "What is your second fractions denominator?" << endl;
  32. cin >> d;
  33.  
  34. while (d == 0 || b == 0)
  35. {
  36. if (d == 0)
  37. {
  38. cout << "Your second fraction has a 0 in the denominator, what would" <<
  39. " you like to change it to?" << endl;
  40. cin >> d;
  41. }
  42. if (b == 0)
  43. {
  44. cout << "Your first fraction has a 0 in the denominator, what would" <<
  45. " you like to change it to?" << endl;
  46. cin >> b;
  47. }
  48. }
  49.  
  50. menu (char symbol);
  51.  
  52. return 0;
  53. }
  54. void menu(char symbol)
  55. {
  56. int a, b, c, d;
  57. int numerator, denominator;
  58. cout << "Have fractions that need solving??" << endl;
  59. cout << "Well, luckily for you, you chanced upon a fractions calculator"
  60. << endl;
  61. cout << "First, we will need the operation you wish to perform." <<
  62. endl;
  63. cout << "choose one of the following: + (addition), - " <<
  64. "(subtraction), * (multiplication), and finally / (division)." << endl;
  65. cin >> symbol;
  66. cout << endl;
  67.  
  68. switch (symbol)
  69. {
  70. case '*':
  71. multiplyFractions(a,b,c,d,numerator, denominator);
  72.  
  73. cout << "Answer is " << numerator << "/" << denominator << endl;
  74. break;
  75.  
  76. case '/':
  77. divideFractions(a,b,c,d,numerator, denominator);
  78.  
  79.  
  80. cout << "Answer is " << numerator << "/" << denominator << endl;
  81. break;
  82.  
  83. case '+':
  84. addFractions(a,b,c,d,numerator, denominator);
  85.  
  86.  
  87. cout << "Answer is " << numerator << "/" << denominator << endl;
  88. break;
  89.  
  90. case '-':
  91. subtractFractions(a,b,c,d,numerator, denominator);
  92.  
  93. cout << "Answer is " << numerator << "/" << denominator << endl;
  94. break;
  95.  
  96. default:
  97. cout << "Invalid input, pay better attention next time." <<
  98. endl;
  99. }
  100. }
  101.  
  102. void addFractions (int& a,int& b,int& c,int& d,int& numerator,int&
  103. denominator)
  104.  
  105. {
  106. int temp;
  107. temp = b*d;
  108. numerator = (temp/b*a)+(temp/d*c);
  109. denominator = temp;
  110. }
  111.  
  112. void subtractFractions (int& a,int& b,int& c,int& d,int& numerator,int&
  113. denominator)
  114.  
  115. {
  116. int temp;
  117. temp = b*d;
  118. numerator = (temp/b*a)-(temp/d*c);
  119. denominator = temp;
  120. }
  121.  
  122. void multiplyFractions(int& a,int& b,int& c,int& d,int& numerator,int&
  123. denominator)
  124.  
  125. {
  126. numerator = (a*c);
  127. denominator = (b*d);
  128. }
  129.  
  130. void divideFractions(int& a,int& b,int& c,int& d,int& numerator,int&
  131. denominator)
  132. {
  133. numerator = (a*d);
  134. denominator = (b*c);
  135. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Fraction calculator

 
0
  #2
Oct 22nd, 2008
menu (char symbol); Should be
menu (symbol);
Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 32
Reputation: emotionalone is an unknown quantity at this point 
Solved Threads: 4
emotionalone emotionalone is offline Offline
Light Poster

Re: Fraction calculator

 
0
  #3
Oct 22nd, 2008
You were also missing a bracket here and there
You also didn't need addresses when passing the values of a, b, c and d
You might not want the code but I did it anyway cos it's fun.
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. void menu(int a, int b, int c, int d, char symbol);
  7.  
  8. void addFractions(int a,int b, int c, int d, int &numerator, int &denominator);
  9.  
  10. void subtractFractions(int a, int b, int c, int d, int &numerator, int &denominator);
  11.  
  12. void multiplyFractions (int a, int b, int c, int d, int &numerator, int &denominator);
  13.  
  14. void divideFractions (int a, int b, int c, int d, int &numerator, int &denominator);
  15.  
  16. //-------------------------------------------------------------------------
  17. int main()
  18. {
  19. int a, b, c, d;
  20. int numerator;
  21. int denominator;
  22. char symbol;
  23. cout << "first, we will need your numerators/denominators." << endl;
  24. cout << "What is your first fractions numerator?" << endl;
  25. cin>> a;
  26. cout << "What is your first fractions denominator?" << endl;
  27. cin >> b;
  28. cout << "What is your second fractions numerator?" << endl;
  29. cin >> c;
  30. cout << "What is your second fractions denominator?" << endl;
  31. cin >> d;
  32. while (d == 0 || b == 0)
  33. {
  34. if (d == 0)
  35. {
  36. cout << "Your second fraction has a 0 in the denominator, what would" <<
  37. " you like to change it to?" << endl;
  38. cin >> d;
  39. }
  40. if (b == 0)
  41. {
  42. cout << "Your first fraction has a 0 in the denominator, what would" <<
  43. " you like to change it to?" << endl;
  44. cin >> b;
  45. }
  46. }
  47. menu(a, b, c, d, symbol);
  48. return 0;
  49. }
  50.  
  51. //--------------------------------------------------------------------
  52.  
  53. void menu(int a, int b, int c, int d, char symbol)
  54. {
  55. int numerator, denominator;
  56. cout << "Have fractions that need solving??" << endl;
  57. cout << "Well, luckily for you, you chanced upon a fractions calculator"
  58. << endl;
  59. cout << "First, we will need the operation you wish to perform." <<
  60. endl;
  61. cout << "choose one of the following: + (addition), - " <<
  62. "(subtraction), * (multiplication), and finally / (division)." << endl;
  63. cin >> symbol;
  64. cout << endl;
  65.  
  66. switch (symbol)
  67. {
  68. case '*':
  69. multiplyFractions(a,b,c,d,numerator, denominator);
  70. cout << "Answer is " << numerator << "/" << denominator << endl;
  71. system("pause>nul");
  72. break;
  73. case '/':
  74. divideFractions(a,b,c,d,numerator, denominator);
  75. cout << "Answer is " << numerator << "/" << denominator << endl;
  76. system("pause>nul");
  77. break;
  78. case '+':
  79. addFractions(a, b, c, d, numerator, denominator);
  80. cout << "Answer is " << numerator << "/" << denominator << endl;
  81. system("pause>nul");
  82. break;
  83. case '-':
  84. subtractFractions(a,b,c,d,numerator, denominator);
  85. cout << "Answer is " << numerator << "/" << denominator << endl;
  86. system("pause>nul");
  87. break;
  88. default:
  89. cout << "Invalid input, pay better attention next time." << endl;
  90. }
  91. }
  92.  
  93. void addFractions (int a, int b, int c, int d, int &numerator, int &denominator)
  94. {
  95. numerator = (a*d) + (b*c);
  96. denominator = (b*d);
  97. }
  98.  
  99. void subtractFractions (int a, int b, int c, int d, int &numerator, int &denominator)
  100. {
  101. numerator = (a*d)-(b*c);
  102. denominator = (b*d);
  103. }
  104.  
  105. void multiplyFractions(int a, int b, int c, int d, int &numerator, int&denominator)
  106. {
  107. numerator = (a*c);
  108. denominator = (b*d);
  109. }
  110.  
  111. void divideFractions(int a, int b, int c, int d, int &numerator, int &denominator)
  112. {
  113. numerator = (a*d);
  114. denominator = (b*c);
  115. }
Regards..

P.S. Try to tab your statements so it's easier to catch when you're missing brackets.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC