I want this program to print a password for the user generated randomly but it isn't just working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I want this program to print a password for the user generated randomly but it isn't just working

#include <iostream> #include <cstdlib> using namespace std; //function for generating a random number //In code playground, it generates 9 - 10 numbers each random number. void password_master (int x) { for (int y = 1; y <= x; y++) { cout << rand() << endl; } } int main() { int user; cout << "Do you want a pin?: \n"; cin >> user; //If the user wants a pin, it prints the random number as a password if (user == "Yes" || "yes") { cout << "Your pin is " << password_master(user) << endl; } else { cout << "Thank you"; } return 0; }

7th Jan 2022, 3:29 PM
𝑨𝒍𝒗𝒊𝒏
𝑨𝒍𝒗𝒊𝒏 - avatar
2 Answers
+ 1
I couldn't even compile it, so let's fix that! - error: comparison between pointer and integer ('int' and 'const char *') if (user == "Yes" || "yes") This is because user is declared as an int, and the code tries to compare it with a pointer. The type of "Yes" is `const char[4]`, now, in C++, when a c-style array (declared with []) is passed as an argument to a function, it decays to a pointer, meaning it get's converted to a one. The result is that `user == "Yes"` is invalid. The solution is simple, declare `user` as a `std::string` instead of an `int`. - error: no matching function for call to 'password_master' cout << "Your pin is " << password_master(user) << endl; The problem is that the function returns void, yet the code is trying to pass the result of that function (void) to cout for printing. How would that work? Right, it won't. The solution is to make the function store the password in a string and return it. The current implementation looks like so: void password_master (int x) { for (int y = 1; y <= x; y++) { cout << rand() << endl; } } The first step is to fix the return value, so instead of void it should be string. We end up with: string password_master (int x) { for (int y = 1; y <= x; y++) { cout << rand() << endl; } } Now we need a string to store the password in and return: string password_master (int x) { string retval; for (int y = 1; y <= x; y++) { cout << rand() << endl; } return retval; } And, instead of printing the random numbers, we store them in the return value: string password_master (int x) { string retval; for (int y = 1; y <= x; y++) { retval.push_back(static_cast<char>(rand())); } return retval; } I'm running out of space so i'll omit the last error :/ The error is at the password_master call in main. It's basically saying that the function takes an int yet we're passing in a string. For testing you can hardcode it, i passed 8 (g�isQ�J�), or do it correctly and ask the user
8th Jan 2022, 11:02 PM
Isho
Isho - avatar
+ 1
[This reply got pushed to the top, dammit. Please read the essay below first] I ran out of characters, so here is another fix in a separate post. If you run the program multiple times, you'll notice that it always generates the same character sequence, this is because the random number generator is not being seeded, to fix that simply call 'srand(time(nullptr))` right at the beginning of `main`.
8th Jan 2022, 11:05 PM
Isho
Isho - avatar