将ascii值转换为char

use*_*477 6 c++ ascii

如何将5个随机ascii值转换为chars?


提示:

从97到122随机生成5个ascii值(所有字母表的ascii值).当你去的时候,确定与每个ascii值对应的字母,并输出由5个字母组成的单词.

我的代码:

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main ()
{
srand (time(NULL));
int val1= rand()%122+97;
int val2= rand()%122+97;
int val3= rand()%122+97;
int val4= rand()%122+97;
int val5= rand()%122+97

cout<<val1<<" and "<<val2<<" and "<<val3<<" and "<<val4<<" and "<<val15<<". "<<






return 0;
}
Run Code Online (Sandbox Code Playgroud)

Rif*_*tus 13

for (int i = 0; i < 5; i++){
    int asciiVal = rand()%26 + 97;
    char asciiChar = asciiVal;
    cout << asciiChar << " and ";
}
Run Code Online (Sandbox Code Playgroud)


A-S*_*ani 5

要将intASCII值转换为字符,您还可以使用:

int asciiValue = 65;
char character = char(asciiValue);
cout << character; // output: A
cout << char(90); // output: Z
Run Code Online (Sandbox Code Playgroud)