| | |
Help Writing Fuction to Read Fractions From User
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 10
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Oct 2004
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
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.
thanks anways though,
-Scott
>so it's not quite so easy
Sure it is. The way cin's >> operator handles whitespace makes it easy:
The really nifty part is that you can break up that call into two calls and it works the same way:
Wrapping everything in a function is trivial as well:
Sure it is. The way cin's >> operator handles whitespace makes it easy:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int num, den; cout<<"Enter a fraction: "; cin>> num >> den; cout<< num <<'/'<< den <<endl; }
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int num, den; cout<<"Enter a fraction: "; cin>> num; cin>> den; cout<< num <<'/'<< den <<endl; }
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; bool get_fraction ( int& num, int& den ) { return cin>> num >> den; } int main() { int num, den; cout<<"Enter a fraction: "; if ( !get_fraction ( num, den ) ) cerr<<"Invalid input"<<endl; else cout<< num <<'/'<< den <<endl; }
I'm here to prove you wrong.
•
•
Join Date: Oct 2004
Posts: 10
Reputation:
Solved Threads: 0
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:
the line where I get the error from trying to call the function is:
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:
thanks again so much for the help,
-Scott
C++ Syntax (Toggle Plain Text)
void addFraction (int numerator1, int denominator1, int numerator2, int denominator2, int& numeratoranswer, int& denominatoranswer) { // Get common donimator for fractions numerator1 = numerator1 * denominator2; denominator1 = denominator1 * denominator2; numerator2 = numerator2 * denominator1; denominator2 = denominator2 * denominator1; // Perform addition on fractions numeratoranswer = numerator1 + numerator2; denominatoranswer = denominator1; }
the line where I get the error from trying to call the function is:
C++ Syntax (Toggle Plain Text)
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)
#include <iostream> #include <cassert> using namespace std; // Prototypes for the functions in this program void getFraction (int& numerator, int& denominator); int gcd (int a, int b); void addFraction (int numerator1, int denominator1, int numerator2, int denominator2, int& numeratoranswer, int& denominatoranswer); int main () { // Declare Character Variables char usercontinue='y'; char operatorsymbol; char doagain; // Declare Numerator and Denominator Variables int numerator1; int denominator1; int numerator2; int denominator2; int nummeratoranswer; int denominatoranswer; do { // Get operatorsymbol from user to use in switch statement, use do while to verify correct input do { cout << "Enter an operation(+,-,*,/): "; cin >> operatorsymbol; // Assign value to doagain variable and display error message if appropirate if ((operatorsymbol == '+') || (operatorsymbol == '-') || (operatorsymbol == '*') || (operatorsymbol == '/')) doagain = 'n'; else { doagain = 'y'; cout << "Error: Please input a correction operation symbol" << endl; } } while (doagain == 'y'); // Prompt and get numerators and denominators to use in operation cout << "Enter a numerator and denominator of a fraction seperated by a space: "; getFraction (numerator1, denominator1); cout << "Enter a numerator and denominator of a fraction seperated by a space: "; getFraction (numerator2, denominator2); addFraction (int numerator1, int denominator1, int numerator2, int denominator2, int numeratoranswer, int denominatoranswer); /*/ Switch Statement to use correct function for operation type switch (operatorsymbol) { case '+': addFraction (int numerator1, int denominator1, int numerator2, int denominator2, int numeratoranswer, int denominatoranswer); break; } */ } while (usercontinue == 'Y' || usercontinue == 'y'); return 0; } void getFraction (int& numerator, int& denominator) { cin >> numerator >> denominator; } int gcd (int a, int b) { assert (b !=0); int rem = a % b; while (rem !=0) { a = b; b = rem; rem = a % b; } return b; } void addFraction (int numerator1, int denominator1, int numerator2, int denominator2, int& numeratoranswer, int& denominatoranswer) { // Get common donimator for fractions numerator1 = numerator1 * denominator2; denominator1 = denominator1 * denominator2; numerator2 = numerator2 * denominator1; denominator2 = denominator2 * denominator1; // Perform addition on fractions numeratoranswer = numerator1 + numerator2; denominatoranswer = denominator1; }
thanks again so much for the help,
-Scott
You don't declare types in a function call:
And check your spelling, particularly for numeratoranswer.
C++ Syntax (Toggle Plain Text)
addFraction (numerator1, denominator1, numerator2, denominator2, numeratoranswer, denominatoranswer);
I'm here to prove you wrong.
![]() |
Other Threads in the C++ Forum
- Previous Thread: type casting
- Next Thread: How to write a program using a stack
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






