C程序设计初等问题

发布于 2024-11-18 17:12:11 字数 850 浏览 5 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小红帽 2024-11-25 17:12:11

两个问题:

  1. 使用 scanf() 的常见问题,因为它在未读取的数字后面留下换行符,并且以下读取操作(此处的 getchar())读取它。
  2. 您不应传递指向 printf() 的指针,而应传递实际值。

Two problems:

  1. The usual problem with using scanf(), in that it leaves the newline after the number unread and the following read operation (the getchar() here) reads it.
  2. You shouldn't pass pointers to printf(), but the actual values.
苍景流年 2024-11-25 17:12:11

您试图将浮点数的地址打印为浮点数,您只想这样说:

printf("Amount in feet: %f\n", feet);

请注意 feet 上缺少地址 (&) 运算符。您想要对其他 printf 调用。

You're trying to print the addresses of your floats as floats, you just want to say this:

printf("Amount in feet: %f\n", feet);

Note the lack of an address (&) operator on feet. You want to apply similar changes to your other printf calls.

世态炎凉 2024-11-25 17:12:11

使用printf,您不需要给它float值的地址,您只需给它值。从 printf 调用中删除 &

您需要 scanf 中的地址,因为该函数会修改您传入的变量,但 printf 只需要这些值。实际上,printf 本质上是将指针重新解释为浮点数,这就是显示垃圾值的原因。

With printf, you don't give it the address of the float values, you just give it the values. Remove the &s from the printf calls.

You need the address in scanf because the function modifies the variables that you pass in, but printf just needs the values. As it is, the printf is essentially reinterpreting the pointers as floats, which is why you get the garbage values displayed.

々眼睛长脚气 2024-11-25 17:12:11

关于我无法保持屏幕向上,这是每个试图直接从 IDE 在图形环境中执行控制台程序的人的常见问题,特别是 Dev-C++。问题是没有 I/O 控制台,然后提供了一个控制台,但只是在程序运行时提供,并且由于程序速度很快,如果不在最后一次输入和输出后添加暂停,则不会有时间阅读输出。

许多 MS Windows Dev-C++ 用户添加了一个可怕的系统(“暂停”)。我总是建议,如果 Dev-C++ 无法提供带有选项“即使在程序结束后也保持打开状态”的 I/O 控制台,那么最好打开一个 shell(Windows 上的 cmd 或 powershell)并直接从那里运行您的程序。

关于输入问题,不幸的是 scanf-ing 有几个缓冲问题,因为给定格式无法识别的输入不会被丢弃并准备好进行下一次读取。例如,

scanf("%f", &aFloat);
scanf("%f", &theNextFloat); // in your case you have the extra getchar();

如果您将 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.

scanf("%f", &aFloat);
scanf("%f", &theNextFloat); // in your case you have the extra getchar();

won't stop for the second scanf if you write 1.25 4.5 as first input, since the 4.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 a
while( getchar() != EOF ) ; instead, and then to exit you need to hit Ctrl-D.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文