| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help Writing a Function that Will Get Fractions From the User
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/CS111FALL04/lab04/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 |
|
#2
|
|||
|
|||
|
have cin parse the string for two numbers. then convert the strings to two ints. beyond saying this i know i'll be doing your homework. and even saying this much is iffy
|
|
#3
|
|||
|
|||
|
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:
Code:
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: Code:
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: Code:
#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 |
|
#4
|
|||
|
|||
|
I assume you know this, but can't see the forest for the trees. When you call a function, you must use a previously declared variable, you cannot declare (or redeclare) in the call:
addFraction (INT numerator, INT denominator,...); should be: addFraction (numerator, denominator,...); |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help Writing a Function that Will Get Fractions From the User |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|