C++ functions with pointers - c++

What is the error? How to solve it? This code is to find the area of the circle using pointers and functions. The error I am facing is that the &answer cannot be converted into float.
#include <iostream>
using namespace std;
void area(float, float);
int main()
{
float radius, answer = 0.0;
cout << "Enter a radius:"; // Take the radius from the user.
cin >> radius;
area(&radius, &answer);
cout << "Area of circle is:" << answer;
return 0;
}
void area(float *value, float *result) // This is the function to calculate the area.
{
*result = 3.142 * (*value) * (*value);
}

You can do one of the 2 things:
Change the prototype to void area(float*, float*);
Remove the prototype and move the function:
void area(float *value, float *result) // This is the function to calculate the area.
{
*result = 3.142 * (*value) * (*value);
}
above the main() function. Either of these will work.

Related

I was wondering why my calc() function didn't showed up in the output

// I am trying to calculate the initial cost, current cost, and profit in this program. But somehow the calc() function wasn't working.
#include<iostream>
using namespace std;
int main()
{
int shares;
cout<<"Enter the number of shares: ";
cin>>shares;
cout<<shares<<endl;
int buy;
cout<<"Enter the Price you bought in: $";
cin>>buy;
cout<<buy<<endl;
int current;
cout<<"Enter the current price: ";
cin>>current;
cout<<current<<endl;
void calc(float shares, float current, float buy, float &total, float &currentval, float &profit);
return 0;
}
void calc(float shares, float current, float buy, float &total, float &currentval, float &profit)
{
total=shares*buy;
currentval=shares*current;
profit=currentval-total;
cout<<"The total is $"<<total<<endl;
cout<<"The current value is $"<<currentval<<endl;
cout<<"The profit is $"<<profit<<endl;
}
The code you posted does not call the function calc. The line in the main that look like a call, is declaring a function, but it is never called.
void calc(float shares, float current, float buy, float &total, float &currentval, float &profit);
musst be replaced by:
float total,currentval,profit;
calc(shares, current, buy,total, currentval,profit);
Before calling function, you need declare it. For example
#include <iostream>
int calc(int arg1, int arg2); //this is declaring of function calc, which gas two int arguments and returns integer
int main()
{
//some your code
std::cout << calc(2, 3); //this prints 10
}
int calc(int arg1, int arg2) //here you define what does this function do
{
return arg1 * (arg1 + arg2); //something random calculating
};
You declare your calc function after main, but you need to do it before.

I am getting an error in typecasting, please help me out with this

Create a class Rectangle. This class has attributes length and width each of which defaults to 1. It has methods that calculate the perimeter and area of the rectangle. It has set and get methods for both length and width. The set methods should verify that length and width are floating – point nos larger than 0.0 and less than 20.0.
#include<iostream>
using namespace std;
class rect
{
float l;
float w;
public:
void setlw();
float getl(float len);
float getw(float width);
void seta();
void setp();
};
void rect:: setlw()
{
cout<<"enter the lenght and width"<<endl;
cin>>l>>w;
}
float rect:: getl(float len)
{
if (l>=0.0 && l<=20.0)
len=l;
else
len=1.0;
return(len);
}
float rect:: getw(float width)
{
if(w>=0 && w<=20.0)
width=w;
else
width=1.0;
return(width);
}
void rect::seta()
{
float a;
a=l*w;
cout<<"the area is"<<a<<endl;
}
void rect:: setp()
{
float p;
p=2*(l+w);
cout<<"the perimeter is"<<p<<endl;
}
int main()
{
rect r;
r.setlw();
cout<<"length is"<<r.getl(float)<<endl;
cout<<"width is"<<r.getw(float)<<endl;
r.seta();
r.setp();
return (0);
}
I have corrected your code. Removed the input parameters for getl() and getw(), as it's not required when you are taking input with setlw() . You've also not declared variables len and width in functions getl and getw.
New Code:
#include<iostream>
using namespace std;
class rect
{
float l;
float w;
public:
void setlw();
float getl();
float getw();
void seta();
void setp();
};
void rect::setlw()
{
cout<<"enter the lenght and width"<<endl;
cin>>l>>w;
}
float rect::getl()
{ float len;
if (l>=0.0 && l<=20.0)
len=l;
else
len=1.0;
return(len);
}
float rect::getw()
{ float width;
if(w>=0 && w<=20.0)
width=w;
else
width=1.0;
return(width);
}
void rect::seta()
{
float a;
a=l*w;
cout<<"the area is"<<a<<endl;
}
void rect::setp()
{
float p;
p=2*(l+w);
cout<<"the perimeter is"<<p<<endl;
}
int main()
{
rect r;
r.setlw();
cout<<"length is"<<r.getl()<<endl;
cout<<"width is"<<r.getw()<<endl;
r.seta();
r.setp();
return (0);
}

How to solve "undeclared identifier" and "redefinition of formal parameter"?

I'm working on a programme to calculate average acceleration and i use 3 function ( by pass reference method) after writing my code this error happens
and "error C2082: redefinition of formal parameter 'Vo'".I've google it and i barely understand it.Can anyone explain to me why this happens and how to solve this?thank you for helping
/* lab assessment 4 kiraan pecutan*/
#include <iostream>
using namespace std;
void data(double& Vo,double& Vt,double& t);
void calculate(double& sum);
void output(double& out);
double Vo,Vt,t,sum,out;
int main()
{
cout<<"please enter your velocity(Vo=m/s)\n,velocity(Vt=m/s)\nand time(s=second)\n\n";
data(Vo,Vt,t);
calculate(sum);
output( out);
return 0;
}
void data(double& Vo,double& Vt,double& t)
{
double Vo,Vt,t;
cin>>Vo;
cin>>Vt;
cin>>t;
cout<<"your Vo="<<Vo<<" ,Vt="<<Vt<<" and T="<<t<<"\n\n";
}
void calculate(double& sum )
{
double Vt,Vo,t;
sum=(Vt-Vo)/t;
}
void output(double& out)
{
double sum;
cout<<"the acceleration ="<<sum;
}
You declare variables with the same name multiple times. Each variable defined by its name should be declared exactly ones. It is not allowed to use the same variable name e.g. as a function parameter and a variable name in the function body, e.g.
void data(double &Vo) {
double Vo = 0.0; // Vo already exists with type double&
// do something
}
Please consider the following hints:
do not use global variables unless necessary, they are hard to debug in big projects
always initialize variables of basic types (int, float, double, ...) with a value otherwise they get a 'random' one
The following code should compile without errors, even though the semantic/computation maybe wrong. I just used your implementation.
#include <iostream>
using namesace std;
//--------------------------------------------------------------------------//
void data(double &Vo, double &Vt, double &t);
void calculate(double &Vo, double &Vt, double &t, double &sum);
void output(double &out);
//--------------------------------------------------------------------------//
int main() {
double Vo = 0.0;
double Vt = 0.0;
double t = 0.0;
double sum = 0.0;
double out = 0.0;
cout << "please enter your velocity(Vo=m/s)\n,velocity(Vt=m/s)\nand time(s=second)\n\n";
data(Vo, Vt, t);
calculate(Vo, Vt, t, sum);
output(out);
return 0;
}
//--------------------------------------------------------------------------//
void data(double &Vo, double &Vt, double &t) {
cin >> Vo;
cin >> Vt;
cin >> t;
cout << "your Vo=" << Vo << " ,Vt=" << Vt << " and T=" << t << "\n\n";
}
//--------------------------------------------------------------------------//
void calculate(double &Vo, double &Vt, double &t, double &sum) {
sum = (Vt - Vo) / t;
}
//--------------------------------------------------------------------------//
void output(double &out) {
cout << "the acceleration =" << out;
}

error: ‘cylinder’ was not declared in this scope

I wrote code in C++ in Xcode and receive:
error: ‘cylinder’ was not declared in this scope
Header file cylinder.h:
#include <iostream>
#ifndef cylinder_h
#define cylinder_h
#endif
#include "stdio.h"
using namespace std;
class cylinder
{
public:
// Constructors
cylinder();
cylinder(double r, double h);
// Accessors
double getRadius();
double getHeight();
void setRadius(double r);
void setHeight(double h);
double area();
double volume();
void write(std::ostream& output);
private:
double radius;
double height;
};
cylinder.cpp :
#include "cylinder.h"
double PI = 3.1415926535898;
#include "stdio.h"
using namespace std;
// Constructors
cylinder::cylinder()
{
radius = 0;
height = 0;
}
cylinder::cylinder(double r, double h)
{
radius = r;
height = h;
}
// Accessors
double cylinder::getRadius()
{
return radius;
}
double cylinder::getHeight()
{
return height;
}
// Setters
void cylinder::setRadius(double r)
{
radius = r;
}
void cylinder::setHeight(double h)
{
height = h;
}
// Calculations
double cylinder::area()
{
return 2 * PI * radius * radius + 2 * PI * radius * height;
}
double cylinder::volume()
{
return PI * radius * radius * height;
}
main.cpp:
#include <iostream>
using namespace std;
#include <string>
#include "cylinder.h"
#include <iomanip>
#include "sphere.h"
#include "prism.h"
#include "cone.h"
#include "pyramid.h"
#include <cstdlib>
int main()
{
double radius, height,sradius,length,width,rheight,cheight,cradius,pheight,plength;
cout << "Enter cylinder height and radius >>> ";
cin >> height >> radius;
cylinder one (radius, height);
cout << "The cylinder volume is " << setprecision(2)<<fixed<<one.volume () << endl;
cout << "The cylinder surface area is " << setprecision(2)<<fixed<<one.area () << endl;
cout <<"CYLINDER: "<<height<<", "<<radius<<endl;
}
I have been stuck for two days. I am so confused. I've already defined cylinder class, and I've tried many ways on the website. Is there anyone who can help me?
It's due tonight!
Sadly a method to try to find the answer and not the answer. It has been posted as an answer because I don't think I can fit it all in the bounds of a comment.
I have removed that which has not been provided, corrected the use if the include guard, shuffled a few things around, and commented out that which was not needed. Hopefully I have left a good enough explanation of what I did and why. If not, ask.
This compiles. I have not tested the logic.
What to do with it:
In main.cpp there are a bunch of files that were included but not provided. To get a working base, I have commented them out. Add them and rebuild the program one by one until the program stops compiling. If this does not make the problem obvious, it has at least reduced the search area.
Revised cylinder.h
// two lines below are an include guard. It prevents a header file from being included
// multiple times, heading off potentially recursive includes (a loop that
// causes the compiler to go forever) and chaos caused by redefining the same
// stuff multiple times.
#ifndef cylinder_h
#define cylinder_h
#include <iostream>
//#include "stdio.h" unused and should be #include <cstdio> when used in C++
//using namespace std; unused and very dangerous.
class cylinder
{
public:
// Constructors
cylinder();
cylinder(double r, double h);
// Accessors
double getRadius();
double getHeight();
void setRadius(double r);
void setHeight(double h);
double area();
double volume();
void write(std::ostream& output);
private:
double radius;
double height;
};
#endif // end of include guard moved to here
Revised cylinder.cpp
#include "cylinder.h"
double PI = 3.1415926535898;
//#include "stdio.h" not used
//using namespace std; dangerous and not used.
// Constructors
cylinder::cylinder()
{
radius = 0;
height = 0;
}
cylinder::cylinder(double r, double h)
{
radius = r;
height = h;
}
// Accessors
double cylinder::getRadius()
{
return radius;
}
double cylinder::getHeight()
{
return height;
}
// Setters
void cylinder::setRadius(double r)
{
radius = r;
}
void cylinder::setHeight(double h)
{
height = h;
}
// Calculations
double cylinder::area()
{
return 2 * PI * radius * radius + 2 * PI * radius * height;
}
double cylinder::volume()
{
return PI * radius * radius * height;
}
Revised main.cpp
#include <iostream>
//#include <string> not needed
#include "cylinder.h"
#include <iomanip>
// the following headers were not provided and may be containing bad code that
// breaks the OP's build. No way to tell. Add one and rebuild. If the program still
//compiles, the problem is likely elsewhere so add another and rebuild.
//#include "sphere.h"
//#include "prism.h"
//#include "cone.h"
//#include "pyramid.h"
//#include <cstdlib> not used
//using namespace std; used but use with caution. Instead, use only the pieces you need
using std::cout;
using std::cin;
using std::setprecision;
using std::fixed;
using std::endl;
// or explicitly state the namespace at each use.
// Eg. std::cout << "blah blah blah" << std::endl;
int main()
{
double radius, height;//,sradius,length,width,rheight,cheight,cradius,pheight,plength;
cout << "Enter cylinder height and radius >>> ";
cin >> height >> radius;
cylinder one (radius, height);
cout << "The cylinder volume is " << setprecision(2)<<fixed<<one.volume () << endl;
cout << "The cylinder surface area is " << setprecision(2)<<fixed<<one.area () << endl;
cout <<"CYLINDER: "<<height<<", "<<radius<<endl;
}
try to put #endif at the end of the header file
I think it's because of your multiples "using namespace std" , use only one , that one in the header file , and be sure to put it after all #includes
Here's your original code in a form that compiles and runs on my gnu/linux system. I tried to not to make many changes. I think you'll benefit from comparing your old code with new code to see what the minimal changes are, this may clear up some things for you.
After that, I show a more cleaned up version of the code, again I haven't really corrected it, especially the style and features used, but I've just tried to cut away a lot of the unnecessary things. I think realising what isn't neeed may also clear up some issues for you.
Code with minimal changes:
cylinder.h:
#include <iostream>
#ifndef cylinder_h
#define cylinder_h
#endif
#include "stdio.h"
using namespace std;
class cylinder
{
public:
// Constructors
cylinder();
cylinder(double r, double h);
// Accessors
double getRadius();
double getHeight();
void setRadius(double r);
void setHeight(double h);
double area();
double volume();
void write(std::ostream& output);
private:
double radius;
double height;
};
cylinder.cpp:
#include "cylinder.h"
double PI = 3.1415926535898;
#include "stdio.h"
using namespace std;
// Constructors
cylinder::cylinder()
{
radius = 0;
height = 0;
}
cylinder::cylinder(double r, double h)
{
radius = r;
height = h;
}
// Accessors
double cylinder::getRadius()
{
return radius;
}
double cylinder::getHeight()
{
return height;
}
// Setters
void cylinder::setRadius(double r)
{
radius = r;
}
void cylinder::setHeight(double h)
{
height = h;
}
// Calculations
double cylinder::area()
{
return 2 * PI * radius * radius + 2 * PI * radius * height;
}
double cylinder::volume()
{
return PI * radius * radius * height;
}
main.cpp:
#include <iostream>
using namespace std;
#include <string>
#include "cylinder.h"
#include <iomanip>
#include <cstdlib>
int main()
{
double radius, height,sradius,length,width,rheight,cheight,cradius,pheight,plength;
cout << "Enter cylinder height and radius >>> ";
cin >> height >> radius;
cylinder one (radius, height);
cout << "The cylinder volume is " << setprecision(2)<<fixed<<one.volume () << endl;
cout << "The cylinder surface area is " << setprecision(2)<<fixed<<one.area () << endl;
cout <<"CYLINDER: "<<height<<", "<<radius<<endl;
}
Here's the code with a bit more cleanup:
cylinder.h:
#ifndef cylinder_h
#define cylinder_h
#include <iostream>
class cylinder
{
public:
// Constructors
cylinder();
cylinder(double r, double h);
// Accessors
double getRadius();
double getHeight();
void setRadius(double r);
void setHeight(double h);
double area();
double volume();
void write(std::ostream& output);
private:
double radius;
double height;
};
#endif
cylinder.cpp:
#include "cylinder.h"
static const double PI = 3.1415926535898;
// Constructors
cylinder::cylinder() : radius(0.0), height(0.0)
{
}
cylinder::cylinder(double r, double h) : radius(r), height(h)
{
}
// Accessors
double cylinder::getRadius()
{
return radius;
}
double cylinder::getHeight()
{
return height;
}
// Setters
void cylinder::setRadius(double r)
{
radius = r;
}
void cylinder::setHeight(double h)
{
height = h;
}
// Calculations
double cylinder::area()
{
return 2 * PI * radius * radius + 2 * PI * radius * height;
}
double cylinder::volume()
{
return PI * radius * radius * height;
}
main.cpp:
#include <iostream>
#include <string>
#include <iomanip>
#include "cylinder.h"
using namespace std;
int main()
{
double radius, height;
cout << "Enter cylinder height and radius >>> ";
cin >> height >> radius;
cylinder one(radius, height);
cout << "The cylinder volume is " << setprecision(2) << fixed << one.volume() << endl;
cout << "The cylinder surface area is " << setprecision(2) << fixed << one.area() << endl;
cout << "CYLINDER: " << height << ", " << radius << endl;
}

Have shape and circle class, and point class. Segmentation fault when creating circle with point class as one parameter

Here is my Shape.h. Ignore all the code that is commented out. That is from a version that I believe was incorrect but I left it in there in case I was wrong.
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <math.h>
#include "Point.h"
using namespace std;
class Shape {
public:
Shape() {}
virtual ~Shape() {}
/*
virtual float calcArea(const Shape& s) const = 0;
virtual float calcCircum(const Shape& s) const = 0;
virtual string calcBox(const Shape& s) const = 0;
virtual void display(const Shape& s) const = 0;
*/
virtual float calcArea() const = 0;
virtual float calcCircum() const = 0;
virtual string calcBox() const = 0;
virtual void display() const = 0;
};
class Circle : public Shape {
public:
int radius;
int pointX;
int pointY;
Point *middlePoint;
float PI;
Circle() : Shape() {
middlePoint = new Point(0,0);
radius = 0;
}
~Circle() {}
Circle(int rad, Point& p) : Shape() {
PI = 3.141592;
*middlePoint = p;
pointX = p.getX();
pointY = p.getY();
radius = rad;
}
// float calcArea(const Circle& s) const {
float calcArea() const {
float tempArea;
// tempArea = PI * s.radius * s.radius;
tempArea = PI * radius * radius;
return tempArea;
}
// float calcCircum(const Circle& s) const {
float calcCircum() const {
// int diameter = 2 * s.radius;
int diameter = 2 * radius;
float tempCircum;
tempCircum = PI * diameter;
return tempCircum;
}
// string calcBox(const Circle& s) const {
string calcBox() const {
// int x = s.pointX;
// int y = s.pointY;
// int r = s.radius;
int x = pointX;
int y = pointY;
int r = radius;
int tlX = x - r;
int tlY = y + r;
int blX = x - r;
int blY = y - r;
int trX = x + r;
int trY = y + r;
int brX = x + r;
int brY = y - r;
Point *topLeft = new Point(tlX,tlY);
Point *bottomLeft = new Point(blX,blY);
Point *topRight = new Point(trX,trY);
Point *bottomRight = new Point(brX,brY);
stringstream output;
string tempOut;
output << *topLeft << *bottomLeft << *topRight << *bottomRight;
tempOut = output.str();
return tempOut;
}
// void display(const Circle& s) const {
void display() const {
cout << "Class Name: Circle" << endl;
// float tmpArea = calcArea(s);
float tmpArea = calcArea();
cout << "Area = " << tmpArea << endl;
// cout << "Radius = " << s.radius << endl;
cout << "Radius = " << radius << endl;
// float tmpCircum = calcCircum(s);
float tmpCircum = calcCircum();
cout << "Circumference = " << tmpCircum << endl;
cout <<"Middle Point = " << middlePoint;
// string bbox = calcBox(s);
string bbox = calcBox();
cout <<"Bounding Box Points = " << bbox;
}
};
Here is my TMA4Question1.cpp code.
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <math.h>
#include "Shape.h"
int main() {
Point *circlePoint = new Point(10,-5);
Circle *mainCircle = new Circle(23,*circlePoint);
}
Ok. Yes this is a homework assignment for University. I'm not looking just for the answer, I would like to know why this program gives me a segmentation fault and how to correct it.
I know the error is in the Circle code, where I pass a pointer to the circlePOint in the constructor for the Circle class. I dont know why it generates a seg fault. I hope someone can provide some insight. Thanks.
Sorry if the code is messy. Had a hard time pasting it into here properly with 4 spaces and all that.
middlePoint is not allocated in your second Circle constructor. You are assigning a value to it before giving it some memory. As an aside, I don't see why anything there needs to be a pointer.
Why do you use pointers to Points inside your classes at all? You only generate memory leaks this way and (without your own copy operations) cause problems with as the midpoints could be shared by different circles.
PS: And it's not needed to have a PI value (even as non-const) in every circle - just use the constant from (afair) cmath for it.