Warnings about conversions
I keep getting warnings about variable conversions, but dunno how to fix them. Can someone have a look and possibly fix it for me (very easy code)?
Warnings:
Quote:
main.cpp(10) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
main.cpp(19) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
main.cpp(57) : warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
main.cpp(59) : warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
Code:
Code:
#include <iostream.h> // input output
#define PI 3.14159
// Function for calculating the area of a circle
float area (float r)
{
float a;
a = PI * r * r;
return(a);
}
// Function for calculating the circumference of a circle
float circumference (float r)
{
float a;
a = 2 * PI * r;
return(a);
}
// Program start
int main ()
{
// Declare needed variables
int type;
float answer;
int n = 0;
int radius;
//Keep looping until a valid choice has been made
do {
cout << "What would you like to calculate?\n";
cout << "1. Circumference\n";
cout << "2. Area\n";
cout << "Enter your choice: ";
cin >> type;
// Check if a valid choice has been made
if (type == 1 || type == 2) {
// A valid choice has been made, exit loop
break;
} else {
// Invalid choice, display error
cout << "Invalid Choice! Please try again\n\n";
}
} while (n != 1);
// Ask for radius
cout << "Please enter a radius for the circle: ";
cin >> radius;
// Use the functions to calculate
if (type == 1) {
answer = circumference(radius);
} else if (type == 2) {
answer = area(radius);
}
//Display answer
cout << "The answer is: " << answer;
//Exit Program
return 0;
}
Re: Warnings about conversions
A few corrections to the code, the first line is especially important.
Code:
#include <iostream> // input output
using namespace std;
const float PI = 3.14159f;
// Function for calculating the area of a circle
// NOTE: you don't need to store the
// result, you can directly return it.
// And since this function is so small
// you can even inline it (for faster code)
inline float area (float r)
{
return PI * r * r;
}
// Function for calculating the circumference of a circle
// NOTE: same here
inline float circumference (float r)
{
return 2 * PI * r;
}
// Program start
int main ()
{
// Declare needed variables
int type;
float answer;
int n = 0;
float radius;
//Keep looping until a valid choice has been made
do {
cout << "What would you like to calculate?\n";
cout << "1. Circumference\n";
cout << "2. Area\n";
cout << "Enter your choice: ";
cin >> type;
// Check if a valid choice has been made
if (type == 1 || type == 2) {
// A valid choice has been made, exit loop
break;
} else {
// Invalid choice, display error
cout << "Invalid Choice! Please try again\n\n";
}
} while (n != 1);
// Ask for radius
cout << "Please enter a radius for the circle: ";
cin >> radius;
// Use the functions to calculate
if (type == 1) {
answer = circumference(radius);
} else if (type == 2) {
answer = area(radius);
}
//Display answer
cout << "The answer is: " << answer;
//Exit Program
return 0;
}