22

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
  • 9,070
  • 7
  • 57
  • 49
user2877477
  • 253
  • 1
  • 4
  • 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
  • 2
    `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

4 Answers4

23

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
  • 17,750
  • 17
  • 113
  • 128
19
for (int i = 0; i < 5; i++){
    int asciiVal = rand()%26 + 97;
    char asciiChar = asciiVal;
    cout << asciiChar << " and ";
}
Riftus
  • 394
  • 4
  • 14
1
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {

    int random;
    time_t current_time = time(NULL);

    // Providing a seed value
    srand((unsigned) time(NULL));

    // For Loop
    for (int i = 0; i < 5; i++)
    {
        int random = 97 + (rand() % 26);
        cout << "Random Letter: " << char(random) << endl;
    }
    return 1;
}

The current_time variable holds the number of seconds passed since January, 1970. This value is passed to the srand() function and then we get a fresh sequence of pseudo-random numbers.

The seed value is provided once in a program no matter how many random numbers are to be generated.

The for loop iterates 5 times per the request of the problem. The random function generates a number between 97 and 123 representing the 26 letters in the Alphabet in there applicable place in the ASCII table.

Then we have a simple output that outputs with the Char() function. This function takes an integer value and outputs the corresponding ASCII related character.

Neil Meyer
  • 473
  • 4
  • 15
0
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,933
  • 21
  • 68
  • 111
  • 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