Question:

Visual C++ program doubt...?

by  |  earlier

0 LIKES UnLike

As a beginner in c++ I tried out a simple program on function overloading.While compiling I received the following warnings:

1.'=' : conversion from 'int' to 'float', possible loss of data

2. 'argument' : conversion from 'int' to 'float', possible loss of data

3. 'argument' : conversion from 'int' to 'float', possible loss of data

4. '=' : conversion from 'int' to 'float', possible loss of data

5. '=' : conversion from 'double' to 'int', possible loss of data

6. '=' : conversion from 'double' to 'int', possible loss of data

After running what ever input I give I get Zero as output in every case.

My program:

#include "stdafx.h"

#include <iostream>

using namespace std;

int circle(int a);

int rectangle(int a,int b);

int triangle(float a,float b);

int main()

{

int area1,a,b;

float area3,area2;

std::cout<<"Enter the radius of the circle"<<endl;

std::cin>>a;

area1= circle(a);

std::cout<<area1<<endl;

std::cout<<"Enter the dimensions of the rectangle"<<endl;

std::cin>>a>>b;

area2= rectangle(a,b);

std::cout<<area2<<endl;

std::cout<<"Enter the base and height of the triangle"<<endl;

std::cin>>a>>b;

area3= triangle(a,b);

std::cout<<area3<<endl;

}

int circle(int a)

{

int area1;

area1=3.14*a*a;

return (0);

}

int rectangle(int a,int b)

{

int area2;

area2=a*b;

return (0);

}

int triangle(float a,float b)

{

int area3;

area3=.5*a*b;

return (0);

}

The output should be:

Enter the radius of the circle 2

6

Enter the dimensions of the rectangle 2 4

8

Enter height and base of triangle 2.5 10

25

Instead the output comes:

Enter the radius of the circle 2

0

Enter the dimensions of the rectangle 2 3

0

Enter the base and height of the triangle 2.5 10

0

 Tags:

   Report

4 ANSWERS


  1. int means integer.  No decimal points, whole numbers only.

    float means floating point.  You could have numbers that aren&#039;t integers.

    All of your areas should be declared as floats.

    No need to keep putting std::cin and std:: cout since you&#039;ve done a using namespace std;

    Unless you&#039;ve been told that all the inputs will be integers (radius, dimensions of rectangle and triangle), I would make them floats also.  Your test seems to indicate that a float declaration is correct.

    In particular for your circle and triangle, your area calculations will be truncated since you declared area as int.  This is why area has to be float.

    And you don&#039;t seem to understand the point of a function return.  This is how you return a value to the calling program.  Since you only return(0), all your answers are zero.  Return area instead.

    I have to point out that while you have function calls, this is not overloading.  Overloading is having the same function name with different parameter types or different numbers of parameters.  Overloaded functions must return the same type (this is why all your areas would need to be floats).  I make the following suggestion to include everything you&#039;ve described doing:

    #include &quot;stdafx.h&quot;

    #include &lt;iostream&gt;

    using namespace std;

    float calcArea(char, float); // for circle

    float calcArea(char, float, float); // for triangle and rectangle

    int main()

    {

    float area,a,b;

    cout &lt;&lt; &quot;Enter the radius of the circle&quot;&lt;&lt;endl;

    cin &gt;&gt; a;

    area= calcArea(&#039;c&#039;, a);

    cout&lt;&lt;area&lt;&lt;endl;

    cout&lt;&lt;&quot;Enter the dimensions of the rectangle&quot;&lt;&lt;endl;

    cin&gt;&gt;a&gt;&gt;b;

    area= calcArea(&#039;r&#039;,a,b);

    cout&lt;&lt;area&lt;&lt;endl;

    cout&lt;&lt;&quot;Enter the base and height of the triangle&quot;&lt;&lt;endl;

    cin&gt;&gt;a&gt;&gt;b;

    area= calcArea(&#039;t&#039;,a,b);

    cout&lt;&lt;area&lt;&lt;endl;



    return(0);

    }

    float calcArea(char type, float radius)

    {

    // Don&#039;t need to use type at this point since only one calc

    return(3.14*radius);

    }

    float calcArea(char type, float a,float b)

    {

    if(type == &#039;r&#039;)

    return(a*b);

    else if(type == &#039;t&#039;)

    return(.5*a*b);

    else

    return(0);

    }


  2. You got a bunch of issues...

    All your area functions probably want to returns floats. You have a number of mismatches between ints and floats.

    You get zero each time because each function ends with &quot;return(0)&quot; which will always return 0 for the area. They should end with statements like return (area1)

  3. Well, the reason you get zero for all of your answers is because each of your functions to do the calculations end in &quot;return(0)&quot;, which (naturally) returns zero.

    As to the &quot;possible loss of data&quot; warnings, you need to understand that a float contains less bits for the value than an int.  (Or, at least, it can contain less.)  On my system, &quot;int&quot; is 32 bits, and &quot;float&quot; is 32 bits.  However, within the 32 bits in &quot;float&quot;, 8 bits are used for the exponent, leaving only 24 bits (1 sign bit, and 23 value bits).  If your int value is larger than 2^23-1, then you will lose bits in the conversion from into to float.

    - kb -

  4. I am only a C programmer but I did spot some mistakes. You must appreciate that an integer is a whole number and float is any decimal number inclusive. You have defined input data to be float but when you do calculations, you are converting them to integers. You have defined a,b as both integers and float. This could be the error line of your code.

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.