944,180 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5096
  • C++ RSS
Oct 17th, 2004
0

Help Writing Fuction to Read Fractions From User

Expand Post »
Hi,
For class I have to write a program that performs mathematical operations onto fractions. I'm really drawing a blank as far as how to setup the function that reads the fractions inputed by the user. If you care to look the assignment can be found at:

http://www.cs.wmich.edu/~nelson/CS11...b04/lab04.html

The way the function needs to work is to read in two integers separated by a space and then store that as a fraction (not as a decimal so maybe it needs to be stored as two integers?). I'm not sure if I the function should return a value or just pass by reference and what type of variable the two numbers separated by a space should be store in (and thus what variable type the parameter(s) of the function should be). If someone could help me out as far as how to set up this function I would be very grateful.

thanks,

-Scott
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skamen is offline Offline
10 posts
since Oct 2004
Oct 17th, 2004
0

Re: Help Writing Fuction to Read Fractions From User

>I'm really drawing a blank as far as how to setup the function that reads the fractions inputed by the user.
It's as simple as reading two integers and saving them in numerator and denominator variables.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 17th, 2004
0

Re: Help Writing Fuction to Read Fractions From User

Quote originally posted by Narue ...
>I'm really drawing a blank as far as how to setup the function that reads the fractions inputed by the user.
It's as simple as reading two integers and saving them in numerator and denominator variables.
If it was that way I would have no problem but the way this program has to work the user will input both numbers on the same line seperated by a space so it's not quite so easy.

thanks anways though,

-Scott
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skamen is offline Offline
10 posts
since Oct 2004
Oct 17th, 2004
0

Re: Help Writing Fuction to Read Fractions From User

>so it's not quite so easy
Sure it is. The way cin's >> operator handles whitespace makes it easy:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int num, den;
  8.  
  9. cout<<"Enter a fraction: ";
  10. cin>> num >> den;
  11.  
  12. cout<< num <<'/'<< den <<endl;
  13. }
The really nifty part is that you can break up that call into two calls and it works the same way:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int num, den;
  8.  
  9. cout<<"Enter a fraction: ";
  10. cin>> num;
  11. cin>> den;
  12.  
  13. cout<< num <<'/'<< den <<endl;
  14. }
Wrapping everything in a function is trivial as well:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. bool get_fraction ( int& num, int& den )
  6. {
  7. return cin>> num >> den;
  8. }
  9.  
  10. int main()
  11. {
  12. int num, den;
  13.  
  14. cout<<"Enter a fraction: ";
  15. if ( !get_fraction ( num, den ) )
  16. cerr<<"Invalid input"<<endl;
  17. else
  18. cout<< num <<'/'<< den <<endl;
  19. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 17th, 2004
0

Re: Help Writing Fuction to Read Fractions From User

Sorry, this is off topic but do you happen to go to Western Michigan U?
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
jasweb2002 is offline Offline
56 posts
since Sep 2004
Oct 17th, 2004
0

Re: Help Writing Fuction to Read Fractions From User

Quote originally posted by jasweb2002 ...
Sorry, this is off topic but do you happen to go to Western Michigan U?
Yeah, I do go to Western Michigan. Thanks for the help.

-Scott
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skamen is offline Offline
10 posts
since Oct 2004
Oct 18th, 2004
0

Re: Help Writing Fuction to Read Fractions From User

Quote originally posted by skamen ...
Yeah, I do go to Western Michigan. Thanks for the help.

-Scott
Boo and hiss. "Proud" Chippewa here. :-|
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
jasweb2002 is offline Offline
56 posts
since Sep 2004
Oct 20th, 2004
0

Re: Help Writing Fuction to Read Fractions From User

I'm still working on this same assignment and have made progress but I'm getting an error when I try to call my addFraction function and have no idea why I'm getting it (hopefully it's something stupid that I'm missing). I get syntax errors in the line where I try to call my addFraction function. The function defentition is:

C++ Syntax (Toggle Plain Text)
  1. void addFraction (int numerator1, int denominator1, int numerator2, int denominator2, int& numeratoranswer, int& denominatoranswer) {
  2.  
  3. // Get common donimator for fractions
  4. numerator1 = numerator1 * denominator2;
  5. denominator1 = denominator1 * denominator2;
  6. numerator2 = numerator2 * denominator1;
  7. denominator2 = denominator2 * denominator1;
  8.  
  9. // Perform addition on fractions
  10. numeratoranswer = numerator1 + numerator2;
  11. denominatoranswer = denominator1;
  12. }

the line where I get the error from trying to call the function is:
C++ Syntax (Toggle Plain Text)
  1. addFraction (int numerator1, int denominator1, int numerator2, int denominator2, int numeratoranswer, int denominatoranswer);

I get 3 errors when I try to compile:

Compiling...
lab04.cpp
C:\Documents and Settings\Scott\My Documents\CS 111\lab04.cpp(51) : error C2144: syntax error : missing ')' before type 'int'
C:\Documents and Settings\Scott\My Documents\CS 111\lab04.cpp(51) : error C2660: 'addFraction' : function does not take 0 parameters
C:\Documents and Settings\Scott\My Documents\CS 111\lab04.cpp(51) : error C2059: syntax error : ')'
Error executing cl.exe.

If you care to look at the whole programs source code:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cassert>
  3.  
  4. using namespace std;
  5.  
  6. // Prototypes for the functions in this program
  7. void getFraction (int& numerator, int& denominator);
  8. int gcd (int a, int b);
  9. void addFraction (int numerator1, int denominator1, int numerator2, int denominator2, int& numeratoranswer, int& denominatoranswer);
  10.  
  11. int main () {
  12.  
  13.  
  14. // Declare Character Variables
  15. char usercontinue='y';
  16. char operatorsymbol;
  17. char doagain;
  18.  
  19. // Declare Numerator and Denominator Variables
  20. int numerator1;
  21. int denominator1;
  22. int numerator2;
  23. int denominator2;
  24. int nummeratoranswer;
  25. int denominatoranswer;
  26.  
  27. do {
  28.  
  29. // Get operatorsymbol from user to use in switch statement, use do while to verify correct input
  30. do {
  31. cout << "Enter an operation(+,-,*,/): ";
  32. cin >> operatorsymbol;
  33.  
  34.  
  35. // Assign value to doagain variable and display error message if appropirate
  36. if ((operatorsymbol == '+') || (operatorsymbol == '-') || (operatorsymbol == '*') || (operatorsymbol == '/'))
  37. doagain = 'n';
  38. else {
  39. doagain = 'y';
  40. cout << "Error: Please input a correction operation symbol" << endl;
  41. }
  42.  
  43. } while (doagain == 'y');
  44.  
  45. // Prompt and get numerators and denominators to use in operation
  46. cout << "Enter a numerator and denominator of a fraction seperated by a space: ";
  47. getFraction (numerator1, denominator1);
  48. cout << "Enter a numerator and denominator of a fraction seperated by a space: ";
  49. getFraction (numerator2, denominator2);
  50.  
  51. addFraction (int numerator1, int denominator1, int numerator2, int denominator2, int numeratoranswer, int denominatoranswer);
  52.  
  53. /*/ Switch Statement to use correct function for operation type
  54.   switch (operatorsymbol) {
  55.  
  56.   case '+':
  57.  
  58.  addFraction (int numerator1, int denominator1, int numerator2, int denominator2, int numeratoranswer, int denominatoranswer);
  59.   break;
  60.  
  61.   } */
  62.  
  63. } while (usercontinue == 'Y' || usercontinue == 'y');
  64.  
  65. return 0;
  66. }
  67.  
  68. void getFraction (int& numerator, int& denominator) {
  69. cin >> numerator >> denominator;
  70.  
  71. }
  72.  
  73. int gcd (int a, int b) {
  74. assert (b !=0);
  75. int rem = a % b;
  76. while (rem !=0) {
  77. a = b;
  78. b = rem;
  79. rem = a % b;
  80. }
  81. return b;
  82. }
  83.  
  84. void addFraction (int numerator1, int denominator1, int numerator2, int denominator2, int& numeratoranswer, int& denominatoranswer) {
  85.  
  86. // Get common donimator for fractions
  87. numerator1 = numerator1 * denominator2;
  88. denominator1 = denominator1 * denominator2;
  89. numerator2 = numerator2 * denominator1;
  90. denominator2 = denominator2 * denominator1;
  91.  
  92. // Perform addition on fractions
  93. numeratoranswer = numerator1 + numerator2;
  94. denominatoranswer = denominator1;
  95. }

thanks again so much for the help,

-Scott
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skamen is offline Offline
10 posts
since Oct 2004
Oct 20th, 2004
0

Re: Help Writing Fuction to Read Fractions From User

Oh and the reason the switch statement is commented out is that I was trying to see if the problem somehow had something to do with the switch statement so I made the function call outside of the switch statement.

-Scott
Reputation Points: 10
Solved Threads: 0
Newbie Poster
skamen is offline Offline
10 posts
since Oct 2004
Oct 20th, 2004
0

Re: Help Writing Fuction to Read Fractions From User

You don't declare types in a function call:
C++ Syntax (Toggle Plain Text)
  1. addFraction (numerator1, denominator1, numerator2, denominator2, numeratoranswer, denominatoranswer);
And check your spelling, particularly for numeratoranswer.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: type casting
Next Thread in C++ Forum Timeline: How to write a program using a stack





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC