与C编程中的前哨控制while循环有关的问题? [英] Problem related to sentinel-controlled while loop in C programming?

查看:77
本文介绍了与C编程中的前哨控制while循环有关的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
main()
{
    char character;
    while(character!='5')
    {
        printf("Enter a character :\n");
        character=getchar();
    }
}





如果我输入单个字符如4,那么printf语句打印两行intead,如下所示: -





If i enter a single character like "4", then the printf statement prints two lines intead of one, as shown below :-

Enter a character :
4
Enter a character :
Enter a character :







为什么两行输入一个字符:是打印的吗?

打印一行只需要什么代码?



我尝试过:



我试图使用scanf函数代替getchar,但我得到了相同的结果。




Why two lines of "Enter a character :" are printed ?
What should be the code for printing only one line ?

What I have tried:

I have tried to use scanf function in place of getchar, but i got the same results.

推荐答案

因为你没有输入'4'! :笑:

不,严重:你进入'4'并按下ENTER键 - 所以缓冲区则包含2个字符(或者在某些系统中可能包含3个字符):'2'和'' \\ n'换行符。正如您将注意到的,当您按下'4'时,您的程序没有立即响应 - 它一直等到您按下ENTER。然后你拿了一个角色 - '4' - 它不是'5',所以你拿下了下一个 - '\\ n' - 而且它也不是'5'。

那是因为getchar是基于行的 - 它总是等待用户在返回任何数据之前完成输入行。有一些方法,但它们取决于您正在使用的特定系统。

我可能会读取整行而不是单个字符,并将该行视为输入(或只是忽略换行的其余部分直到换行符。)
Because you didn't enter just a '4'! :laugh:
No, seriously: You entered '4' and pressed the ENTER key - so the buffer then contains 2 characters (or possibly three in some systems): the '2' and a '\n' newline character. As you will have noticed, your program didn't respond immediately when you pressed the '4' - it waited until you pressed ENTER as well. Then you fetched a character - the '4' - and it wasn't a '5', so you fetched the next - the '\n' - and it wasn't a '5' either.
That's because getchar is line based - it always waits for the user to finish typing the line before it returns any data. There are ways round that, but they depend on the specific system you are using.
I'd probably read a whole line instead of a single character, and look at the line as an input (or just ignore the rest of the line up to a newline).


你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



使用调试器,您将看到您的程序正在做什么。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

With the debugger, you will see exactly what your program is doing.


这篇关于与C编程中的前哨控制while循环有关的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆