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

>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.

>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

>so it's not quite so easy
Sure it is. The way cin's >> operator handles whitespace makes it easy:

#include <iostream>

using namespace std;

int main()
{
  int num, den;

  cout<<"Enter a fraction: ";
  cin>> num >> den;

  cout<< num <<'/'<< den <<endl;
}

The really nifty part is that you can break up that call into two calls and it works the same way:

#include <iostream>

using namespace std;

int main()
{
  int num, den;

  cout<<"Enter a fraction: ";
  cin>> num;
  cin>> den;

  cout<< num <<'/'<< den <<endl;
}

Wrapping everything in a function is trivial as well:

#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;
}

Sorry, this is off topic but do you happen to go to Western Michigan U?

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

Yeah, I do go to Western Michigan. Thanks for the help.

-Scott

Boo and hiss. "Proud" Chippewa here. :-|

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:

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:

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:

#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

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

You don't declare types in a function call:

addFraction (numerator1, denominator1, numerator2, denominator2, numeratoranswer, denominatoranswer);

And check your spelling, particularly for numeratoranswer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.