mardi 8 novembre 2022

How to randomize the name entered by the user

#include <iostream>
#include <algorithm>
#include <conio.h>
#include <vector>
#include <time.h>

using namespace std;

int main()
{
    int jumlah_orang;
    char user3;
    vector<string> names;
    string nama;
    srand(time(NULL));
    do
    {
        system("cls");
        cout << "Jumlah Orang: "; cin >> jumlah_orang;
        cin.ignore();
        for(int i = 0; i < jumlah_orang; i++)
        {
            cout << "Nama ke-" << i+1 << ": ";
            getline(cin, nama);
            names.push_back(nama);
        }
        cout << "\nNama:\n";
        for(int i = 0; i < names.size(); i++)
        {
            cout << names[i] << endl;
        }
        int random_name = rand() % names.size();
        cout << nama[random_name] << endl;
        cout << "\nUlang Program? (Y/T): "; 
        cin >> user3;
    }
    while(user3 == 'Y' || user3 == 'y');
    getch();
}

The name that comes out is always in the form of one character. For example, enter the name "Russel". The output can be "u", "s", "l", etc. This is not what I want.

I want users to enter the number of users and enter their respective names. When random is done, the output data is the name of one of the users entered.

Aucun commentaire:

Enregistrer un commentaire