Hey this is really one of the first things I've ever coded. I was wondering how might I fix this error. I am currently trying to do some research but can't find anything that is helpful in fixing it.
#include <iostream> // needed for Cin and Cout
#include <cmath>
#include <csmath>
using namespace std;
/************************************
* defines
*************************************/
#define PI 3.14159
/*************************************
* function prototype
*************************************/
int main()
{
//surface and volume
float radius;
float height;
float surfacearea;
float volume;
int pi = 3.14159
//Get the radius
cout << "enter the radius: ";
cin >> (float)radius;
//Get height
cout << "enter height: ";
cin >> height;
//Get the surfacearea
surfacearea = 2(pi*radius^2)+2(pi*radius)* height;
cout << "The surfacearea is: " << surfacearea;
//get volume
volume = (pi*radius)^2*height;
cout << "The volume is: " << volume << endl;
system ("pause");
return 0;
}
Change int to double for pi, because pi is a floating point number, which, as stated in the comments, is C++'s default for floating point numbers. Unless there is a particular reason to use float, use double for floating-point numbers.
double pi = 3.14159;
And the warning will go away.
Also, you don't have to cast your input to float, simply:
cin >> radius;
Additionally, at the very least, change radius^2 to radius*radius.
But better yet, avoid ^ altogether and use std::pow, an example of which can be found here.
Additionally, you don't need to #define PI 3.14159 because you never use it, and you try to define pi in main().
You better declare and initialize local variables right before you need them. For constants like pi you better use const and proper type. For a proper type C++11 offers you a great tool - auto. And ^ does not mean power in C++ you have to use std::pow() instead. So your code should look like this:
const auto pi = 3.14159;
//Get the radius
auto radius = 0.0;
cout << "enter the radius: ";
cin >> radius;
//Get height
auto height = 0.0;
cout << "enter height: ";
cin >> height;
//Get the surfacearea
auto surfacearea = 2 * pi * pow( radius, 2.0 ) + 2 * pi * radius * height;
cout << "The surfacearea is: " << surfacearea << endl;
//get volume
auto volume = pow( pi*radius, 2.0 ) * height;
cout << "The volume is: " << volume << endl;
To begin with, a warning is not an error; if it were a compilation error, then the code would not even compile. However, since it was a warning, that means your code did compile successfully and run, except that it produced a warning about something in your code. Now to the bugs in your code:
Firstly, your declaration for the local variable pi is incorrect. pi is declared in your code as a variable of data type int, short for integer. An integer is only a whole number, positive and negative one, but one that is neve more specific than 10^0. Now the problem is that you are trying to store a decimal value in an int variable. While the compiler is able to make a conversion of the decimal value into an int value, you lose the precision of the value; that's because it rounds the value. If you compile this sample code:
int floating = 1.23456789;
cout << floating << endl;
It will output 1 instead of 1.23456789, with the reason being that an int variable cannot store a float or double value; it however can convert this float or double value into an int value by rounding it.
Therfore, you should change your declaration for pi to:
double pi = 3.14159; // By the way, you forgot to add a semicolon here
Another problem: you are using unnecessary typecating in your cin statement for the radius:
cin >> (float)radius;
You would need to use casting if you want to change the data type of a variable for a particular operation (you don't change the variable data type; you merely process its value as the data type cast. In your case, it is unrequired, because the radius variable is already declared as a data type of float, in the line:
float radius;
Therefore, I would recommend you to simply change this cin statement to:
cin >> radius;
One more thing: the following lines in your code have a problem:
surfacearea = 2(pi*radius^2)+2(pi*radius)* height;
volume = (pi*radius)^2*height;
The "^" symbol does not raise a number to a power; it is called a bitwise XOR operator in c++ and it server the purpose of copying the bit if it is set in one operand but not both. You can find more information about it here: Bitwise Exclusive OR Operator: ^
In c++, if you want to raise a number x to a power like 2, then you have to do x * x. Alternatively, you can use the pow() function like: pow(x, 2.0). For your code, if we use the x*x method, it would be like:
surfacearea = 2(pi*radius*radius)+2(pi*radius)* height;
volume = (pi*radius)*(pi*radius)*height;
Alternatively, if we use the pow() function, then the code would look like:
surfacearea = 2(pi*pow(radius, 2))+2(pi*radius)* height;
volume = pow((pi*radius), 2)*height;
Fixing these peoblems should get your code to work.
Related
I am a complete beginner in programming and I was given the following assignment:
Write a C++ program that computes a pair of estimates of π, using a sequence of inscribed and circumscribed regular polygons. Halt after no more than 30 steps, or when the difference between the perimeters of the circumscribed and inscribed polygons is less than a tolerance of ε=10⁻¹⁵. Your output should have three columns, for the number of sides, the perimeter of an inscribed polygon, and perimeter of the circumscribed polygon. For the last two columns, display 14 digits after the decimal point.
well, I decided to use the law of cos to find the lengths of the sides of the polygon but when I was testing out my program I realized the line:
a = cos(360 / ngon);
keeps giving me a zero as the output which makes everything else also zero and I am not sure what is wrong please help.
P.S. Sorry if the program looks really sloppy, I am really bad at this.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#define _USE_MATH_DEFINES
#include <math.h>
#include <cmath>
using namespace std;
int main()
{
char zzz;
int ngon = 3, a, ak;
double insngon = 0.0;
double cirngon = 0.0;
cout << "Number of Sides" << "\t\t\t" << "Perimeter of insribed region" << "\t\t\t" << "Perimeneter of circumscribed polygon" << "\t\t" << "\n";
while (ngon <= 30)
{
a = cos(360 / ngon);
ak = pow(.5, 2) + pow(.5, 2) - 2 * .5*.5*a;
insngon = (ak*ngon);
cirngon = (ak / (sqrt(1 - pow(ak, 2))));
cout << fixed << setprecision(14) << ngon << " " << insngon << " " << cirngon << endl;
ngon++;
if (cirngon - insngon <= pow(10.0, -15));
cin >> zzz;
return 0;
}
cout << "\nEnter any character and space to end ";
cin >> zzz;
return 0;
}
One issue is that you declared integers, yet you are using them in the call to cos here:
int ngon = 3, a, ak;
//...
a = cos(360 / ngon);
Since a is an integer, the return value of cos (which is of type double) will be truncated. Also, since ngon is an integer, the 360 / ngon will also truncate.
The fix is to make a a double, and divide 360.0 by ngon to prevent the truncation:
int ngon = 3, ak;
double a;
//...
a = cos(360.0 / ngon);
The other issue, as pointed out in the comments is that the trigonometric functions in C++ use radians as the argument, not degrees. You need to change the argument to the equivalent value in radians.
Another issue is that you're using pow to compute values that are constant. There is no need to introduce an unnecessary function call to compute constant values. Just define the constants and use them.
For example:
const double HALF_SQUARED = 0.25
const double EPSILON_VALUE = 10.0e-15;
and then use HALF_SQUARED and EPSILON_VALUE instead of the calls to pow.
Also, pow is itself a floating point function, thus can produce results that are not exact as is discussed by this question . Thus pow(ak, 2) should be replaced with simply ak * ak.
Use float a; (or double a) instead of int a.
Here the return type of a is int
And calculating
a = cos(360/ngon)
Is equivalent to a= cos(120) that is the result of cos(120) is 0.8141 and being a integer type "a" will only store the integer part it.
Therefore 'a' will be 0 and discarding floating value.
Also use double ak; instead of int ak;.
Because here pow function has been used which have return type 'double'
#include <iostream> // try to convert height in centimeters to meters
using namespace std;
int main()
{
int height_cm ; // declaring first int in centimeters
cout << " State your height in centimeters: " << endl ; // stating
cin >> height_cm ; /* I was thinking also of storing this value for later reusage in retyping but I don´t know how to do it */
double (height_cm) ; /* here is the problem,debugger says that I can not declare height_cm again because I had declared it as int before , but I´m actually trying to retype it */
height_cm /= 100 ; /* I´m also not sure about this , I think this assignment should be possible to get number in meters */
cout << " Your height in meters is: " << height_cm << endl ; /* this should give me height in meters */
return 0 ;
}
The problem is that, as your compiler is saying, you are trying to use the same name (height_cm) for another variable. Try doing:
...
double height_m = height_cm/100.0;
cout << " Your height in meters is: " << height_m<< endl ;
...
This way the meter variable will have a new name and the compiler will compile. Moreover, note that I divided height_cm by 100.0 instead of 100. This is because 100 is an int and 100.0 is a float or double. If you use int then you would have an int division meaning that you would lose the decimal part.
A part from that:
I was thinking also of storing this value for later usage in retyping but I don´t know how to do it: The cin>>height_cm; code takes whatever the user has typed, converts it to int and stores it in a variable called height_cm that you can use anytime you want in the current function (in this case main()).
I´m also not sure about this, I think this assignment should be possible to get number in meters: That code would compile with no problem. However, this would end up with an int divison. If you want you can do:
Code:
...
double height_m(height_cm);// this converts the int to double and stores it in the new variable height_m
height_m /= 100;// Divide the double variable height_m by 100 and store it again in height_m
...
Note that in this case although you are using 100 instead of 100.0 that would not be an int division because height_m is a double.
I am required to fully understand the following code :
#include <iostream>
using namespace std;
double area(double length, double width);
double time(double p_area, double h_area, double mow_rate);
int main() {
double d_plot_length, d_plot_width, d_home_side, d_mow_rate;
double plot_area, home_area, time_taken;
// I've used double for all of these to get the most precise values possible, something I'd only really consider doing on small programmes such as this
cout << "What is the length of the plot? In meters please." << endl;
cin >> d_plot_length;
cout << "What is the width of the plot? In meters please." << endl;
cin >> d_plot_width;
cout<< "What is the size of the side of the house? In meters please." << endl;
cin >> d_home_side;
cout << "What is the rate at which you are going to be mowing? In meters per minute please" << endl;
cin >> d_mow_rate;
// Just getting all the data I need from the user
plot_area = area(d_plot_length, d_plot_width);
home_area = area(d_home_side, d_home_side);
time_taken = time(plot_area, home_area, d_mow_rate);
cout << "It will take " << time_taken << " minutes to mow this lawn. Better get cracking" << endl;
return 0;
}
double area(double length, double width) {
double value;
value = length * width;
return value;
}
double time(double p_area, double h_area, double mow_rate) {
double value;
value = (p_area - h_area) / mow_rate;
return value;
}
I am struggling to understand how the time() function works.
So far I understand that :
time_taken , gets its value from the time() function: time(plot_area, home_area, d_mow_rate).
The time() function gets its values from the function declaration at the bottom.
double time(double p_area, double h_area, double mow_rate) {
double value;
value = (p_area - h_area) / mow_rate;
return value;
}
However, this is where I'm stuck. The user is asked to enter values for d_plot_length, d_plot_width, etc. So I cannot understand how the compiler knows what these values p_area, and h_area actually are.
I realise that somehow the area() function is being used to aid the time() function, but as far as I'm aware the variables P_area etc within the time() function do not have values assigned to them.
Please can someone fill in the gaps in my understanding.
To be more precise, I want to know exactly how time_taken is displayed on the screen, from the start of the process, to the cout. Like I say I am familiar with most areas but not all.
In your program, you had computed the following values:
plot_area = area(d_plot_length, d_plot_width);
home_area = area(d_home_side, d_home_side);
When the method area(double,double) is invoked, the resultant double value gets stored in these variables.
Then you have the function call:
time_taken = time(plot_area, home_area, d_mow_rate);
This is the call by value type of function invocation. A copy of the values in the variables, plot_area, home_area and d_mow_rate are passed to the function. In the time(double, double, double) the computing is done upon the basis of the logic you had defined in this method and the resultant value is returned to the function call in the main() method.
Please note that the function call is of call by value and hence only a copy of the values are passed to the arguments mentioned in the function time(double, double, double) even though the variable names are the same in the main() and in the function call.
For further reading, I will suggest you to have a look at the following links:
Call By
Value
Call By
Reference
Call By
Pointer
I'm working on below program and I want the program to do the same thing, but with not one main() function, but instead one main() function PLUS one user defined function called computeConeVolume that contains the calculation. In other words I want to remove the one line calculation and replace it with a function call, then write and add the function below main with the calculation, surrounded any other syntax that I need to complete it.
The function should contain local variables and a constant declared and must have the calculation, it may not do anything else such as input or output.
Should be able to declare "global" variables anywhere but no variables above or outside of main() and the function are allowed.
A value-returning function should be used because it's a little simpler to understand, but you can employ a void function.
Need to have a function prototype at the top of the code, then main, then your function.
Need some help with this since I'm new to C++ and trying to learn.
//Cone Volume Calculator Program
#include <iostream>
using namespace std;
int main( )
{
//Declare variables and constants
double coneRadius = 0.0;
double coneHeight = 0.0;
const double PI = 3.1415;
double coneVolume = 0.0;
//Prompt the user for inputs
cout << "Enter the radius of the cone: ";
cin >> coneRadius;
cout << "Enter the height of the cone: ";
cin >> coneHeight;
//Do the calculation
coneVolume = 0.3333 * PI * coneRadius * coneRadius * coneHeight;
//Display the result
cout << "The volume of your cone is: " << coneVolume << endl;
system("pause");
return 0;
} //end of main
I'm trying to recycle some of Amadeus' answer and use some of your code.
First of all, you should define the function you wish to calculate the cone volume with. Something like:
double coneVolume(double, double);
You should pay attention to always leave the main function at the end of your .c document.
What you also need is a declaration of your function. This is where you actually write down what the function does:
double coneVolume(double coneRadius = 0.0, double coneHeight = 0.0) {
double coneVolume = coneVolume = 0.3333 * PI * coneRadius * coneRadius * coneHeight;
return coneVolume;
}
The value setting in the method head is just a thing for default values, this isn't really needed here, just to show you.
Where is the const double PI = 3.1415; going? Somewhere above your functions, then it's visible everywhere in your document. You could also think about using math.h by include, then you can use M_PI, which is about the same thing as your PI constant. (To be more precise it is a definition which replaces any time you write M_PI by the actual Pi value)
If you really want the function to just calculate without input, you can just define them locally, just like in your main.
Note: Global scope is always out of the main method.
How about this program
#include <cmath>
#include <iostream>
using namespace std;
double coneVolume(double, double);
int main( )
{
//Declare variables and constants
double coneRadius = 0.0;
double coneHeight = 0.0;
//Prompt the user for inputs
cout << "Enter the radius of the cone: ";
cin >> coneRadius;
cout << "Enter the height of the cone: ";
cin >> coneHeight;
//Do the calculation
//Display the result
cout << "The volume of your cone is: " << coneVolume(coneRadius, coneHeight) << endl;
system("pause");
return 0;
} //end of main
double coneVolume(double coneRadius, double coneHeight)
{
double PI = acos(-1.0);
double volume = coneRadius * coneRadius * coneHeight * PI / 3.0;
return volume;
}
Please note that I added the cmath library in order to use the acos function.
double PI = acos(-1.0);
I read this trick in a piece of code that a guy used to do trigonometry using C++.
I did not use any kind of parenthesis when I calculated the volume because both * and / are in the same order of precedence and are evaluated from left to right. I divided by 3.0 because I am using doubles. For the prototype of the function that is right above the main function
double coneVolume(double, double);
I just wrote the type of arguments of the function as only the type of the arguments of the function is needed for propotypes.
I am working with a basic C++ program to determine the area and perimeter of a rectangle. My program works fine for whole numbers but falls apart when I use any number with a decimal. I get the impression that I am leaving something out, but since I'm a complete beginner, I have no idea what.
Below is the source:
#include <iostream>
using namespace std;
int main()
{
// Declared variables
int length; // declares variable for length
int width; // declares variable for width
int area; // declares variable for area
int perimeter; // declares variable for perimeter
// Statements
cout << "Enter the length and the width of the rectangle: "; // states what information to enter
cin >> length >> width; // user input of length and width
cout << endl; // closes the input
area = length * width; // calculates area of rectangle
perimeter = 2 * (length + width); //calculates perimeter of rectangle
cout << "The area of the rectangle = " << area << " square units." <<endl; // displays the calculation of the area
cout << "The perimeter of the rectangle = " << perimeter << " units." << endl; // displays the calculation of the perimeter
system ("pause"); // REMOVE BEFORE RELEASE - testing purposes only
return 0;
}
Change all your int type variables to double or float. I would personally use double because they have more precision than float types.
int datatype stands for integer (i.e. positive and negative whole numbers, including 0)
If you want to represent decimal numbers, you will need to use float.
Use the float or double type, like the others already said.
But it ain't as simple as that. You need to understand what floating-point numbers actually are, and why (0.1 + 0.1 + 0.1) != (0.3). This is a complicated subject, so I won't even try to explain it here - just remember that a float is not a decimal, even if the computer is showing it to you in the form of a decimal.
use floats not ints an integer (int) is a whole number, floats allow decimal places (as do doubles)
float length; // declares variable for length
float width; // declares variable for width
float area; // declares variable for area
float perimeter; // declares variable for perimete
You've defined your variables as integers. Use double instead.
Also, you can look up some formatting for cout to define the number of decimal places you want to show, etc.