在 C 中正确使用 EOF

发布于 2024-11-10 11:55:39 字数 1052 浏览 5 评论 0原文

我是编程新手,我真的很想学习编写一个像样的程序。我不太确定如何使用 EOF。我的程序已编译,当我运行它时,除了 EOF 部分之外,它工作正常。该程序应该向主函数返回 -1 值(使用 CTRL+z 后),并打印一条语句,然后关闭程序。 -1 与 EOF 本身的 -1 是不同的值。

 #include <stdio.h>
//Function Declaration
int inputFunction (int num);

int main (void)
{
    //Local Declarations
    int num;
    do
    {    
    inputFunction (num);

    if (num < 0 || num > 100)
            inputFunction (num);
    else
        inputFunction (num);
    }while (num != -1);
    return 0;
}
int inputFunction (int num)
{
    int rc;

    do
    {
    printf("Enter an integer from 0 to 100: \n");
    rc = scanf("%d", &num);
       {
       if (rc != EOF)
              {
              if (num < 0 || num >100)
                 {
                 printf("ERROR\n");
                 return num;
                 }
              else
                  {
                 return num;
                 }
              }
       else
           {
           num = -1;
           return num;
       }
       }  
    }while (rc != EOF);
    }

I'm new to programming and I really want to learn writing a decent program. I am not exactly sure how to use EOF. My program was compiled and when I run it, it works fine except for the EOF part. The program is supposed to return a -1 value (after using CTRL+z) to the main function, and will print a statement then will close the program. The -1 is a different value from the -1 of EOF per se.

 #include <stdio.h>
//Function Declaration
int inputFunction (int num);

int main (void)
{
    //Local Declarations
    int num;
    do
    {    
    inputFunction (num);

    if (num < 0 || num > 100)
            inputFunction (num);
    else
        inputFunction (num);
    }while (num != -1);
    return 0;
}
int inputFunction (int num)
{
    int rc;

    do
    {
    printf("Enter an integer from 0 to 100: \n");
    rc = scanf("%d", &num);
       {
       if (rc != EOF)
              {
              if (num < 0 || num >100)
                 {
                 printf("ERROR\n");
                 return num;
                 }
              else
                  {
                 return num;
                 }
              }
       else
           {
           num = -1;
           return num;
       }
       }  
    }while (rc != EOF);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

七堇年 2024-11-17 11:55:39

变量 num 从未在 main 中赋值。您调用 inputFunction 并且它返回一个值,但您忽略它返回的值。

The variable num is never assigned in main. You call inputFunction and it returns a value, but you ignore the value it returns.

离不开的别离 2024-11-17 11:55:39

CTRL-Z (^Z) 的 ASCII 值是 26(十进制),在 DOS 中经常用作文件结束标记。请记住,您在 main 中将变量 num 作为按值调用 不作为参考,因此当您比较它时它会包含垃圾值。

The ASCII value for CTRL-Z (^Z), which is often used as an end-of-file marker in DOS is 26 (decimal).And remember you are passing variable num in main as call by value not as reference so it would contain garbage value when you compare it.

ζ澈沫 2024-11-17 11:55:39

正如 Gabe 所说,您需要分配 inputFunction() 的返回值才能使用它 - 因为函数中的变量具有本地范围。因此,每次在 main() 中调用 inputFunction() 时,都应该这样做:

num = inputFunction(num);

否则,当程序执行返回到 main() 时,inputFunction() 中的 num 值会丢失。这是您的程序未按预期执行的主要原因。

您的代码也存在一些逻辑问题。在 main() 的第一个 if/else 语句中,两个语句产生完全相同的结果。其他行为可能也不完全是您想要做的。

我认为您可能会受益于用笔和纸规划您希望程序执行的操作。一种方法是用箭头等“绘制出”程序将执行的操作(流程图)。或者,您可以从 1-2 句话的目标描述开始,然后逐渐将其扩展为越来越详细,直到您拥有程序将执行的一系列任务,然后您可以将其转换为代码。

As Gabe has stated you need to assign the return value of inputFunction() in order to use it - because variables in functions have local scope. So every time you are calling inputFunction() in main() you should be doing it like so:

num = inputFunction(num);

Otherwise the value of num in inputFunction() is lost when program execution returns to main(). This is the main reason your program is not performing as expected.

There are also some logic problems with your code. In the first if/else statement in main(), both statements produce exactly the same result. Other behaviour might not be exactly what you want to do either.

I think you might benefit from planning out with pen and paper what you want the program to do. One way is to "map out" what the program will do with arrows etc (a flowchart). Or you can start with a 1-2 sentence description of the goal, and then gradually expand that into more and more detail until you have a series of tasks that the program will do, which you can then convert to code.

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