Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <iostream>
#include <ctime>
#include <string>
#include <cctype>

using namespace std;

//c
int main()
    {
    char Pone;
    char Ptwo;
    char Pthree;
    char Pfour;
    char Pfive;
    char Psix;
    char Pseven;
    char Peight;
    char One;
    char Two;
    char Three;
    char Four;
    char Five;
    char Six;
    char seven;
    char Eight;
    char Nine;
    char Ten;
    cout << "Enter your username, Make sure it's 10 characters long.";
    cin >> One >> Two >> Three >> Four >> Five >> Six >> seven >> Eight >> Nine >> Ten;
    bool c = isdigit(One);
    bool ch = isdigit(Two);
    bool che = isdigit(Three);
    bool chec = isdigit(Four);
    bool check = isdigit(Five);
    bool checki = isdigit(Six);
    bool checkin = isdigit(seven);
    bool checking = isdigit(Eight);
    bool checkingc = isdigit(Nine);
    bool checkingch = isdigit(Ten);
    cout << endl;
    for (int i = 0; i < 100000; i++) {
        if (c && ch && che && chec && check && checki && checkin && checking && checkingc && checkingch == 1) {
            cout << "Username must contain a letter, pleae reenter your username ";
            cin >> One >> Two >> Three >> Four >> Five >> Six >> seven >> Eight >> Nine >> Ten;
        }
    }
    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 << "Your random password is " << pass << "\n";
    }
    else {
        cout << "Enter your Password, Make sure it's 8 characters long and make sure there is no tilde~";
        cin >> Pone >> Ptwo >> Pthree >> Pfour >> Pfive >> Psix >> Pseven >> Peight;
      
    }
    cout << "Your Username is " << One << Two << Three << Four << Five << Six << seven << Eight << Nine << Ten << endl << "Your Password is " << Pone << Ptwo << Pthree << Pfour << Pfive << Psix << Pseven << Peight << endl;
return 0;
}


What I have tried:

I've tried everything anything helps
when you put 1 letter and the rest digits it prints Please reenter your password
Posted
Updated 31-Jul-22 0:31am
v2
Comments
Renz 2022 30-Jul-22 2:53am    
So whatever you guys did to change what I put in the question solved it
Richard MacCutchan 30-Jul-22 5:08am    
See the comments and suggestions in your duplicate of this question. And in future, please do not repost.

In this program, without reading it any further, a nonsensical number of variables are declared.
It would be much better to use arrays or vectors here.
char Pone;
char Ptwo;
char Pthree;
char Pfour;
char Pfive;
char Psix;
char Pseven;
char Peight;
char One;
char Two;
char Three;
char Four;
char Five;
char Six;
char seven;
char Eight;
char Nine;
char Ten;

instead of Pone, Ptwo, Pthree, Pfour, Pfive, Psix, Pseven and Peight you could better write
char P[8];
or
vector<char>P(8, 0);

The Vector has several advantages; you can easily expand, delete and initialize elements.

In fact, all these variables are not even needed.
// EDIT:
Since both the name and the password are text, string would be the correct data type here. Rick had already explained to you that you can check or change each individual character of a string after it has been read. The current problem of the spaces disappearing would be gone if you read an entire line with getline(). Example: see my ChkPasswd() function below.

The password entry could be written much easier and better.
C++
cin >> password;

for (auto &c:password) c = toupper(c);

if (password == "RANDOM") {
	GenerateRandomPW(password);
}
else do {
	cout << "Enter your Password, Make sure it's 8 characters long it has no tilde~\n";
	// cin >> Pone >> Ptwo >> Pthree >> Pfour >> Pfive >> Psix >> Pseven >> Peight;
} while (!ChkPasswd(password));

cout << "Your password is " << password << "\n";


The subprogram for checking the password could look like this:
C++
bool ChkPasswd(string &password)
{
   password.clear();
   while (password == "")
      getline(cin, password);

  // TODO: Check if the password meets the requirements
  return true;
}


C++
cout << "Enter your username, Make sure it's 10 characters long.";
cin >> One >> Two >> Three >> Four >> Five >> Six >> seven >> Eight >> Nine >> Ten;
bool c = isdigit(One);
bool ch = isdigit(Two);
bool che = isdigit(Three);
bool chec = isdigit(Four);
bool check = isdigit(Five);
bool checki = isdigit(Six);
bool checkin = isdigit(seven);
bool checking = isdigit(Eight);
bool checkingc = isdigit(Nine);
bool checkingch = isdigit(Ten);
cout << endl;
for (int i = 0; i < 100000; i++) {
	if (c && ch && che && chec && check && checki && checkin && checking && checkingc && checkingch == 1) {
		cout << "Username must contain a letter, pleae reenter your username ";
		cin >> One >> Two >> Three >> Four >> Five >> Six >> seven >> Eight >> Nine >> Ten;
	}
}

Entering a name is absolutely wrong this way. Not only are countless unnecessary variables consumed, but a whole host of other impractical restrictions are made.

1. Usernames should be 10 characters long and contain no spaces.
2. Checking whether letters are contained is nonsensical with the AND link
Only if all 10 variables contain digits would a new entry be made, but this is not checked.
3. There is no check for special characters or arithmetic symbols.
4. The for loop doesn't make sense because the index is never used and the loop repeats 1000000 times with the goes through the same immutable result. If the if() query is really going to be true, you have to Enter 100000 x 10 letters without changing the conditions.

The same as above applies to the many similar variables. They are all not needed here.

Solution for this:
Use strings for input and loop through the validation until the input is valid.

There are also other places that cause warnings that are better avoided.

The isdigit() function has an int return value. The assignment to a bool variable is ugly.
bool c = isdigit(One);

warning C4800: "int": Variable is set to Boolean value ("True" or "False") (impact on performance possible)

There is a problem with the string:
"@#',.$&^-_+={}[}()*?!%\/`<>";


warning C4129: "/": unrecognized sequence of escape sequence

The character '\' should be encoded differently here.
 
Share this answer
 
v6
Comments
Renz 2022 30-Jul-22 2:31am    
@merano89 i separated them all in to all those variables to store the boolean value of it being true or not meaning 1
i also needed to make sure the first character was a letter and that it was uppercase
merano99 30-Jul-22 8:02am    
You can check each letter in a string individually. Consider using find_first_not_of() if you have a whitelist of letters you allow. You can convert characters with toupper() instead of nagging them.
Renz 2022 30-Jul-22 2:46am    
my compiler doesn't recognize 'oder'
merano99 30-Jul-22 3:44am    
Nothing for the compiler, it's just text that was missed when translating, it means 'or'. I replace it.

If you process the text as a char, then what is "the first"? It would be better to read in a string and then check the individual characters in it.

I suggest you check if our suggestions improve your solution. If there are new questions, it is best to open a new question.

Something else. Thanks for marking my text as solution. It would be nice if you also fill out the rating with stars for our posts. A solution without stars looks strange.
Stefan_Lang 31-Jul-22 6:32am    
My five. Good code suggestions and good catch on that '\/'!
I suggest using std::string to read a single string and then verifying if the username is in the correct format. (That is precisely what you are doing with the password, so why not with the username too?)

Creating multiple variables will only make the entire program verbose and scary to debug (and maintain, if that's the plan).

But if you insist, this is a known behavior in C++ where std::cin reads once and then doesn't read the next ones.

string - C++ "cin" only reads the first word - Stack Overflow[^]
 
Share this answer
 
Besides being about the worst possible way to accomplish this task, you have an infinite loop :
C++
for (int i = 0; i < 100000; i++) {
    if (c && ch && che && chec && check && checki && checkin && checking && checkingc && checkingch == 1) {
        cout << "Username must contain a letter, pleae reenter your username ";
        cin >> One >> Two >> Three >> Four >> Five >> Six >> seven >> Eight >> Nine >> Ten;
    }
}
If the if is statement is true the prompt message is displayed and new data is entered. However, the for loop is never escaped from because there is no break statement. Regardless of what data is input, the data evaluated in the if statement (the check***s) do not change their values so the results of the if statement will always be the same.

The reason that is the worst way possible to do this is because it is absurd to enter one character at a time. Your code should accept a string which is essentially an array of characters. Then you can utilize loops to examine every character in the string and apply the same logic to each character. That is a much better approach to take than to accept ten characters and apply logic ten times with no loops.
 
Share this answer
 
Comments
merano99 29-Jul-22 8:01am    
It's not an infinite loop, since already after 100000 runs the job is done ;-)
I also find it strange that names always have to be 10 characters long and spaces are completely ignored.
Rick York 29-Jul-22 13:55pm    
Good point - 100K loops is not infinite but it's close enough. Especially for a user who might be trapped in that loop.
Renz 2022 30-Jul-22 2:33am    
i dont know how to implement spaces as actual characters.
merano99 30-Jul-22 6:34am    
With the data type char and cin as input there is no way because the space as a separator is not read.
While the other solutions already pointed out many things that are actually wrong, hwere's a list of what's bad:

1. 'using namespace std;'
While not wrong, I strongly advise new programmers against doing this until they have a good grasp of the STL. The reason is that this line does literally nothing to improve their code, but it makes it harder to read the code, because you may not recognize whether a function you are calling hails from the STL, or somewhere else. Adding the prefix, 'std::' to any symbol hailinlg from the STL is a low price to pay for good readability of your code!

2. Choose meaningful names for your variables.
E. g. in 'Pone', 'Ptwo', etc., neither the 'P', nor the rest of the name carry any information on what these variables are used for. The only information anyone can obtain from such names is your inability or unwillingness to use container types (see below).

3. Learn to use containers!
When you have at least two variables used for basically the same thing and code referring to them in the same manner, then you should use a single variable of an appropriate container type instead. The basic C array often suffices, but if you want to learn C++, you should consider using std::vector instead. Or if it's just about strings, then instead use std::string. There are more and very interesting container types available in the STL, but std::string and std::vector cover almost all relevant use cases; It's ok to forget about the other types for now.

4. Learn to use loops!
This is an automatic with item 3 above: when you have data stored in containers, there's always something you want to do with its elements. So learn about using for, ranged for, and maybe even about using container iterators.

5. Avoid repetitive statements.
Your expressions combining multiple variables and conditions with '&&' or other operators should all be replaced with either already existing functions from the STL, or with loops. For one, it's bad style and hard to read. Secondly it's easy to mess up and potentially hard to fix. Most importantly, however, is that by doing this you hard-code the number of elements you are dealing with into your expressions, making them extremely hard to adjust, when you want to adapt your program to a different length or number of elements!

6. Do not make any assumptions on input!
It's ok to print info warning about what kind of input your program expects, but you can not rely on the user actually adhering to that advice! At the very least, you have to consider the case where the user accidentally makes a typo, or doesn't realize whether Caps lock or Num lock are active. If your program cannot deal with such cases gracefully, it's not a good program.

7. Use more functions!
Your program isn't very long, but it is supposed to do a number of different things, and at each stage there are a number of things to watch out for. For the sake of readability, and also to better be able to find and fix bugs, you should use separate functions for most of these stages: initialization, ask for input, check validity of the input, print advice on why the input is invalid (or possibly print feedback that it's valid). See solution 3 for an example on how you can split up your program into some functions with more limited scope.

I'm sure that just by following these tips you can avoid many of the problems that cause your program to fail.
 
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