简体   繁体   English

谁能检查程序并告诉我如何才能获得正确的输出?

[英]can anyone check the program and tell me how can i get the correct output?

#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);

}

after compiling the program the o/p is: 在编译程序之后,o / p是:

enter the first keyword 输入第一个关键字

I entered '$' and used ctrl+z to eof and 'enter' to continue the program. 我输入'$'并使用ctrl + z来eof并'enter'继续该程序。 But even without entering the second keyword the compiler printing the output as 但是即使没有输入第二个关键字,编译器也会将输出打印为

enter your second keyword 输入第二个关键字

it is '$' greater than '->' as i expected 正如我预期的那样,它比“->”大“ $”

Can anyone help with this program? 任何人都可以帮助这个计划吗?

Sorry if any grammatical or phrase errors. 对不起,如果有任何语法或短语错误。

getchar() is taking extra inputs \\n too when you press enter which is still there in buffer.You need to absorb this extra character to let second getchar work.Try calling getchar twice like below- 当你按下仍然存在于缓冲区中的entergetchar()正在进行额外的输入\\n 。你需要吸收这个额外的字符让第二个getchar尝试两次调用getchar如下

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

other than above option you can use standard function setvbuf to control buffering.One more option is there(personally i do not prefer this to avoid undefined behaviour) is using fflush(stdin) 除了上面的选项,你可以使用标准函数setvbuf来控制缓冲。还有一个选项(个人我不喜欢这个以避免未定义的行为)使用fflush(stdin)

The problem is that your newline gets buffered and passed onto the next getchar call. 问题是你的换行符被缓冲并传递给下一个getchar调用。 You need to deal with the buffered newline in perhaps the following way: 您需要以下列方式处理缓冲换行:

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

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

The space before %c is a common idiom that tells scanf to ignore any space before the following character which in our case also includes the newline. %c之前的空格是一个常见的习惯用法,它告诉scanf在后面的字符之前忽略任何空格,在我们的例子中也包括换行符。 It is not necessary in the first instance in this particular case but vital in the second. 在第一种情况下,在这种特殊情况下并非必要,但在第二种情况下则不是必需的。

You also don't need the stdlib include and you can return without the brackets, like return 0; 你也不需要stdlib包括,你可以return去掉括号,如return 0;

Actually, if you feel like experimenting and you are on a Linux terminal, you can set the terminal in raw mode which will remove any buffer and parsing abilities that the terminal would provide for you. 实际上,如果您想要进行实验并且您使用的是Linux终端,则可以将终端设置为原始模式 ,这将删除终端为您提供的任何缓冲区和解析功能。 To do that run /bin/stty raw in the terminal. 为此,在终端中运行/bin/stty raw

This way there will be no buffering and you won't have to worry about any buffered newlines. 这样就不会有缓冲,您也不必担心任何缓冲的换行符。 The output on the console will look funny though (I've entered here a and b ) unless you also regulate that with placing strategically carriage returns ( \\r ): 在控制台上输出看起来很有趣,但(我在这里已经进入了ab )除非您还调整与战略性放置回车( \\r ):

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

I've used your original code for the above. 我已经使用了上面的原始代码。

To restore it, just run /bin/stty cooked 要恢复它,只需运行/bin/stty cooked

C takes the '\\n' as the second character. C将'\\ n'作为第二个字符。 What you can do is input both the character in the same line as 你可以做的是输入同一行中的字符

$ @ $ @

or else modify your program by not using the getchar() function 或者不使用getchar()函数修改程序

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

Notice the white space between " and %c 注意%c之间的空格

This does the trick. 这样就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我没有得到此代码的 output(反转字符串)。 谁能告诉我为什么? - I am not getting the output for this code(Reversing a string). Can anyone tell me why? 谁能帮助我更正本程序的编写并运行该程序? - Can anyone help me to correct my writing of this procedure and run this program? 谁能告诉我为什么当我尝试在菜单驱动的数组操作程序中调用 Insert 或 Delete 函数时我的程序会崩溃? - Can anyone tell me why my program crashes when I try to call the Insert or Delete function in a menu driven array manipulation program? 谁能告诉我为什么会出现此错误...? - Can anyone tell me why am i getting this error...? 此输出为3谁能告诉我如何? - The output of this is 3 can anybody tell me how? 谁能告诉我这段代码是如何工作的? - Can anyone tell me how this piece of code works? c中的分段错误。 谁能告诉我如何解决它? - segmentation fault in c. Can anyone tell me how to solve it? 任何人都可以告诉我这是什么意思 - Can anyone tell me what does this mean 谁能解释这个程序的输出 - Can anyone explain output of this program 谁能告诉我如何完成此操作:数组与指针的关系代码 - Can anyone tell how can I get this done: code of relation of array with pointers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM