R
R
Raigon2020-06-02 20:15:38
C++ / C#
Raigon, 2020-06-02 20:15:38

Why doesn't it output the generated password to a c++ text file?

I understand that the code itself consists of crutches, but I need help with outputting an array with a password to a text file.

#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;


void Vasya(char z[20])
{
    ofstream out;
    out.open("D:\\hello.txt");
    
    if (out.is_open())
    {
        out << z << endl;
    }
}


int main()
{
    int x;
    int d;
    
    cout << "Введите кол-во паролей: ";
    cin >> x;
    
    cout << "Введите длину пароля: ";
    cin >> d;
    
    char pass[20] = {};
    
    for (int i = 0; i < x; i++) {
        
        for (int y = 0;  y < d; y++) {
            if ((rand() % 100 + 1) >= 50) {
                pass[y] = rand() % 10;
            } else {
                pass[y] = char(rand()%26+0x61);
            }
        }
        
        Vasya(pass);
        
        pass[20] = {0};
        
        //cout << "\n";
    }
    
    cout << pass;
    
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2020-06-02
@galaxy

Well, first of all, it brings out.
Secondly, pass[y] = rand() % 10;we insert non-printable characters. Probably wanted pass[y] = rand() % 10+0x30;?
Thirdly, it is necessary to terminate on time and before printing:

pass[d] = '\0';
Vasya(pass);

Next, write on pluses, use std::string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question