C++ C++;警告:控件到达非无效函数的末尾

C++ C++;警告:控件到达非无效函数的末尾,c++,C++,我需要帮助修复程序。它没有运行。我不断得到警告控件到达非无效函数的末尾。我不知道如何修理它。请帮帮我。该程序假定要求球体的体积或表面积。我在最后2}收到警告 #include <iostream> #include <iomanip> #include <cmath> #include <math.h> using namespace std; char s = '\0'; const char SENTINEL = 's'; float ra

我需要帮助修复程序。它没有运行。我不断得到警告控件到达非无效函数的末尾。我不知道如何修理它。请帮帮我。该程序假定要求球体的体积或表面积。我在最后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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
字符s='\0';
const char SENTINEL='s';
浮动半径,回答;
void get_半径(float&);
浮子表面面积(浮子);
浮子体积(float);
浮子横截面(浮子);
常数浮点PI=3.14;
int main()
{

cout您的函数声明必须与返回的内容相匹配。您必须确保返回的值来自声明返回内容的函数

volume()和surface_area()正在使用cout打印内容,但没有返回任何内容

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;
}
浮动体积(浮动半径){
浮动答案;
答案=4.0/3.0*PI*pow(半径,3);

cout声明函数类型时,需要返回该类型的值。例如,函数:

    float volume (float radius) {}
    int veryBadFunction(int flag)
    {
        if (flag == 1) {
            return 1;
        }
    } 
需要一个return语句返回一个float类型的值

如果不需要函数实际返回某些内容,则将其声明为void以让编译器知道这一点。在这种情况下:

    void volume (float radius)
不过要小心,因为void函数不能返回值(尽管它们可以使用裸返回语句)

还要注意,跳过return语句的潜在路径可能会触发此错误。例如,我可以使用以下函数:

    float volume (float radius) {}
    int veryBadFunction(int flag)
    {
        if (flag == 1) {
            return 1;
        }
    } 

在这种情况下,即使函数中有一个return语句,只要flag的值不是“1”,它就会被跳过。这就是为什么错误消息的措词是control…

既不是
volume()
也不是
surface\u area()
return
任何东西。我当时如何修复它。我尝试了return 0;但它不起作用。当你编写代码时,从一些小而简单的可行方法开始,然后构建。如果你这样做的话,你会确切地知道问题所在,并且快速查看教科书会告诉你如何编写一个函数来修复它我能做些什么来修复它。我试着返回答案,但那没有用。你需要将它添加到两个浮点函数的末尾。我试过了。我认为它不需要返回任何东西。它只需要让我输入一个新的半径。它应该是另一个变量的半径,但我的老师说不需要。我忘了去掉,现在,当我返回答案时,我去掉了它;对于这两种情况,警告都消失了,但当我输入半径时,它不会给我答案。它只是重复按v或aI不需要它返回任何变量,但我需要它循环,让我重新开始,除非我按s