Home  >  Q&A  >  body text

c/c++ 字符转换

输入a b输出为什么会有一个10结尾呢?
输出为:

97
a
32
 
98
b
10
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
    int chr;
    while ((chr = getchar()) != EOF)
    {
        cout<<chr<<endl;
        if (char(chr))
        {
            cout<<char(chr)<<endl;
        }
    }

    return 0;
}
PHP中文网PHP中文网2714 days ago574

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 15:09:53

    When you enter 97 and then retract, you actually entered three characters: 9, 7, newline, so the following output will appear:
    97
    57 -> ASCII code of the number 9
    9 -> Number 9 character
    55 -> Number 7 ASCII code
    7 -> Number 7 character
    10 -> Line break ASCII code
    -> Here is a Line break

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 15:09:53

    Generally, you need to add a get() to remove the newline character

    reply
    0
  • Cancelreply