VS2022 code analysis error using uninitialized memory C++

Viewed 30

Here are the errors on the following code after the VS2022(v143) upgrade: enter image description here Could someone please suggest what is wrong here and how to fix it?

//Cricle properties problem
#include <iostream>
using namespace std;
float Qradius(float diameter)
{
    float radius = diameter / 2;
    return radius;
}

float Warea(float radius)
{
    float area = (radius *radius) *3.14;
    return area;
}

float Ecircumference(float diameter)
{
    float circumference = 3.14 * diameter;
    return circumference;
}

float Rarclength(float arcangle, float circumference)
{
    float arclength = (circumference *arcangle) / 360;
    return arclength;
}

int main()
{
    float diameter, arcangle;
    float area, circumference, arclength, radius;
    cout << "Type the diameter ";
    cin >> diameter;

    cout << "Type the arcangle ";
    cin >> arcangle;

    cout << "The radius of the circle is " << Qradius(diameter) << endl;
    cout << "The area is " << Warea(radius) << endl;
    cout << "The circumference is " << Ecircumference(diameter) << endl;
    cout << "The arc length is " << Rarclength(arcangle, circumference) << endl;
}
1 Answers

I solved the warnings and explained why they were coming up in the comments in the code. Also do not use using namespace std;

#include <iostream>

float Qradius(float diameter)
{
    float radius = diameter / 2;
    return radius;
}

float Warea(float radius)
{
    // if no f is specified, the compiler assumes it is a double
    // the warning tells you that it converts a double to float
    // which could lead to loss of data (C4244)
    float area = (radius *radius) * 3.14f;
    return area;
}

float Ecircumference(float diameter)
{
    // same as aboth
    float circumference = 3.14f * diameter;
    return circumference;
}

float Rarclength(float arcangle, float circumference)
{
    float arclength = (circumference *arcangle) / 360;
    return arclength;
}

int main()
{
    float diameter, arcangle;
    // area and arclength are unused (C4101)
    float /*area,*/ circumference, /*arclength,*/ radius;
    std::cout << "Type the diameter ";
    std::cin >> diameter;

    std::cout << "Type the arcangle ";
    std::cin >> arcangle;

    // radius and circumference is never set
    // and later used without setting any value (C6001)
    radius = Qradius(diameter);
    circumference = Ecircumference(diameter);

    std::cout << "The radius of the circle is " << radius << std::endl;
    std::cout << "The area is " << Warea(radius) << std::endl;
    std::cout << "The circumference is " << circumference << std::endl;
    std::cout << "The arc length is " << Rarclength(arcangle, circumference) << std::endl;
}
Related