【问题标题】:can anyone check the program and tell me how can i get the correct output?谁能检查程序并告诉我如何获得正确的输出?
【发布时间】:2013-08-06 07:11:09
【问题描述】:
#include <stdio.h>
#include <stdlib.h>

int main()

{
    
    char a,b;
    
    printf("enter the firstkeyword \n");
    a = getchar();
    printf("enter your second keyword \n");
    b = getchar();
    
    if(a>b)
    {
    printf("it is '%c' greater than '%c' as i expected \n",a,b);
    }
    else if (b>a)
    {
    printf("here'%c'is greater than '%c' \n",b,a);
    }
    else
    {
    printf("dont type the same character twice in next session \n");
    }
    return(0);
    
}

程序编译后的o/p为:

输入第一个关键字

我输入了 '$' 并使用 ctrl+z 到 eof 和 'enter' 继续程序。但即使没有输入第二个关键字,编译器也会将输出打印为

输入您的第二个关键字

正如我所料,它是 '$' 大于 '->'

谁能帮助这个程序?

如有任何语法或短语错误,请见谅。

【问题讨论】:

  • 请打开所有编译器警告,并学习使用调试器(在 Linux 上:使用gcc -Wall -g 编译,使用gdb 进行调试)。
  • @chintusrikanth 正在读取的下一个字符是 '\n',因为您也在按 Enter 键
  • @BasileStarynkevitch:你在 OP 的源代码中发现任何错误吗?

标签: c compilation output getchar


【解决方案1】:

getchar() 也接受了额外的输入 \n 当你按下 enter 时,它仍然存在于缓冲区中。你需要吸收这个额外的字符让第二个 getchar 工作。尝试调用 getchar 两次,如下所示-

char just_to_consume;
a = getchar();
just_to_consume = getchar();
printf("enter your second keyword \n");
b = getchar();
just_to_consume = getchar();

除了上述选项之外,您还可以使用标准函数setvbuf 来控制缓冲。还有一个选项(我个人不喜欢这样做以避免未定义的行为)使用fflush(stdin)

【讨论】:

  • 非常感谢 dayal rai...如果您不介意可以更简短地解释一下吗?如果输入关键字超过2个怎么办,有没有什么捷径可以减少编码长度??实际上,我正在从那本书中的“c from dummies”中学习 c,他们提到使用 ctrl + z 之类的 eof(c 中的第 169 页用于傻瓜)可以运行程序,但它不起作用。兄弟能告诉我原因吗??
  • 也许setvbufwhat does getchar actually do 会有所帮助。减少编码长度取决于实践、定义的选择、优化以及当然的经验。不必着急。ctrl+z 用于 EOF 但我在你的代码中看不到它的任何用途。请看看here
【解决方案2】:

问题是您的换行符被缓冲并传递到下一个getchar 调用。您可能需要通过以下方式处理缓冲的换行符:

printf("enter the firstkeyword \n");
scanf(" %c", &a);

printf("enter your second keyword \n");
scanf(" %c", &b);

%c 之前的空格是一个常见的习惯用法,它告诉scanf 忽略后面的字符之前的任何空格,在我们的例子中也包括换行符。在这种特殊情况下,在第一种情况下没有必要,但在第二种情况下至关重要。

您也不需要包含stdlib,您可以在不带括号的情况下使用return,例如return 0;

实际上,如果您想进行实验并且您使用的是 Linux 终端,您可以将终端设置为 raw mode,这将删除终端为您提供的任何缓冲区和解析能力。为此,请在终端中运行 /bin/stty raw

这样就没有缓冲,您不必担心任何缓冲的换行符。控制台上的输出看起来很有趣(我在这里输入了ab),除非您还通过策略性地放置回车符(\r)来调节它:

$ ./a.out 
         enter the firstkeyword 
                               aenter your second keyword 
                                                         bhere'b'is greater than 'a' 

我在上面使用了你的原始代码。

要恢复它,只需运行/bin/stty cooked

【讨论】:

  • @chintusrikanth 什么没用?在我的情况下,新行没有传递给下一个调用,我能够得到以下输出:enter the firstkeyword b enter your second keyword a it is 'b' greater than 'a' as i expected
  • 对不起@Nobilis 我正在使用 orwell devc++。如果你能告诉我在 windows 中的 dev c++,请提前感谢
  • @chintusrikanth 在scanf 语句的情况下编译器或平台无关紧要,请告诉我什么不起作用?
  • @Nobilis 我认为您需要关注与说明符一起使用的space。我猜OP没有遵循space
  • @Dayalrai 我已经解释了为什么那里需要空间,希望能为 OP 澄清一些事情。
【解决方案3】:

C 将 '\n' 作为第二个字符。你可以做的是在同一行输入两个字符

$@

或者不使用 getchar() 函数来修改你的程序

char a,b;
printf("enter the firstkeyword \n");
scanf(" %c",&a);
printf("enter your second keyword \n");
scanf(" %c",&b);

注意 "%c

之间的空白

这就是诀窍。

【讨论】:

    猜你喜欢
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多