#include <iostream>
#include <iomanip>
#define _USE_MATH_DEFINES //needed to include the math constants
#include <math.h>
#include <string> //needed to include texts
using namespace std;
double Volume(double Length, double Width, double Height)
{
double volume;
volume = Length*Width*Height;
return volume;
}
double Area(double Length, double Width, double Height)
{
double area;
area = 2 * Width*Length + 2 * Length*Height + 2 * Height*Width;
return area;
}
void DisplayData(double Length, double Width, double Height, double volume, double area)
{
cout << "For the width " << Width << ", the length " << Length << " and the Height " << Height << "; the volume of the box is " << volume << " and the surface area is " << area << ".";
}
int main()
{
double Length, Width, Height;
cout << "Welcome! This program will calculate the volume and surface area of a box. All this program needs is you to input the length, width and height of the box." << endl;
cout << "Please note that all meausurments are in meters." << endl;
cout << "Please insert a value for the length: " << endl;
cin >> Length;
cout << "Please insert a value for the width: " << endl;
cin >> Width;
cout << "Please insert a value for the height: " << endl;
cin >> Height;
cout << endl;
Volume;
Area;
DisplayData;
return 0;
}//end main
I am writing a program with functions but it gives me the error in the title. How exactly do I call functions? I don't really understand that part. Do you just write the name of the function or is there something else involved?
I guess you should call the functions like this
double volume = Volume(Length, Width, Height);
double area = Area(Length, Width, Height);
DisplayData(Length, Width, Height, volume, area);
instead of the three meaningless statements
Volume;
Area;
DisplayData;
Function name and variable name is clashing with each other. Compiler is treating volume variable as function and looking for arguments after it.
double volume;
Change it to
double dVolume;
dVolume = Length*Width*Height;
return dVolume;
Do similar changes for area as well.
Related
I have included the code I wrote below. I have created a function that calculates the volume of a cone based on the user's input. This is working as intended.
# include <iostream>
# include <string.h>
# include <string>
using namespace std;
// ConeVolume prototype
float ConeVolume(float radius, float height);
int main()
{
// Establish variables
float radius1;
float height2;
float volumeCone = ConeVolume(radius1, height2);
// User input to define the varibales
cout << "Radius: ";
cin >> radius1;
cout << "Height: ";
cin >> height2;
// Return variable using the ConeVolume function
cout << endl << "Cone Volume: " << volumeCone;
return 0;
}
// Function that calculates the volume of a Cone
float ConeVolume(float radius, float height)
{
float pi = 3.14;
float volume = (pi/3)*(radius * radius) * (height);
return volume;
}
My question... if I were to call the function by outputting the variable "float ConeVolume" as below, why does the program return '0'? Can I not set the value of a variable equal to a function?
// Return variable using the volumeCone float variable
cout << endl << "Cone Volume: " << volumeCone;
You've simply made a silly mistake. You've called the 'ConeVolume' function before taking user input. So, only garbage values are being passed to the function.
# include <iostream>
# include <string.h>
# include <string>
using namespace std;
// ConeVolume prototype
float ConeVolume(float radius, float height);
int main()
{
// Establish variables
float radius1;
float height2;
//wrong code here
// you've called the function before taking input of radius1 and height2
//float volumeCone = ConeVolume(radius1, height2);
// User input to define the varibales
cout << "Radius: ";
cin >> radius1;
cout << "Height: ";
cin >> height2;
// Correct code:
// Call the function after taking input
float volumeCone = ConeVolume(radius1, height2);
// Return variable using the ConeVolume function
cout << endl << "Cone Volume: " << volumeCone;
return 0;
}
// Function that calculates the volume of a Cone
float ConeVolume(float radius, float height)
{
float pi = 3.14;
float volume = (pi/3)*(radius * radius) * (height);
return volume;
}
Hope this helped.
The program return 0 because the value volumeCone is not being updated after you changed the values of radius1 and height2.
You have to call the function coneVolume() again, or better yet just call it after you define radius1 and height2.
int main()
{
// Establish variables
float radius1;
float height2;
// User input to define the varibales
cout << "Radius: ";
cin >> radius1;
cout << "Height: ";
cin >> height2;
float volumeCone = ConeVolume(radius1, height2);
// Return variable using the ConeVolume function
cout << endl << "Cone Volume: " << volumeCone;
return 0;
}
Insert the line
volumeCone = ConeVolume(radius1, height2);
after the line
cin >> height2;
And change line
float volumeCone = ConeVolume(radius1, height2);
to
float volumeCone;
I am a newbie in C++ and I am quite confused in programmer - defined functions.
It shows these errors
40 [Error] cannot convert 'double' to 'double(double, double)' in assignment
40 [Error] assignment of function 'double total_area(double, double)'
I cannot enter total_area = cross_area + side_area;
If I try to remove the double, it results to more errors
I can't find any information in youtube or in google that seems helpful.
// P33_2.cpp This program illustrates the local and global variables and call-by-value.
// This program computes the side area and the cross section area of a cylinder
#include<iostream>
#include<cmath>
using namespace std;
const double PI = 3.14159; // This variable is defined globally, known to all functions in this program as PI
double cross_area(double r); // Function prototype for function cross_area
double side_area(double r, double h); // Function prototype for function Side_area
double total_area(double cross_area, double side_area);
int main(void)
{
double h, r; //variables local to the main function
cout << "Enter the radius and the height of the cylinder in Cm <Enter> ";
cin >> r >> h;
cout << endl;
cout << "Before I do any computation or call any function, I want to let you know that \n";
cout << "you have entered r = " << r << " and h = " << h << "." << endl;
cout << "I am planning to use inch, thus in the first function, I will convert r, and " << endl;
cout << "in the second one I will convert h \n";
cout << "The cross section area of the cylinder is " << cross_area(r) << " inch-sqr endl\n";
cout << "The side area of the cylinder is " << side_area(r,h) << " inch-sqr \n\n";
cout << "The total surface area is "<< total_area << "inch-sqr \n \n";
return 0;
}
double cross_area(double r)
{
//Cross secion area includes the disks at the bottom and the top
r = r * 0.3937; // converting r to inch
return 2*PI*pow(r,2);
}
double side_area(double r, double h)
{
double area; //variable local to Side_area function
h = h * 0.3937; // converting h to inch
area = 2*PI*r*h;
return area;
}
double total_area(double cross_area, double side_area)`enter code here`
{
total_area = cross_area + side_area;
return 0;
}
See improved working code below
in your code, you have not passed any argument to total_area function. How do you think you will calculate area without passing arguments.
cout << "The total surface area is "<< total_area << "inch-sqr \n \n";
#include<iostream>
#include<cmath>
using namespace std;
const double PI = 3.14159; // This variable is defined globally, known to all functions in this program as PI
double cross_area(double r); // Function prototype for function cross_area
double side_area(double r, double h); // Function prototype for function Side_area
double total_area(double cross_area, double side_area);
int main(void)
{
double h, r; //variables local to the main function
cout << "Enter the radius and the height of the cylinder in Cm <Enter> ";
cin >> r >> h;
cout << endl;
cout << "Before I do any computation or call any function, I want to let you know that \n";
cout << "you have entered r = " << r << " and h = " << h << "." << endl;
double dcross_area = cross_area(r);
double dside_area= side_area(r,h);
cout << "I am planning to use inch, thus in the first function, I will convert r, and " << endl;
cout << "in the second one I will convert h \n";
cout << "The cross section area of the cylinder is " << cross_area(r) << " inch-sqr endl\n";
cout << "The side area of the cylinder is " << side_area(r,h) << " inch-sqr \n\n";
cout << "The total surface area is "<< total_area(dcross_area, dside_area) << "inch-sqr \n \n";
return 0;
}
double cross_area(double r)
{
//Cross secion area includes the disks at the bottom and the top
r = r * 0.3937; // converting r to inch
return 2*PI*pow(r,2);
}
double side_area(double r, double h)
{
double area; //variable local to Side_area function
h = h * 0.3937; // converting h to inch
area = 2*PI*r*h;
return area;
}
double total_area(double cross_area, double side_area)
{
return cross_area + side_area;
}
I've only recently learnt C++. I had a school project for making a BMI calculator. Unfortunately it is showing up errors beyond my scope of understanding. I'm not sure if I should use a different data type for my height and weight variables - should these by double?
#include <iostream>
using namespace std;
float bmi_calc(int height, int weight){
float bmi_user = weight / height * height;
return bmi_user;
}
int main()
{
int weight_user;
int height_user();
cout << "Enter your weight in kilograms";
cin >> weight_user;
cout << "Enter your height in meters";
cin >> height_user;
cout << "Your BMI is " << bmi_calc(height_user, weight_user);
}
fixed code:
#include <iostream>
using namespace std;
float bmi_calc(float height, float weight) {
return weight / (height * height);
}
int main()
{
float weight_user;
float height_user;
cout << "Enter your weight in kilograms ";
cin >> weight_user;
cout << "Enter your height in meters ";
cin >> height_user;
cout << "Your BMI is " << bmi_calc(height_user, weight_user);
return 0;
}
since your height is given in meters you need a float, since a int can only be used for whole numbers. also use proper parenthesis to get the formula right :)
While using Xcode8 says that it is running but there is no output.
Output:
10
Please enter the rectangle's length:
10
Please enter the rectangle's width:
The length for the rectangle is 10.00
The width for the rectangel is 10.00
The area for a rectangle is 100.00
The radius for a circle is 10.00
The area for a circle is 314.16
Program ended with exit code: 0
Wanted Output:
Please enter the rectangle's length: 10
Please enter the rectangle's width: 10
The length for the rectangle is 10.00
The width for the rectangel is 10.00
The area for a rectangle is 100.00
The radius for a circle is 10.00
The area for a circle is 314.16
Program ended with exit code: 0
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
// Function prototypes
double getLength(); // Function that receives the length
double getWidth(); // Function that receives the width
double getArea(double); //Function calculates area for circle
double getArea(double,double); // Function calculates area for rectangle
void displayData(double,double,double,double, double);
// displayData function is used to display values
int main() {
// Declares variables
double length, width, circleArea, rectangleArea;
length = getLength(); // Stores length
width = getWidth(); // Stores width
circleArea = getArea(width); // Stores area of circle
rectangleArea = getArea(width, length); // Stores are of rectangle
// Displays the length, width, area of circle, and rectangle area
displayData(length, width, rectangleArea, width, circleArea);
return 0;
}
/* This function asks the user to enter the rectangle's length and
then returns that values as a double */
double getLength() {
double length;
cout << "Please enter the rectangle's length: ";
cin >> length; // User input is stored in variable length
cout << "\n";
/* While loop doesn't let user input a
negative value or a value of zero for
the length of the rectangle. */
while (length <= 0) {
if (length < 0) { //if statement used when a negative value is entered for length
cout << "INVALID INPUT!" << endl; // Error message
cout << "Please enter a positive value for the Length of the Rectangle: ";
// Asks the user to enter the length again
cin >> length; // Stores value of length
if (length > 0) {
cout << "\n";
}
}
if (length == 0) { // If statement used when a value of zero is entered for length
cout << "INVALID INPUT!" << endl;//error message
cout << "Please enter a positive value for the Length of the Rectangle: ";
// Asks the user to enter the length again
cin >> length;// Stores the value of length
if (length > 0) {
cout << "\n";
}
}
}
return length;
}
/* This function asks the user to enter the rectangle's width and
then returns that values as a double */
double getWidth() {
double width;
cout << "Please enter the rectangle's width: ";
cin >> width; // User input is stored in variable width
cout << "\n";
/* While loop doesn't let user input a
negative value or a value of zero for the
width of the rectangle. */
while (width <= 0) {
if (width < 0) { // If statement used when a negative value is entered for width
cout << "INVALID INPUT!" << endl; // Error message
cout << "Please enter a positive value for the Width of the Rectangle: ";
// Asks the user to enter the width again
cin >> width; //Stores the value of width in the variable, width
if (width > 0) {
cout << "\n";
}
}
if (width == 0) { // If statement used when a value of zero is entered for width
cout << "INVALID INPUT!" << endl; // Error message
cout << "Please enter a positive value for the Width of the Rectangle: ";
// Asks the user to enter the width again
cin >> width; // Stores the value of width in the variable, width
if (width > 0)
{
cout << "\n";
}
}
}
return width;
}
double getArea(double radius) {
const double PI = 3.14159;
double circleArea;
circleArea = PI * pow(radius, 2); // Formula for the area of a circle
return circleArea;
}
/* This function accepts the rectangle's length and width as arguments and returns
the rectangle's area. The area is calculated by multiplying the length by the
width. */
double getArea(double length, double width) {
double rectangleArea;
rectangleArea = length * width; // Formula for the area of a rectangle
return rectangleArea;
}
/* This function accepts the rectangle's length, width, and area as arguments
and displays them in an appropriate message on the screen */
void displayData(double l, double w, double ra, double r, double ca) {
cout << setprecision(2) << fixed;
cout << "The length for the rectangle is " << l << endl;
cout << "The width for the rectangle is " << w << endl;
cout << "The area for a rectangle is " << ra << endl << endl;
cout << "The radius for the circle is " << r << endl;
cout << "The area for the circle is " << ca << endl;
}
When you use << with cout, it doesn't immediately push to the screen. A common way to send the output, as you've shown, is endl. If you don't want a line return, use flush.
cout << "Please enter the rectangle's length: " << flush;
EDIT: Xcode 8.3 seems to have new rules about cin and cout on the same line.
I need help with fixing the program. It is not running. I keep getting the warning control reaches end of non void function. I dont know how to fix it. Please help me. The program is suppose to find the volume or the surface area of a sphere. I get the warning on the last 2 }
#include <iostream>
#include <iomanip>
#include <cmath>
#include <math.h>
using namespace std;
char s = '\0';
const char SENTINEL = 's';
float radius, answer;
void get_radius (float&);
float surface_area (float);
float volume (float);
float cross_section (float);
const float PI = 3.14;
int main()
{
cout << "This program will let you input the radius of a sphere to find its volume or surface area." << endl << endl;
cout << "Enter 'v' for volume or 'a' for surface area of a sphere" << endl;
cout << "'s' to stop" << endl;
cin >> s;
while (s != SENTINEL)
{
get_radius (radius);
if(s == 'V')
{
volume (radius);
}
else if(s == 'A')
{
surface_area (radius);
}
cout << "Enter 'v' for volume or 'a' for surface area of a sphere" << endl;
cout << "'s' to stop" << endl;
cin >> s;
}
system("PAUSE");
return 0;
}
void get_radius (float& radius)
{
cout << "Please enter the radius of the sphere: " << endl;
cin >> radius;
}
float volume (float radius){
float answer;
answer = 4.0/3.0 * PI * pow (radius, 3);
cout << "The volume is: " << answer << endl;
}
float surface_area (float radius){
float answer;
answer = 4.0 * PI * pow(radius, 2);
cout << "The surface area is: " << answer << endl;
}
your function declaration must match what you are returning. You have to make sure you are returning values from functions that are declared that they are returning something.
volume() and surface_area() are printing things with cout but are not returning anything.
float volume (float radius){
float answer;
answer = 4.0/3.0 * PI * pow (radius, 3);
cout << "The volume is: " << answer << endl;
return answer;
}
float surface_area (float radius){
float answer;
answer = 4.0 * PI * pow(radius, 2);
cout << "The surface area is: " << answer << endl;
return answer;
}
When you declare the type of a function, you need to return a value of that type. For example, your function:
float volume (float radius) {}
Needs a return statement returns a value of type float.
If you don't need the function to actually return something, then declare it void to let the compiler know that. In this case:
void volume (float radius)
Just be careful, since void functions must NOT return a value (they can use a bare return statement, though).
Note also that potential paths which skips the return statement can trigger this error. For example, I could have this function:
int veryBadFunction(int flag)
{
if (flag == 1) {
return 1;
}
}
In this case, even though there IS a return statement in the function, it gets skipped any time the value of flag is something other than '1'. This is why the error message is worded control reaches...