//I want to use 3 functions here with 1 struct that one function do the input
and one for calculation and the other one for out put but
I don't know how to reference //the variables in functions rec2 and rec3. I want to do it without using pointer
struct rectangle {
float length;
float width;
float area,perimeter;
};
rectangle rec1();
rectangle rec2();
rectangle rec3();
int main(){
rectangle f;
f = rec1();
f=rec2();
f = rec3();
return 0;
}
rectangle rec1(){
rectangle h;
cout<<"insert the length: ";
cin>>h.length;
cout<<"\ninsert width: ";
cin>>h.width;
return h;
}
rectangle rec2(){
rectangle z;
z.area=z.length*z.width;
z.perimeter=2*(z.length+z.width);
return z;
}
rectangle rec3(){
rectangle x;
cout<<"\narea is: "<<x.area<<endl<<"perimeter is: "<<x.perimeter<<endl;
return x;
}
You need to add methods to your rectangle struct.
struct rectangle
{
float length, width, area, perimeter;
void Input()
{
cout << "insert the length: ";
cin >> length;
cout << "\ninsert width: ";
cin>> width;
}
void Process(); // etc
void Output(); // etc
};
// Create a rectangle object and call it's methods
int main()
{
rectangle r;
r.Input();
r.Process();
r.Output()
}
The methods can now reference the member variables of the struct.
I suggest you change your design.
Place the input, process and output functions as methods inside the rectangle structure.
Placing the functions inside the structure allows them to access the data members.
Every time you return a new rectangle and assign it to your f variable, you are overwriting all of f's members, not just the ones you modified inside the function. You need to change the functions to modify f directly instead. You don't have to use a pointer for that, you can use a reference instead:
struct rectangle {
float length;
float width;
float area, perimeter;
};
void rec1(rectangle&);
void rec2(rectangle&);
void rec3(rectangle&);
int main(){
rectangle f;
rec1(f);
rec2(f);
rec3(f);
return 0;
}
void rec1(rectangle &r){
cout << "insert the length: ";
cin >> r.length;
cout << endl << "insert width: ";
cin >> r.width;
}
void rec2(rectangle &r){
r.area = r.length * r.width;
r.perimeter = 2 * (r.length + r.width);
}
void rec3(rectangle &r){
cout << endl << "area is: " << r.area << endl << "perimeter is: " << r.perimeter << endl;
}
But, this is C++ we are talking about, afterall. Member methods are your friends :)
struct rectangle {
float length;
float width;
float area, perimeter;
void rec1();
void rec2();
void rec3();
};
void rectangle::rec1(){
cout << "insert the length: ";
cin >> length;
cout << endl << "insert width: ";
cin >> width;
}
void rectangle::rec2(){
area = length * width;
perimeter = 2 * (length + width);
}
void rectangle::rec3(){
cout << endl << "area is: " << area << endl << "perimeter is: " << perimeter << endl;
}
int main(){
rectangle f;
f.rec1();
f.rec2();
f.rec3();
return 0;
}
Related
Whenever I ask for the area or perimeter, it returns some absurd values.
I tried fixing it for 2 days but it is the same!
The code and the class is given below with the output!
I expect the output to be 100 but it shows
252134578 <<<< this
maincode :
#include <iostream>
#include "rect_class.hpp"
using namespace std;
int main()
{
rectangle rect;
int width= 10, height = 10, choice, newwidth, newheight;
bool loop = false;
while(loop == false){
cout << endl << endl;
cout << " *** Menu *** " << endl;
cout << "(1) Draw Rectangle" << endl;
cout << "(2) Area" << endl;
cout << "(3) Perimeter" << endl;
cout << "(4) Resize" << endl;
cout << "(5) Quit" << endl;
cout << "Enter your choice :";
cin >> choice;
cout << endl;
switch(choice){
case 2 :cout << rect.getArea();
break;
case 3 : cout << rect.getPerimeter();
break;
case 4 : cout << "enter your height : ";
cin >> newheight;
cout << "enter your width : ";
cin >> newwidth;
rect.setHeight(newheight);
rect.setWidth(newwidth);
break;
case 5 : loop = true;
cout << "exiting...";
break;
default: cout << "bro type the menu nums !!";
break;
}
};
rect_class.hpp
class rectangle {
public :
int getHeight() const {return itsHeight;} //accessors
int getWidth() const {return itsWidth;}
void setHeight(int height){itsHeight = height;}
void setWidth(int width){itsWidth = width;}
int getArea();
int getPerimeter();
private :
int itsHeight;
int itsWidth;
};
int rectangle::getArea(){
return itsWidth*itsHeight;
};
int rectangle::getPerimeter(){
return 2*(itsWidth*itsHeight);
};
I am in the beginning years of my programming journey,so sorry for any silly mistakes! :-)
Your rectangle class members are not initialized. You set values for variables int width= 10, height = 10, but don't pass it to the rectangle class constructor.
Change this code:
rectangle rect;
int width= 10, height = 10, choice, newwidth, newheight;
To this:
int choice=0, newwidth=0, newheight=0; //always initialize variables!
rectangle rect(10, 10); //create rectangle with 10, 10
Now you need to add constructors to the rectangle class:
class rectangle {
public:
rectangle() = delete; //we don't need it anymore
rectangle(int width = 0, int height = 0) : itsHeight(height), itsWidth(width ) { }
//... rest of your code
This constructor allows you to create rectangle with given parameters or just create with default parameters (0,0).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
#include <iostream>
#include <string>
using namespace std;
/*----------------RECTANGLE CLASS----------------*/
class rectangle {
int w, h;
public:
void setVals(int, int);
int area();
int perimeter();
};
void rectangle::setVals( int x, int y) {
w = x;
h = y;
}
int rectangle::area() {
return w * h;
}
int rectangle::perimeter() {
return (2 * w) + (2 * h);
}
/*----------------CIRCLE CLASS----------------*/
class circle {
double pi = 3.14159265358979;
double r;
void setR(double);
double area();
double circumference();
};
void circle::setR(double radius) {
r = radius;
}
double circle::area() {
return pi * (r * r);
}
double circle::circumference() {
return 2 * pi * r;
}
/*----------------MAIN FUNCTION----------------*/
int main() {
int choice;
cout << "Enter 1 if you would like to calculate the area of a rectangle, Enter 2 if you would like calculate the area of a circle";
cin >> choice;
if (choice == 1) {
int width, height;
cout << "Enter the width of the rectangle ";
cin >> width;
cout << "Enter the height of the rectangle ";
cin >> height;
rectangle r;
r.setVals(width, height);
cout << "The area of the rectangle is " << r.area() << "\n";
cout << "The perimeter of the rectangle is " << r.perimeter() << "\n";
}
else if (choice == 2) {
double r;
cout << "Enter the radius of the circle ";
cin >> r;
circle c;
c.setR(r);
cout << "The area of the circle is " << c.area(); << "\n"l;
cout << "The circumference of the circle is " << c.circumference() << "\n";
}
return 0;
}
this is just a practice program for getting the hang of OOP in C++, I am aware that C++ compiles from the top-down (so your main function has to be below any variables and objects that it is using), but from what I have seen, there shouldn't be any issue with creating two classes in a single .cpp file, yet I am having this issue. Any help is greatly appreciated, thanks.
The member functions of circle are not marked public.
It has nothing to do with multiple classes; it's just that your second class is improperly defined.
The main goal of the program is to ask the user for a shape, dimensions of the said shape, and to calculate its' area. Using the functions is required.
I'm pretty sure the error lies within
int main()
and
void shape_output(...
void area_output(...
functions
#include <iostream>
#include <iomanip>
using namespace std;
void show_menu();
int user_choice();
int calc_area();
void shape_output(int);
void area_output(int);
int main()
{
int area;
int shape;
show_menu();
shape = user_choice();
area = calc_area();
shape_output(shape);
area_output(area);
return 0;
}
void show_menu()
{
cout << "Calculating the area of a shape\n\n"
<< "1. Circle\n"
<< "2. Rectangle\n"
<< "3. Square\n"
<< "4. Quit\n"
<< "Enter the number of your choice: " << endl;
}
int user_choice()
{
int CIRCLE = 1;
int SQUARE = 2;
int RECTANGLE = 3;
int QUIT = 4;
int choice;
cin >> choice;
if(choice < CIRCLE || choice > QUIT)
{
cout << "Please enter a valid menu choice" << endl;
cin >> choice;
}
return choice;
}
int calc_circle()
{
double radius,
area,
Pi = 3.14;
cout << "Enter the radius: ";
cin >> radius;
if(radius < 0)
{
cout << "Invalid, Try again: ";
cin >> radius;
}
area = Pi * radius * radius;
return area;
}
int calc_rectangle()
{
double height,
width,
area;
cout << "Enter the height: ";
cin >> height;
if(height < 0)
{
cout << "Invalid, Try again: ";
cin >> height;
}
area = height * width;
return area;
}
int calc_square()
{
double base,
area;
cout << "Enter the base: ";
cin >> base;
if(base < 0)
{
cout << "Invalid, Try again: ";
cin >> base;
}
area = base * base;
return area;
}
void quit()
{
cout << "Have a good day!\n";
}
int calc_area()
{
const int CIRCLE = 1;
const int SQUARE = 2;
const int RECTANGLE = 3;
const int QUIT = 4;
int choice = user_choice();
switch(choice)
{
case CIRCLE:
calc_circle();
break;
case SQUARE:
calc_square();
break;
case RECTANGLE:
calc_rectangle();
break;
case QUIT:
quit();
break;
default:
quit();
return 0;
break;
}
return choice;
}
void shape_output(int answer_choice)
{
cout << "Shape: " << answer_choice << endl;
}
void area_output(int answer_area)
{
cout << "Area: " << answer_area << endl;
}
I expect the output to be as such:
choose the shape:
number of the shape
specific dimension of a shape:
dimension(s)
shape: chosen shape
area: calculated area
but the output im getting is:
choose the shape:
number of the shape
number of the shape ( I have to put it in twice)
specific dimension of a shape:
dimension(s)
shape: number of the chosen shape, not the actual word
area: number of the chosen shape again.
Basically, I realized that my whole code was garbage, so I wrote it all from the start.
I had a lot of problems with correctly calling the functions and filling in the parameters and arguments, so I looked at some basic tutorials on functions and incoroporated them into my code.
Thanks everyone for the comments.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//Declaring function prototypes.
int question();
double option(int);
double input_fun();
double calc_circle(double);
double calc_square(double);
double calc_rect(double, double);
void output(double, double);
// main function where all the other functions are called from
int main()
{
int choice; // not related to `int choice` in `double question()`
// Data from question() function will be stored in `int choice`.
double option_case; // Data from `double option()` will be stored here.
choice = question();
option_case = option(choice);
output(choice, option_case);
return 0;
}
// function to prompt the user to choose a shape or quit
int question()
{
int choice;
cout << "Please choose a shape\n"
<< "Press 1 for CIRCLE\n"
<< "Press 2 for SQUARE\n"
<< "Press 3 for RECTANGLE\n"
<< "Press 4 to QUIT\n";
cin >> choice;
while (choice < 1 && choice > 4)
{
cout << "Invalid entry, Try again: \n";
cin >> choice;
}
return choice;
}
// Function to determine user's choice
double option(int choice)
{
double calc_area,
radius,
length,
width;
switch (choice) // Depending on user's choice, switch case decides what functions to call
{
case 1:
cout << "Enter Radius of the circle\n";
radius = input_fun();
calc_area = calc_circle(radius);
return calc_area;
break;
case 2:
cout << "Enter the base of the square\n";
length = input_fun();
calc_area = calc_square(length);
return calc_area;
break;
case 3:
cout << "Enter the length\n";
length = input_fun();
cout << "Enter the width\n";
width = input_fun();
calc_area = calc_rect(length, width);
return calc_area;
break;
case 4:
return 0;
break;
}
}
// This function is activated when user is prompted to
// enter the dimension of the chosen shape.
double input_fun()
{
double value;
cin >> value;
while (value < 0)
{
cout << "Value is lower than 0, try again: \n";
cin >> value;
}
return value;
}
//this function is activated if user chooses a circle.
double calc_circle(double radius)
{
double Pi = 3.14;
double power = 2.0;
return(Pi * pow(radius, power));
}
//this function is activated if user chooses a square.
double calc_square(double base)
{
double power = 2.0;
return(pow(base, power));
}
//this function is activated if user chooses a rectangle.
double calc_rect(double length, double width)
{
return(length * width);
}
void output(double shape, double area)
{
if(shape == 4)
cout << "Have a nice day\n";
else
{
cout << "Shape: " << shape << endl;
cout << "Area: " << area << endl;
}
}
I'm new to programming, I have been reading Sams Teach Yourself C++ in 24 hours. I just started learning classes and am confused on how to allow user input on private data. I created the following class that returns the area of a Trapezoid. Any suggestions would be greatly appreciated.
class Trapezoid
{
//assigns a number to a variable
private:
int a = 20;
int b = 25;
int height = 30;
int area;
public:
int getArea();
};
int Trapezoid::getArea()
{
// calculates the area and returns it.
area = (a + b) / 2 + height;
return area;
}
#include "AreaTrapezoid.hpp"
#include <iostream>
int main()
{
// accesses area inside the Trapeziod class.
Trapezoid areaT;
areaT.getArea();
// displays the area result
std::cout << "The area of a Trapezoid: " << areaT.getArea() << std::endl;
std::cout << system("pause");
return 0;
}
You need to read the user's input, and then expose public access to assign new values to the class's private members. For example:
class Trapezoid
{
//assigns a number to a variable
private:
int a = 20;
int b = 25;
int height = 30;
public:
int getArea();
void setA(int value);
void setB(int value);
void setHeight(int value);
};
int Trapezoid::getArea()
{
// calculates the area and returns it.
return (a + b) / 2 + height;
}
void Trapezoid::setA(int value)
{
a = value;
}
void Trapezoid::setB(int value);
{
b = value;
}
void Trapezoid::setHeight(int value)
{
height = value;
}
#include <iostream>
#include <limits>
#include <cstdlib>
#include "AreaTrapezoid.hpp"
int main()
{
Trapezoid areaT;
int value;
// get the user's input and apply it to the Trapeziod.
std::cout << "Enter A: ";
std::cin >> value;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
areaT.setA(value);
std::cout << "Enter B: ";
std::cin >> value;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
areaT.setB(value);
std::cout << "Enter Height: ";
std::cin >> value;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
areaT.setHeight(value);
// displays the area result
std::cout << "The area of the Trapezoid: " << areaT.getArea() << std::endl;
std::system("pause");
return 0;
}
Alternatively, use a constructor instead:
class Trapezoid
{
//assigns a number to a variable
private:
int a;
int b;
int height;
public:
Trapezoid(int a, int b, int height);
int getArea();
};
Trapezoid::Trapezoid(int a, int b, int height)
: a(a), b(b), height(height)
{
}
int Trapezoid::getArea()
{
// calculates the area and returns it.
return (a + b) / 2 + height;
}
#include <iostream>
#include <limits>
#include <cstdlib>
#include "AreaTrapezoid.hpp"
int main()
{
int a, b, h;
// get the user's input and apply it to the Trapeziod.
std::cout << "Enter A: ";
std::cin >> a;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter B: ";
std::cin >> b;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
areaT.setB(value);
std::cout << "Enter Height: ";
std::cin >> h;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// displays the area result
Trapezoid areaT(a, b, h);
std::cout << "The area of the Trapezoid: " << areaT.getArea() << std::endl;
std::system("pause");
return 0;
}
Okay so i created program that simulates a landscaping company and so we have to calculate the cost of Sod and fence. So when i enter in both the length and width they out put huge decimals for example
Parkton Landscaping
Enter Length: 10
Enter width: 12
Lanscaping Costs
Sod = 6871947680.00
Fence = 19327352760.00
Press any key to continue . . .
Sod is suppose to = 56.40
and Fence is suppose to = 990.00
please help here is my code and both files
#include <iostream>
#include <iomanip>
using namespace std;
#include "c:\Users\barta\OneDrive\Documents\Visual Studio 2015\Projects\Project 6\Project 6\Geometry.h"
#include "Pricing.h"
int main()
{
int length, width;
Pricing landscape;
Geometry geo;
const double Fenceprice = 22.50;
const double Sodprice = .47;
cout << "\t Parkton Landscaping " << endl;
cout << "Enter Length: ";
cin >> length;
cout << "Enter width: ";
cin >> width;
//Pricing(length, width);
//geo.getLength();
//geo.getWidth();
std::cout << std::fixed << std::setprecision(2);
landscape.displayOutput();
cout << "Sod = " << landscape.getsodCost(length) << endl;
cout << "Fence = " << landscape.getFenceCost(Fenceprice) << endl;
system("pause");
return 0;
}
here is the header file:
#pragma once
class Geometry
{//an object is how you access the class
public:// where the public function definitions are created
Geometry();// Default Constructor
Geometry(int, int);
Geometry(int);
void setLength(int); //assigns length
void setWidth(int); //assigns width
void setSide(int); //assigns side
//Constructor function that recieves the values for the rectangle
//Constructor function that recieves values for the cube
int getLength(),
getWidth(),
getSide(),
getArea(),
getPerimeter(),
getSurfaceArea();
private: //where the private members are created
int length,
width,
side;
void checkNum(int); //function that checks to see if the number is less than 0
};
Geometry::Geometry()
{
length = length;
width = width;
side = 0;
}
Geometry:: Geometry(int length, int width) /*function recieves 2 intergers and calls checkNum to validate if */
{
setLength(length);
setWidth(width);
checkNum(length);
checkNum(width);
}
Geometry:: Geometry(int sides)
{
checkNum(sides);
setSide(sides);
}
int Geometry::getLength()
{
return length;
}
int Geometry::getWidth()
{
return width;
}
int Geometry::getSide()
{
return side;
}
int Geometry::getArea()
{
return length * width;
}
int Geometry::getPerimeter()
{
return 2 * (length + width);
}
int Geometry::getSurfaceArea()
{
return 6 * (side * side);
}
void Geometry::setLength(int len)
{
length = len;
checkNum(len);
}
void Geometry::setWidth(int widths)
{
width = widths;
checkNum(widths);
}
void Geometry::setSide(int s)
{
side = s;
checkNum(s);
}
void Geometry::checkNum(int num) //function checks to see if the number is less than zero
{
if (num <= 0)
{
cout << "!!!!!!!!WARNING!!!!!! this isnt a number" << " program will now exit......" << endl;
system("pause");
exit(1);
}
}
Header file #2
#include "Geometry.h"
class Pricing : Geometry
{
public:
Pricing();
Pricing(int length, int width);
double getsodCost(double);
double getFenceCost(double);
void displayOutput();
private:
};
Pricing::Pricing(int length, int width) :Geometry(length, width)
{
}
Pricing::Pricing()
{
}
double Pricing::getsodCost(double price)
{
getArea();
return getArea()*price;
}
double Pricing::getFenceCost(double price)
{
getPerimeter();
return getPerimeter()*price;
}
void Pricing::displayOutput()
{
cout << "\n\n";
cout << "\t Lanscaping Costs " << endl;
}
Because you never initialize the objects with valid values, meaning Geometry::width and Geogrpapy::length are uninitialized and have indeterminate values. Using them uninitialized leads to undefined behavior.