C4244 warning

i can not gid rid of these 3 errors

i'm new to this so please take it easy

thanks




#include <stdio.h>
#include <math.h>

int main()
{

int i, n;
float x, val, sum, t;

printf(" Enter the value for x : ");
scanf("%f", &x);

n = 10;
val = x;
x = x * 3.14159 / 180;
t = x;
sum = x;


for (i = 1; i < n + 1; i++)
{
t = (t * pow((double)(-1), (double)(2 * i - 1)) * x * x) /
(2 * i * (2 * i + 1));

sum = sum + t;
}


printf("\nsine value of %f is : %8.4f\n", val, sum);

getchar();
getchar();

return 0;
}
What 3 errors? I'm afraid my telepathy isn't working today, so you'll have to go to the trouble of telling me the old-fashioned way.
hahaha

Error 1 error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. e:\win32project4\win32project4\source.cpp 11 1 Win32Project4

Warning 2 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data e:\win32project4\win32project4\source.cpp 15 1 Win32Project4

Warning 3 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data e:\win32project4\win32project4\source.cpp 22 1 Win32Project4



Firstly, to nitpick, none of those are errors, they are warnings. Having said that, you're doing the right thing by trying to address them; far too many people ignore compiler warnings, resulting in bugs in their code.

The first warning can be ignored. It's Microsoft's way of trying to frighten you into using their own variant of C, rather than the standard libraries.

The other warnings are fairly self-explanatory: on lines 15 and 22, you're taking a value that's stored as a double, and copying the value into something that's only a float. This results in losing information, because a float uses less bytes than a double. The conversion is handled by reducing the precision of the value, so that it can be stored in the smaller type.

In line 15, this is happening because a floating point literal, such as 3.14159, is always of type double, unless you add qualifiers to make it something else. The whole expression x * 3.14159 / 180 is therefore considered to have type double. You then assign the value of that expression to x, which is a float.

At line 22, it's similar - the value returned by pow((double)(-1), (double)(2 * i - 1)) is a double, so therefore the type of the entire expression on the RHS is double. t is a float.
so change the float to doubles for all the program?? i'm new to this so please excuse my ignorance
i have got rid of the warnings

but i still have the error c4996 scanf
Maybe you did not read well what MikeyBoy wrote:

The first warning can be ignored. It's Microsoft's way of trying to frighten you into using their own variant of C, rather than the standard libraries.


So, just ignore it!
i fixed it thanks guys
I recommend using iostream like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstdio>
#include <math.h>
#define PI 3.14159265
using namespace std;
int main(){
int i, n;
double x, val, sum, t;

cout<<" Enter the value for x : ";
cin>>x;

n = 10; val = x; x = x * PI / 180; t = x; sum = x;

for (i = 1; i < n + 1; i++)
{
t = (t * pow((double)(-1), (double)(2 * i - 1)) * x * x) /
(2 * i * (2 * i + 1));

sum = sum + t;
}
cout<<"\nsine value of "<<val<<" is : "<<sum<<endl;

getchar();
getchar();

return 0;
}
Last edited on
Topic archived. No new replies allowed.