samedi 14 septembre 2019

What is wrong in this random string printing code?

Im trying to print X (x = user input) number to lines of string array (Alpha) and if there is "A" in the line it should print something (string or random number) in that line, for example "This line has "A" " . I cant figure out whats wrong in my code, like this line in code is not working right: (Alpha[dist(gen)] == Alpha[0])

It is printing out the ("This line has "A" ) in the line which has other string Alphabets (S,P C etc)

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <random>
#include <climits>



using namespace std;



int main()
{

int num;
const int SIZE = 6;

cout << "How many iterations: ";
cin >> num;
string Aplha[SIZE] = { "A", "D", "P", "E" , "C", "S" };




random_device randNum;
mt19937 gen(randNum());
uniform_int_distribution <> dist(0, 5);
uniform_int_distribution <> distRange(INT_MIN, INT_MAX);

for (int i = 0; i < num; i++)
{
    cout << Alpha[dist(gen)];   
    if (Alpha[dist(gen)] == Alpha[0])
    {
        cout << " this line has A " << distRange(gen);
    }
    cout << endl;
}



return 0;
}

Result should be...

if a line has "A" then :

A this line has A 123231315
D
P
D
S
A this line has A 845421541
C




Aucun commentaire:

Enregistrer un commentaire