C程序设计初等问题
#include <stdio.h>
#include <math.h>
int main (void)
{
float inches;
printf("Enter the number of inches\n");
scanf("%f\n",&inches);
float feet;
float cm;
float yards;
float meter;
feet = 12 * inches;
cm = 2.54 * inches;
yards = 36 * inches;
meter = 39.37 * inches;
printf("Amount in feet: %f\n", &feet);
printf("Amount in cm: %f\n", &cm);
printf("Amount in yards: %f\n", &yards);
printf("Amount in meters: %f\n", &meter);
getchar();
return 0;
}
我正在使用 Dev c++
这是我在 C 中处理的问题吗?基本上输入以英寸为单位的数字,然后以厘米、码、米和英尺为单位打印数量。这给了我 0.0000 或所有这些的东西,或者实际上是它的时间。我无法保持屏幕向上,我认为这就是 getchar() 的目的,但我一定是弄错了。任何帮助都很棒。谢谢!
编辑1
将 dev c++ 保留在屏幕上而不是在我放入东西后关闭怎么样?当屏幕弹出时,我还必须在它返回任何内容之前输入 2 个值?为什么??
#include <stdio.h>
#include <math.h>
int main (void)
{
float inches;
printf("Enter the number of inches\n");
scanf("%f\n",&inches);
float feet;
float cm;
float yards;
float meter;
feet = 12 * inches;
cm = 2.54 * inches;
yards = 36 * inches;
meter = 39.37 * inches;
printf("Amount in feet: %f\n", &feet);
printf("Amount in cm: %f\n", &cm);
printf("Amount in yards: %f\n", &yards);
printf("Amount in meters: %f\n", &meter);
getchar();
return 0;
}
I'm using Dev c++
Is the problem i'm problem I'm working on in C. Basically enter in a number in inches then print amount in cm,yards,meters and feet. This is giving me 0.0000 or something for all of them or actually the time it is up. I can't keep the screen up and I thought that was the purpose of getchar() but I must have been mistaken. Any help is great. Thanks!
EDIT 1
What about as far as keeping dev c++ on the screen instead of closing out after I put stuff in? I am also having to put 2 values in before it returns in anything when the screen pops up? Why??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
两个问题:
scanf()
的常见问题,因为它在未读取的数字后面留下换行符,并且以下读取操作(此处的getchar()
)读取它。printf()
的指针,而应传递实际值。Two problems:
scanf()
, in that it leaves the newline after the number unread and the following read operation (thegetchar()
here) reads it.printf()
, but the actual values.您试图将浮点数的地址打印为浮点数,您只想这样说:
请注意
feet
上缺少地址 (&
) 运算符。您想要对其他printf
调用。You're trying to print the addresses of your floats as floats, you just want to say this:
Note the lack of an address (
&
) operator onfeet
. You want to apply similar changes to your otherprintf
calls.使用
printf
,您不需要给它float
值的地址,您只需给它值。从printf
调用中删除&
。您需要
scanf
中的地址,因为该函数会修改您传入的变量,但printf
只需要这些值。实际上,printf 本质上是将指针重新解释为浮点数,这就是显示垃圾值的原因。With
printf
, you don't give it the address of thefloat
values, you just give it the values. Remove the&
s from theprintf
calls.You need the address in
scanf
because the function modifies the variables that you pass in, butprintf
just needs the values. As it is, theprintf
is essentially reinterpreting the pointers as floats, which is why you get the garbage values displayed.关于我无法保持屏幕向上,这是每个试图直接从 IDE 在图形环境中执行控制台程序的人的常见问题,特别是 Dev-C++。问题是没有 I/O 控制台,然后提供了一个控制台,但只是在程序运行时提供,并且由于程序速度很快,如果不在最后一次输入和输出后添加暂停,则不会有时间阅读输出。
许多 MS Windows Dev-C++ 用户添加了一个可怕的
系统(“暂停”)
。我总是建议,如果 Dev-C++ 无法提供带有选项“即使在程序结束后也保持打开状态”的 I/O 控制台,那么最好打开一个 shell(Windows 上的 cmd 或 powershell)并直接从那里运行您的程序。关于输入问题,不幸的是 scanf-ing 有几个缓冲问题,因为给定格式无法识别的输入不会被丢弃并准备好进行下一次读取。例如,
如果您将
1.25 4.5
作为第一个输入,则不会停止第二个 scanf,因为4.5
已经可用于下一个 scanf。在您的情况下,它是留在缓冲区中的换行符,并且由于 getchar 已找到它,因此不需要等待输入。你可以使用while( getchar() != EOF ) ;
相反,然后要退出,您需要按 Ctrl-D。About I can't keep the screen up, it's a common problem to everyone trying to execute a console program in a graphical environment directly from the IDE, in particular Dev-C++. The problem is that there's no console for I/O, then one is provided but just for the time the program is running, and since programs are fast, if you do not add a pause after your last input and output, you won't have the time to read the output.
Many MS Windows Dev-C++ users add an horrorific
system("pause")
. I always suggest that, if Dev-C++ is not able to provide a console for I/O with the option "keep it opened even after the program ends", then it is better you open a shell (cmd or powershell on windows) and run your program directly from there.About the input problem, unluckly scanf-ing has several buffering problem since the input that is not recognized for the given format is not discarded and is ready for the next reading. E.g.
won't stop for the second scanf if you write
1.25 4.5
as first input, since the4.5
is already available for the next scanf. In your case it was a newline that was left in the buffer and since getchar has found it, it does not need to wait for input. You could use awhile( getchar() != EOF ) ;
instead, and then to exit you need to hit Ctrl-D.