如何将int变量转换为ascii单词

时间:2015-11-27 06:48:24

标签: c++

大家下午好,我的程序中有一些问题,我想把int变量转换成ascii字。但是当我编译它时,它只打印出第一个单词。这是我的程序

#include <iostream>
#include <conio.h>
using namespace std;
int main() {
    int progd1=72, progd2=69, progd3=76, progd4=76, progd5=79;
    char H=progd1, E=progd2, L=progd3, l=progd4, O=progd5;
    cout<<"\nknown :ascii1=72\n";
    cout<<"         ascii2=69\n";
    cout<<"         ascii3=76\n";
    cout<<"         ascii4=76\n";
    cout<<"         ascii5=79\n";
    cout<<"\nIf all that variable spliced together, "
    <<"\nthen the program will form into ASCII, that is "<<H,E,L,l,O;
    getch();
}

当我编译它时,它只打印出H字。

2 个答案:

答案 0 :(得分:0)

您需要使用插入运算符(&lt;&lt;)来为cout提供每个变量:

cout << "\n If all that variable spliced together ..." <<H<<E<<L<<l<<O;

答案 1 :(得分:0)

因为,没有按照您的想法行事。 a,b表示评估a,然后评估b,然后使用b的结果(例如(1+1, 2*3)为6)。

cout<<"\nIf all that variable spliced together, "<<"\nthen the program will form into ASCII, that is "<<H,E,L,l,O;

此语句评估cout<<"\nIf all that variable spliced together, "<<"\nthen the program will form into ASCII, that is "<<H。然后它评估E并且不对结果做任何事情,然后它评估L并且不对结果做任何事情,依此类推。

您已经知道如何打印多个内容 - 继续链接<<运算符。 cout<<...<<H<<E<<L<<l<<O;