Error compiling C source code

I need help identifying a bug in my program that I wrote in C. Please keep in mind that I am still learning C. I am trying to implement what I have learned far. My IDE is MS visual studio 2010.

Here is the program, the program description is written as a comment:

/*The distance between two cities (in km) is input through the keyboard. Write a program to convert and print this distance in meters, feet, inches and centimeters*/ #include<stdio.h> #include<conio.h> //I have used #include<stdio.h> and #include<conio.h> above int main() { float km, m, cm, ft, inch ; clrscr(); printf("\nEnter the distance in Kilometers:"); scanf("%f", &km ); // conversions m=km*1000; cm=m*100; inch=cm/2.54; ft=inch/12; // displaying the results printf("\nDistance in meters =%f", m); printf("\nDistance in centimeters =%f", cm); printf("\nDistance in feet =%f", ft); printf("\nDistance in inches = %f", inch); printf("\n\n\n\n\n\n\nPress any key to exit the program."); getchar(); return 0; } Errors: 1>e:\my documents\visual studio 2010\projects\distance.cpp(32): error C2857: '#include' statement specified with the /YcStdAfx.h command-line option was not found in the source file 
+4
source share
3 answers

error C2857: the #include statement specified in the /YcStdAfx.h command-line option was not found in the source

This means that the compiler (VisualStudio 2010) forces you to include StdAfx.h, but in the source where you do not include it.

Try adding:

 #include <StdAfx.h> 

at the top of the source file.

+6
source

SanSS has already explained the error message. Let me briefly explain the warnings. The first scanf warning can be ignored at this point. The problem with scanf is that it is unsafe if you try to read a line in a pre-allocated C-line (for example, in a char or char array). You read in a float, which always has a fixed size (usually four bytes). Therefore, overflow cannot happen here.

The second warning refers to the expression inch=cm/2.54 . Literal 2.54 is regarded as a double precision value. Thus, cm/2.54 will also have a double meaning - the result of such a computational expression will always increase. Although cm is of type float (single precision), the result will be double . However, inch is of type float, so assigning = will implicitly empty the result from double to float . Since the variable a float has less accuracy, the result becomes less accurate. To avoid this warning, change the numeric literal so that the expression looks like this: inch = cm / 2.54f . This tells the compiler that 2.54 should be treated as a literal with a single float precision.

+3
source

to the warning C4996
compared to 2010, especially compared to 2012.
you should put the following code at the top of the file

 #define _CRT_SECURE_NO_WARNINGS 

And set the precompiled header option to "not use" on the project properties page.

+3
source

All Articles