C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 17th, 2004, 08:05 PM
skamen skamen is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 9 skamen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old October 17th, 2004, 08:53 PM
m3rajk m3rajk is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 24 m3rajk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #3  
Old October 20th, 2004, 12:41 AM
skamen skamen is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 9 skamen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 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:

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

Reply With Quote
  #4  
Old October 22nd, 2004, 05:42 AM
Buster77 Buster77 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 28 Buster77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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,...);

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help Writing a Function that Will Get Fractions From the User


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT