Code will never be executed error

I'm a newbie to C++ and I'm not sure how to fix this error message I keep receiving.

[code]

#include <iostream>
#include <cmath>

using namespace std;

int main()

{ double coneradius,coneheight,sphereradius,side1,side2,pyrbase,pyrheight,circleradius; double ConeVolume,SphereVolume,HypTriangle,PyramidVolume,CircleArea,BaseArea,side3;
const double Pi= 3.141593;

int userchoice;

std::cout << "Please enter a number between 1 and 5.\n\n";
std::cout<< "1.Calculate the Cone Volume. \n";
std::cout<< "2.Calculate the Sphere Volume. \n";
std::cout<< "3.Calculate the Hypotenuse of a Triangle. \n";
std::cout<< "4.Calculate the Volume of a Pyramid. \n";
std::cout<< "5.Calculate the Area of a Circle. \n";
std::cin >> userchoice;

switch(userchoice)
{
case 1:
//Prompt user for dimensions of cone//

std::cout<< "Please enter the cone radius in inches: \n";
std::cin>> coneradius;

std::cout<< "Please enter the cone height in inches: \n";
std::cin>>coneheight;

//Find the volume of the cone//

ConeVolume=(1/3) * Pi * coneradius * coneradius * coneheight;

//Output the values//

std::cout<< "The volume of the cone is: "<<ConeVolume<< "inches^3.\n\n";

break;

case 2:

//Prompt user for sphere dimensions//

std::cout<< "Please enter the sphere radius in inches: \n";
std::cin>>sphereradius;

//Find the volume of the sphere//

SphereVolume=(4/3)* Pi * sphereradius* sphereradius* sphereradius;

//Output the values//

std::cout<< "The volume of the sphere is: "<<SphereVolume<< "inches^3.\n\n";
break;

case 3:

//Prompt user for sides of right triangle//

std::cout<< "Please enter side 1: \n";
std::cin>>side1;

std::cout<< "Please enter side 2: \n";
std::cin>>side2;

//Find hypotenuse of a right triangle//

side3=side1 * side1 + side2 * side2;

HypTriangle= sqrt (side3);

//Output the values//

std::cout<< "The hypotenuse of the right triangle is: "<<HypTriangle<<"inches\n\n";

break;
case 4:


//Prompt user for dimensions of Pyramid with square base//

std::cout<< "Please enter the base in inches: \n";
std::cin>> pyrbase;

std::cout<< "Please enter the height in inches: \n";
std::cin>>pyrheight;

//Find the volume of a Pyramid with a square base//

BaseArea=pyrbase * pyrbase;

PyramidVolume=1/3 * BaseArea * pyrheight;

//Output the values//

std::cout<< "The volume of the Pyramid is: "<<PyramidVolume<<"inches^3.\n\n";
break;
case 5:

//Prompt user for radius of a circle//
std::cout<< "Please enter the radius in inches: \n";
std:cin>>circleradius;

//Find Area of the Circle//

CircleArea=Pi * circleradius * circleradius;

//Output the values//

std::cout<<"The Area of the Circle is: "<<CircleArea<<"inches^2.\n\n";

break;


system ("pause"); Error Message: Code will never be executed

return (0);
}
}
Last edited on
What's the error message?

EDIT:

Oh whoops, it's in the title.

It would help if we knew what line it was on... but anyway, it's probably this:

BaseArea=pyrbase^2;

^ is not for exponents. It's a bitwise XOR operator that does not work with floating point values. If you want to square something do this:

BaseArea = pyrbase * pyrbase;
Last edited on
Thank you! Im receiving this new error message now :/

system("pause"); --------code will never be executed

return (0);
}
}
Last edited on
Please use the code tags. See http://www.cplusplus.com/articles/jEywvCM9/

It does seem like you have a structure like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main ()
{
    int a = 7;
    switch ( a )
    {
        case 7:
        std::cout << "Hello\n";
        break;
        std::cout << "world\n";
        return 0;
    }
}

My compiler fails to complain about that even though lines 11-12 will never execute. What does your compiler say?
You're using standard namespace so I think its better to use cout than std::cout
For system("pause"); i think you should include a line #include <cstdlib> in order to use it......
My compiler fails to complain about that even though lines 11-12 will never execute. What does your compiler say?
Not even a warning? I get a warning in VS 2010 for 'unreachable code'.
I am about 90% certain the error is from a visual studio compiler.
This warning is because those two limes of code are after a break statement inside of a switch. You probably meant to put those lines after the switch {...}
booradley60 wrote:
Not even a warning?

Ahh, there is explicit flag: -Wunreachable-code
With that GCC 4.4.7 and Clang 3.4.2 do mention the issue, but GCC 4.9.1 does not. Fittingly,
GCC bugzilla wrote:
-Wunreachable-code is broken and has been removed from GCC 4.5. Do not use it.


A well-formed switch statement should also have the default case in the end, if nothing else but to hint that we ignore all the unknown cases.
Topic archived. No new replies allowed.