2011-06-29 195 views
4

我需要帮助来识别我的程序中使用C编写的错误。请记住,我仍在学习C语言。我正在尝试实施我所学到的知识。我的IDE是MS Visual Studio 2010中编译C源代码时出错

这里是程序,程序说明写成评论:

/*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 

回答

6

错误C2857:与/YcStdAfx.h命令 - 指定“的#include”声明在源代码中找不到行选项

这意味着编译器(VisualStudio 2010)强制包含StdAfx.h,但在源代码中不包含它。

尝试增加:

#include <StdAfx.h> 

在源文件的顶部。

3

SanSS已经解释了错误信息。让我简要解释警告。关于scanf的第一个警告现在可以忽略。 scanf的问题在于,如果尝试将字符串读入预先分配的C字符串(例如char数组或字符指针),它是不安全的。你正在阅读一个总是有固定大小(通常是四个字节)的浮动。所以这里不会发生溢出。

第二个警告是关于表达式inch=cm/2.54。字面值2.54被视为双精度值。所以cm/2.54也将是一个双重价值 - 这样的计算表达式的结果将始终是upcast。虽然cm的类型为float(单精度),但结果将为double。但是,inch的类型为float,所以分配=将隐式地将结果从double向下舍入为float。由于float变量的精度较低,结果会变得不那么精确。要避免此警告,请更改数字文字,以便表达式如下所示:inch = cm/2.54f。这告诉编译器2.54将被视为单个精度float文字。

3

以警告C4996
在VS 2010年,特别是在2012年VS
你必须把下面的代码在文件

#define _CRT_SECURE_NO_WARNINGS 

的顶部,设置预编译头选项设置为“不在项目的属性页面使用“。