Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include <iostream>
#include <ctime>
#include <string>
#include <cctype>
#include <cstdlib>
#include <vector>
using namespace std;

int main()
{
    cout << "Enter your name." << endl;
    string name;
    cin >> name;
    cout << "Welcome " << name << endl;
    char arr[10];
    cout << "Enter your username, Make sure it's 10 characters long." << endl;
    cin >> arr;
    string H = "JacksonFive";
    cout << endl;
    string password;
    cout << "Enter Random for random password, enter anything else to get to choose your password" << endl;
    cin >> password;

    if (password == "Random") {
        string data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            "abcdefghijklnmopqrstuvwxyz"
            "1234567890";
        string pass;
        for (int i = 0; i <= 8; i++) {
            pass = pass + data[rand() % data.length()];
        }
        cout << endl;
        cout << "Your Username: " << arr << endl << "Your Password: " << pass << endl;
    }
    else {
        cout << "Enter your Password, Make sure it's 8 characters long and make sure there is no tilde~";
        string P;
        cin >> P;
        
        cout << "Your Username: " << arr << endl << "Your Password: " << P << endl;
    }
    cout << "Please login to ensure your account is safe from termination." << endl;
    int logatt = 0;
    string usern;
    string userp;
    while (logatt < 5) {
        cout << "Enter your username." << endl;
        cin >> usern;
        if (usern == arr) {
            cout << "Enter your password." << endl;
            cin >> userp;
            if (userp == password) {
                cout << "Show home screen" << endl;
            }
            else {
                cout << "Try again" << endl;
                logatt++;
            }
        }
        else {
            cout << "Try again" << endl;
            logatt++;
        }
    }


 

    return 0;
}


What I have tried:

this runs fine except if you enter your password correctly it says try again
Posted
Updated 31-Jul-22 2:05am

Quote:
Run this in your compiler and see the problem bc it looks fine.

NO

The problem has been known for several posts and it doesn't look good at all. Above all, repeating it over and over doesn't make it any better.

We've discussed this several times here:
Whats-wrong-with-this-pls-help-its-supposed-to-be
and here:
Is-there-anything-I-can-add-to-this-login-it-works

Quote:
Is there anything I can add to this login it works fine as it is

I'm surprised you still have questions despite this opinion. Unless you're willing to use the suggestions, it won't work.

Why is the username now being read in multiple times? The first query should be completely deleted.

Why are there actually three different variables for a password? (password, pass, P)

Even at first glance the source code is obviously just awful and I won't put that through any compiler.
 
Share this answer
 
Comments
Renz 2022 31-Jul-22 19:32pm    
READ THE CODE password if for type of password
pass of for random
And what are you even talking about with the rest of the comment I use the compiler for testing new functions as well so that explains the random string but everything else I have no answer for.
Renz 2022 31-Jul-22 19:40pm    
Any no one ever said anything about debuggers in the previous questions and I have completely new code in this one and different questions
Ill delete the questions also you guys solved that and now there is a new problem
merano99 1-Aug-22 2:06am    
"And what are you even talking about with the rest of the commentary?"
Here seems to be the problem. We make suggestions that don't seem to be understood and may therefore not be used.
"Nobody ever said anything about debuggers in the previous questions."
Yes, that's right. I usually assume that it is known that there are debuggers. I don't feel good about pointing this out, but if it helped in your case, that's good.
I suggest writing functions for reading in the password, the random-password and the username.
This makes troubleshooting easier and you can only ask for this function. At the moment the code doesn't seem to be very good in so many places. When we get such a big morsel in front of us, the list goes on.
I had already suggested a function for your password problem as an example. If you would take over that and would also take over the know-how for other parts, we would get further. Unfortunately, you're stuck in places like the arr[] , which wouldn't be a good solution to your problem.
My suggestion was to read a complete line with getline(), why don't you just do that?
Don't get frustrated, but please also make sure that we don't get frustrated when the old problem comes up again as a new question.
Because there is no way out of the loop except to run out of logatt attempts - which doesn't get changed if the password matches.

Try a break command when you get a match to exit the loop immediately.

To be honest, if you'd even tried running this in the debugger you would have seen exactly what was going on and it would have been obvious to you what the problem was.
Get used to the debugger - it will save you a huge amount of time in the future ...
 
Share this answer
 
It runs fine but is wrong. They're not the same thing!

What would be wrong with string arr instead of char arr[10]? Just use string everywhere.

string H = "JacksonFive" serves no purpose.

You have password but then read the user-supplied password into P instead of into password, although it would work if the user entered the same string both times.

You also tell the user not to use a tilde in the password but don't verify that it hasn't occurred.

The passwords don't match because you read the user-supplied one into P but then test it against userp. And if you generate a password, you put it in pass. (Your code is so confusing that I've had to continually update this answer as I find more things!)

Start to learn how to use a debugger. Now. If you don't, you'll never be able to see why your code is doing something different than what you believe it should. It's the only way to find problems in non-trivial code.
 
Share this answer
 
v6
Comments
Renz 2022 31-Jul-22 19:33pm    
When I start debugging it just runs, no errors, 2 warnings about random strings in my code and that's it
Greg Utas 31-Jul-22 19:57pm    
A random string is an error. I don't think the code does what you want it to.
Renz 2022 31-Jul-22 19:36pm    
Each variable serves a different purpose and I'll have to see how to actually use a debugger
Greg Utas 31-Jul-22 20:00pm    
If you've learned to use a debugger, you've taken a very important step.

Yes, each variable serves a different purpose--too many of them! You need one variable for the user's password, whether the user enters it or you generate it, and a second variable when the user retypes the password. That's all. But you have FOUR of them: password, pass, P, and userp.
Renz 2022 31-Jul-22 20:32pm    
userp is for you to sign in but I can delete P
Quote:
this runs fine except if you enter your password correctly it says try again

Thus, it runs wrong !
Stop guessing and see exactly what your code does with the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900