当前位置:C++技术网 > 精选软件 > C++ Primer Plus 6th 6.10 复习题 第2题 cout的++ch 和 ch+1的区别

C++ Primer Plus 6th 6.10 复习题 第2题 cout的++ch 和 ch+1的区别

更新时间:2019-05-05 09:57:33浏览次数:1+次

题目:
2.在程序清单6.2中,用ch+1替换++ch将发生什么情况呢?
程序清单6.2:

#include <iostream>
int main()
{
    char ch;
    std::cin.get(ch);
    while(ch!='.')
    {
        if(ch == '\n')
            std::cout<<ch; // done if newline
        else
            std:cout<<++ch; // done otherwise
        std::cin.get(ch);
    }
    // try ch + 1 instead of ++ch for interesting effect
    std::cout<<'\nPlease excuse the slight confusion.\n'
    //std::cin.get();
    //std::cin.get();
    return 0;
}


答案:  
++ch 和 ch+1得到的数值相同。但是++ch的类型为char,将作为字符打印,而ch+1是int类型(因为将char和int相加),将作为数字打印。

C++技术网辅导详解解答:
    答案解释的很清楚了,就不赘述了。