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...
Related
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 wrote this program for my course, but when I run it the output within the OutputData() function just spits out zero.
we have been studying void functions and referencing. I do everything that I think I am supposed to be doing. And he gives an outline on what we should have a set up. Sadly, it does not work and I'm sure I'm missing something.
What makes it output zero when run?
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
// Constants
const double pi = 3.14;
// Global Variables
double radius, circumference, area;
// Prototypes
void Banner();
void ComputeArea(double, double);
int ComputeCircumference(double);
void GetValue(double);
bool GoAgain();
void OutputData(double, double, double);
int main()
{
Banner();
do {
GetValue(radius);
circumference = ComputeCircumference(radius);
ComputeArea(radius, area);
OutputData(radius, area, circumference);
} while (GoAgain());
return 0;
} // Function main()
// ==================
// =================
void Banner() {
cout << "Welcome to the program!\n";
cout << "Input a radius of a circle,\n";
cout << "I will compute the area and\n";
cout << "circumference of that radius.\n";
cout << "Let's begin!\n";
} // Function Banner()
// =========================
// =====================
void ComputeArea(double, double) {
area = pow((pi * radius), 2);
} // Function ComputeArea()
// ==============================
// =====================================
int ComputeCircumference(double
circumference) {
circumference = 2 * pi * radius;
return circumference;
} // Function ComputeCircumference()
// ======================================
// ==========================
void GetValue(double) {
double radius;
cout << "Please enter your circles radius: " << endl;
cin >> radius;
} // Function GetValue()
// ===========================
// =========================
bool GoAgain() {
bool validAnswer;
char answer;
do {
cout << "Enter another radius?\n";
cout << "[y/Y] to go again. [n/N] to exit: ";
cin >> answer;
if ((answer == 'y') || (answer == 'y') ||
(answer == 'n') || (answer == 'N'))
validAnswer = true;
else {
validAnswer = false;
cout << "Error. Enter a valid character: ";
}
} while (!validAnswer);
if ((answer == 'y') || (answer == 'Y'))
return true;
else if ((answer == 'n') || (answer == 'N'))
return false;
} // Function GoAgain()
// ===========================
// ===========================
void OutputData(double, double, double) {
cout << "Here are the results: ";
cout << "You entered: " << radius << " for the radius." << endl;
cout << "Area: " << area << endl;
cout << "Circumference: " << circumference << endl;
} // Function OutputData()
// ============================
If you want to pass results from functions, there are three methods:
Return the result
Pass the result variable by reference (or pointer)
Use a global variable
Your functions don't have variable names in their declarations or definitions.
Note: when passing values to functions without reference, the values are passed by copy. Modifying of a non-reference parameter modifies the copy not the original variable passed to the function.
Returning the Result
//! Note the return type
double Compute_Area(double radius) // Note the parameter name
{
// Note the "return" statement used to return a value.
return 2.0 * pi * radius;
}
Return by Reference
void Compute_Area(double radius,
double& area) // Note the '&' to designate reference
{
// The "area" variable is the parameter variable,
// which is the caller's variable.
area = 2.0 * pi * radius;
}
Global Variable
double area;
void Compute_Area(double radius)
{
area = 2.0 * pi * radius;
}
You should make a habit of providing parameter names in your function's declarations and definitions.
When using more than one parameter of the same type, parameter names help distinguish the parameters (such as height and width for the area of a rectangle). Otherwise Users of the functions (including yourself) will have a difficult time figuring out the purpose of each parameter.
This is my first C++ programming practice. When I run this it displays "Enter the length of the side" and "Which figure's area are you calculating? ". I enter 3 and square but the only result I can get is "Unknown figure. Try again." I'm not sure why this happens. Maybe something’s not connected well..
#include <iostream>
#include<math.h>
using namespace std;
int main() {
int Side;
float Area;
float sqrt2 = 1.414;
float sqrt3 = 1.732;
float sqrt4 = 2;
float sqrt5 = 2.236;
float cot = 2.077;
float pi = 3.141;
char figure;
char equaliteral_triangle,square,pentagon,hexagon,heptagon,octagon;
cout << "Enter the length of the side: " << endl;
cin >> Side;
cout << "Which figure's area are you calculating? " << endl;
cin >> figure;
if(figure == equaliteral_triangle) {
Area = (sqrt3/4) * (Side * Side);
cout << "The area of triangle is, "<< Area << endl;
}
else if (figure == square) {
Area = (Side * Side);
cout << "The area of square is, "<< Area << endl;
}
else if (figure == pentagon) {
Area = (0.25 *(5 *(5 +(2*sqrt5)))) * (Side * Side);
cout << "The area of pentagon is, "<< Area << endl;
}
else if (figure == hexagon) {
Area = ((3*sqrt3)/2) * (Side * Side);
cout << "The area of hexagon is, "<< Area << endl;
}
else if (figure == heptagon) {
Area = (7/4) * (Side * Side) * cot;
cout << "The area of heptagon is, "<< Area << endl;
}
if (figure == octagon) {
Area = (2 * (1+ sqrt2)) * (Side * Side);
cout << "The area of octagon is, "<< Area << endl;
}
else {
cout << "Unknown figure. Try again." << endl;
}
}
char equaliteral_triangle,square,pentagon,hexagon,heptagon,octagon;
You have declared these variables and haven't initialized them
IT should be
char equaliteral_triangle='t', square='s', pentagon='p', hexagon='h', heptagon='H', octagon='o';
You need to give some values to the char variables you defined. Without giving them some values they will remain undefined and therefore you will not match them. Note that these
char figure;
char equaliteral_triangle,square,pentagon,hexagon,heptagon,octagon;
are variables. If you type square into the console, it will not match the square variable's value. So, first of all, work with the characters like this:
char figure;
char equaliteral_triangle = '3',square = '4',pentagon = '5',hexagon = '6',heptagon = '7',octagon = '8';
and when you are successful and the program works, refactor it so that it will use a switch-case instead of if-else if which is more elegant in this case, when you have characters.
You have no values in your chars which means they all are NULL and when you compare them with the value user entered they will not be equal.
I would recommend only using the number of sides only if you want to have only these figures.
Eg if you have 3 sides you know it would be a triangle so use the formula accordingly .
Cout (and everything else I've attempted to put after the return) doesn't work when placed after the return in this function. What's wrong?
float tanDegrees() {
string i;
double iDouble;
string choice;
cout << "Give me a number to find the value of degrees in. ";
getline(cin, i);
iDouble = stod(i);
double PI = 3.14159265359;
float answer = tan((PI / 180)*iDouble);
cout << "Test!" << endl;
return answer;
cout << "Test!" << endl;
}
The return causes you to leave the function's scope. So anything after "return" won't be touched.
When the program is operating correctly, it is supposed to ask the user for a code (A, C ,D) and then ask for a Radius.
Which it does fine.
But when it's finished it is supposed to cout something that looks like "The area of a circle with radius 6.75 is 143.14"
But when I run mine, it prints "The area of a circle with radius 6.75 is A." I just can't seem to find what I'm doing wrong, and any help is greatly appreciated
#include <iostream>
#include <iomanip>
using namespace std;
/*run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
cout << fixed << setprecision(2);
char Code;
double Radius;
//promt for a code and A, C, or D
cout << "Please enter your code A, C, or D ";
// read the input
cin >> Code;
//promt user for value of radius
cout << "Please enter a value for the radius";
//read the input radius
cin >> Radius;
// based on the code, calculate the required computed result
double Const;
Const = 3.1416;
'A' == Const * Radius;
'C' == 2 * Const * Radius;
'D' == 2 * Radius;
if (Code == 'A')
cout << "The area of a circle with radius" << Radius << "is" << 'A' << endl;
else if (Code == 'C')
cout << "The circumference of a circle with radius" << Radius << "is" << 'C' << endl;
else if (Code == 'D')
cout << "The diameter of a circle with radius" << Radius << "is" << 'D' << endl;
//output the result
return 0;
}
I think you better review the latest lesson in C++ first.
Here is the fix for your code:
double a = Const * Radius; //'A' == Const * Radius;
double c = 2 * Const * Radius; //'C' == 2 * Const * Radius;
double d = 2 * Radius; //'D' == 2 * Radius;
if (Code == 'A')
cout << "The area of a circle with radius" << Radius << "is" << a /*'A'*/<< endl;
else if (Code == 'C')
cout << "The circumference of a circle with radius" << Radius << "is" << c /*'C'*/ << endl;
else if (Code == 'D')
cout << "The diameter of a circle with radius" << Radius << "is" << d /*'D'*/ <<
endl;
'A', 'B', 'C' are values as well as 1, 3, 100, etc.
= is an assignment operator while == is compare operator.
Once you write something like 'A' == Radius; it will be evaluated to boolean (most likely false value) and that's it. In the same way you can write false; or 5; in your code. It does nothing at all.
You're not assigning the value but making it equal to that. by the you can assign a value to char.
#include <iostream>
#include <iomanip>
using namespace std;
/*run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
cout << fixed << setprecision(2);
char Code;
double Radius;
//promt for a code and A, C, or D
cout << "Please enter your code A, C, or D ";
// read the input
cin >> Code;
//promt user for value of radius
cout << "Please enter a value for the radius";
//read the input radius
cin >> Radius;
// based on the code, calculate the required computed result
double Const;
Const = 3.1416;
/* NO need of this. you can't assign value to a char.
'A' == Const * Radius;
you have used == sign whic is not assigning
'C' = 2 * Const * Radius;
'D' = 2 * Radius;
*/
if (Code == 'A'){
float temp; //change
temp=Const * Radius; //change
cout << "The area of a circle with radius" << Radius << "is" << temp << endl;
}
else if (Code == 'C'){
float temp;
temp=2 * Const * Radius;
cout << "The circumference of a circle with radius" << Radius << "is" << temp<< endl;
}
else if (Code == 'D'){
float temp;
temp=2 * Radius;
cout << "The diameter of a circle with radius" << Radius << "is" << temp << endl;
}
//Hope this helps.
//output the result
return 0;
}