【问题标题】:C++ - I am having trouble with the output as I am getting numbers and lettersC++ - 我在输出数字和字母时遇到问题
【发布时间】:2017-07-17 03:06:57
【问题描述】:

我对 C++ 和一般编程还是很陌生,所以要温柔。

给定圆心和圆上的一个点,您可以使用此公式求圆的半径。编写一个程序,提示用户输入圆心和圆上的一个点。然后程序应该输出圆的半径、直径、周长和面积。您的程序必须至少具有以下功能:

calculateRadius:接收圆心和点的 x-y 坐标(由用户输入)并计算点之间的距离。该值作为圆的半径返回。

calculateArea:接收圆的半径,计算并返回圆的面积。

calculatePerimeter:接收圆的半径,计算并返回圆的周长。

输出应该清楚地显示结果圆的半径、面积和周长。

这是我目前所拥有的,它给了我数字和字母作为输出。

#include <iostream>
#include <cmath>
#define PI 3.141592

using namespace std;

int calculateRadius(float x1, float x2, float y1, float y2);
int calculateArea(float radius);
int calculatePerimeter(float radius);

int main()
{
    float x1;
    float x2;
    float y1;
    float y2;

    cout << "Please enter the first X Coordinate: " << endl;
    cin >> x1;
    cout << "Please enter the first Y Coordinate: " << endl;
    cin >> y1;

    cout << "Please enter the second X Coordinate: " << endl;
    cin >> x2;
    cout << "Please enter the second Y Coordinate: " << endl;
    cin >> y2;

    cout << "The radius of the circle is: " << calculateRadius << endl;
    cout << "The diameter of the circle is: " << endl;
    cout << "The cirfumference of the circle is: " << calculatePerimeter << endl;
    cout << "The area of the circle is: " << calculateArea << endl;

    system("pause");
    return 0;
}

int calculateRadius(float x1, float x2, float y1, float y2)
{
    float distance;
    float radius;

    distance = sqrt((pow(x2 - x1, 2)) + (pow(y2 - y1, 2)));

    radius = (distance / 2);

    return radius;
}

int calculateArea(float radius)
{
    float area;

    area = PI * pow(radius, 2);

    return area;
}

int calculatePerimeter(float radius)
{
    float perimeter;

    perimeter = 2 * PI * radius;

    return perimeter;
}

【问题讨论】:

  • 你的问题是什么?这是相当广泛的。
  • @Carcigenicate 当我运行代码时,我得到了数字和字母,这显然是错误的。我不知道我做错了什么。所以我的问题是,是否有人能找到我得到错误答案的原因。
  • @Jcarlton55 如果您有任何更新(如您的评论),最好用清晰的信息编辑您自己的问题。

标签: c++


【解决方案1】:

您还需要将函数的返回类型从 int 更改为 float,因为您使用的是单精度。

【讨论】:

    【解决方案2】:

    你需要为函数指定参数

    #include <iostream>
    #include <cmath>
    #define PI 3.141592
    
    using namespace std;
    
    int calculateRadius(float x1, float x2, float y1, float y2);
    int calculateArea(float radius);
    int calculatePerimeter(float radius);
    
    int main()
    {
        float x1;
        float x2;
        float y1;
        float y2;
    
        cout << "Please enter the first X Coordinate: " << endl;
        cin >> x1;
        cout << "Please enter the first Y Coordinate: " << endl;
        cin >> y1;
    
        cout << "Please enter the second X Coordinate: " << endl;
        cin >> x2;
        cout << "Please enter the second Y Coordinate: " << endl;
        cin >> y2;
    
        cout << "The radius of the circle is: " << calculateRadius(x1,x2,y1,y2) << endl;
        cout << "The diameter of the circle is: " << endl;
        cout << "The cirfumference of the circle is: " << calculatePerimeter(x1) << endl;
        cout << "The area of the circle is: " << calculateArea(x1) << endl;
    
        system("pause");
        return 0;
    }
    
    int calculateRadius(float x1, float x2, float y1, float y2)
    {
        float distance;
        float radius;
    
        distance = sqrt((pow(x2 - x1, 2)) + (pow(y2 - y1, 2)));
    
        radius = (distance / 2);
    
        return radius;
    }
    
    int calculateArea(float radius)
    {
        float area;
    
        area = PI * pow(radius, 2);
    
        return area;
    }
    
    int calculatePerimeter(float radius)
    {
        float perimeter;
    
        perimeter = 2 * PI * radius;
    
        return perimeter;
    }
    

    【讨论】:

    • 这是什么意思?如果这是一个愚蠢的问题,我很抱歉。 @兰道夫
    • cout 只是打印函数的地址,当你想用参数调用函数时,就像在 calculateRadius() 中你需要用 calculateRadius(x1,x2,y1,y2 ),而不仅仅是计算半径。您需要指定需要从哪些值计算半径,以便函数使用这些参数计算半径并为您返回。
    猜你喜欢
    • 2017-06-07
    • 2021-02-08
    • 2020-09-21
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    相关资源
    最近更新 更多