16

How do I convert 5 random ascii values into chars?


Prompt:

Randomly generate 5 ascii values from 97 to 122 (the ascii values for all of the alphabet). As you go, determine the letter that corresponds to each ascii value and output the word formed by the 5 letters.

My Code:

#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;
}
ivanleoncz
  • 7,457
  • 4
  • 52
  • 48
user2877477
  • 193
  • 1
  • 3
  • 8
  • Prefer the `` header. – chris Oct 21 '13 at 02:10
  • @chris What do you mean? – user2877477 Oct 21 '13 at 02:12
  • Perhaps [a link to ``](http://en.cppreference.com/w/cpp/numeric/random) will explain that. – WhozCraig Oct 21 '13 at 02:13
  • `cout << (char)val1 << etc..` actually, you should not be using int in the first place. – Alden Oct 21 '13 at 02:14
  • 1
    `rand` is rather sucky when there are better PRNGs like `std::mt19937` right around the corner, and `std::uniform_int_distribution` actually gives a *uniform* distribution, unlike the modulus bias here. Plus it's plain to see what the range is. http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful – chris Oct 21 '13 at 02:14
  • 4
    `rand()%122` produces a value in the range [0, 122). Adding 97 to such a value produces a value in the range [97, 219). Not what you wanted. – Igor Tandetnik Oct 21 '13 at 02:16
  • 5
    If you have to ask 6 questions in 3 hours, perhaps you should spend a little more time studying a [proper book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Benjamin Lindley Oct 21 '13 at 02:17
  • 1
    [Expanding on chris' comment, See It Live](http://ideone.com/pEhIDI) – WhozCraig Oct 21 '13 at 02:20
  • This answer [here](https://stackoverflow.com/a/10847317/3054219) already shows how to convert integers to char*. – Sonic78 Mar 29 '22 at 12:30

3 Answers3

18
for (int i = 0; i < 5; i++){
    int asciiVal = rand()%26 + 97;
    char asciiChar = asciiVal;
    cout << asciiChar << " and ";
}
Riftus
  • 384
  • 4
  • 14
17

To convert an int ASCII value to character you can also use:

int asciiValue = 65;
char character = char(asciiValue);
cout << character; // output: A
cout << char(90); // output: Z
A-Sharabiani
  • 15,608
  • 15
  • 99
  • 122
-1
int main() 
{

    int v1, v2, v3, v4, v5,v6,v7;
    cout << "Enter 7 vals ";
    cin >> v1 >> v2 >> v3 >> v4 >> v5 >> v6 >> v7;

    cout << "The phrase is " 
         << char(v1) 
         << char(v2) << " "
         << char(v3) << " "
         << char(v4) 
         << char(v5) 
         << char(v6)  
         << char(v7);

        system("pause>0");
}
PM 77-1
  • 12,254
  • 20
  • 64
  • 106
  • What benefit does this answer provide? E.g. why do you use char() and not std::to_chars or std::to_string, as suggested by [this answer](https://stackoverflow.com/a/10847317/3054219)? – Sonic78 Mar 29 '22 at 12:26
  • was just sharing the way i did it. it is also beginner friendly (which I am) so it is easy to interpret and doesn't involve all the advance functions or tactics. if you are so well educated on the subj be nice on the forum – Aoi Hyoudou Apr 09 '22 at 17:56