6 Replies - 4468 Views - Last Post: 15 April 2009 - 07:20 PM Rate Topic: -----

#1 smms25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 02-March 09

Using a Function to Return Area, Perimeter

Posted 15 April 2009 - 06:52 PM

okay , so i have no clue how to use a function in c++, this program is supposed to ask for the length and width of the rectangle (this is done in the main function... i used cin>>length,etc.) .... the function should return area and perimeter of the rectangle.... any help appreciated

#include "stdafx.h"
#include "iostream"
using namespace std;

double areaRectangle (double length, double width);
void perimfRectangle (double length, double width);

int main(void)
{
	double length,width,area,perimeter;
	cin>>length;
	cin>>width;
	area=areaRectangle(double length, double width);
	cout<<"\n Area of Rectangle="<<area;
	perimfrectangle(double length, double width);
	cout<<"\n Perimeter of Rectangle="<<perimf;
	cout<<endl;
	cin.get();
	return 0;
}

double areaRectangle(double length,double width);
{
	return length*width;
}
void perimfRectangle(double length,double width);
{
	return (2*length+2*width);
}


This post has been edited by smms25: 15 April 2009 - 06:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using a Function to Return Area, Perimeter

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1647
  • View blog
  • Posts: 8,510
  • Joined: 07-September 06

Re: Using a Function to Return Area, Perimeter

Posted 15 April 2009 - 07:01 PM

Well, you have a few errors.

First:
void perimfRectangle(double length,double width);
{
	return (2*length+2*width);
}


can only return void, as it has void as the return type. Returning anything other than void will cause an error.

You will need to change it to something like so:
double perimfRectangle(double length,double width);
{
	return (2*length+2*width);
}


and remember to update your function definition above main as well.

Second, functions are case sensitive.
	perimfrectangle(double length, double width);

will not work.

Third, primf is never declared.
	cout<<"\n Perimeter of Rectangle="<<perimf;

So you are trying to output a variable that was never created.

So there are a few things to fix, then it should act better.

HTH
Was This Post Helpful? 0
  • +
  • -

#3 BlakeJustBlake   User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Using a Function to Return Area, Perimeter

Posted 15 April 2009 - 07:02 PM

On the top, take out the lengths and widths and leave the doubles. In main take out the doubles and leave the lengths and widths. At the bottom, leave it like it is.
Was This Post Helpful? 0
  • +
  • -

#4 smms25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 02-March 09

Re: Using a Function to Return Area, Perimeter

Posted 15 April 2009 - 07:11 PM

okay so i fix some of the errors that the c++ gave back however, im getting this error--- '{' : missing function header (old-style formal list?), where
double areaRectangle(double length,double width);
{
	return length*width;}
double perimf(double length,double width);
{
	return (2*length+2*width);
}


as well as error C2064: term does not evaluate to a function taking 2 arguments
this error when i declare perimeter=...
---
#include "stdafx.h"
#include "iostream"
using namespace std;

double areaRectangle (double length, double width);
double perimf(double length, double width);

int main(void)
{
	double length,width,area,perimeter,perimf;
	cin>>length;
	cin>>width;
	area=areaRectangle(length,width);
	cout<<"\n Area of Rectangle="<<area;
	perimeter=perimf(length,width);
	cout<<"\n Perimeter of Rectangle="<<perimf;
	cout<<endl;
	cin.get();
	return 0;
}

double areaRectangle(double length,double width);
{
	return length*width;}
double perimf(double length,double width);
{
	return (2*length+2*width);
}

Was This Post Helpful? 0
  • +
  • -

#5 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1647
  • View blog
  • Posts: 8,510
  • Joined: 07-September 06

Re: Using a Function to Return Area, Perimeter

Posted 15 April 2009 - 07:17 PM

Look at the variables you are declaring:
double length,width,area,perimeter,perimf;

And on this line:
	cout<<"\n Perimeter of Rectangle="<<perimf;

You are using perimf as a function as a variable. You can't have a variable and function, or class, or struct with the same name.

Just delete the variables perimf, and it should be happier.

HTH
Was This Post Helpful? 0
  • +
  • -

#6 BlakeJustBlake   User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Using a Function to Return Area, Perimeter

Posted 15 April 2009 - 07:19 PM

The first one is because you have semicolons after your parameters on those functions, there shouldn't be.

The second one is because in main you declare a double called perimf, and you can't overload names like that.
Was This Post Helpful? 0
  • +
  • -

#7 smms25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 02-March 09

Re: Using a Function to Return Area, Perimeter

Posted 15 April 2009 - 07:20 PM

View Postsmms25, on 15 Apr, 2009 - 06:11 PM, said:

okay so i fix some of the errors that the c++ gave back however, im getting this error--- '{' : missing function header (old-style formal list?), where
double areaRectangle(double length,double width);
{
	return length*width;}
double perimf(double length,double width);
{
	return (2*length+2*width);
}


as well as error C2064: term does not evaluate to a function taking 2 arguments
this error when i declare perimeter=...
---
#include "stdafx.h"
#include "iostream"
using namespace std;

double areaRectangle (double length, double width);
double perimf(double length, double width);

int main(void)
{
	double length,width,area,perimeter,perimf;
	cin>>length;
	cin>>width;
	area=areaRectangle(length,width);
	cout<<"\n Area of Rectangle="<<area;
	perimeter=perimf(length,width);
	cout<<"\n Perimeter of Rectangle="<<perimf;
	cout<<endl;
	cin.get();
	return 0;
}

double areaRectangle(double length,double width);
{
	return length*width;}
double perimf(double length,double width);
{
	return (2*length+2*width);
}



sorry for the extra post , working on my issues now
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1