Hey, im making a code to ask a user to type in a username and password to continue and for the console to say "thanks" and i need help. Heres my "Unsuccessful" Code:

#include <iostream>

using namespace std;

int main(){
    int a, b;
    a = 159876;
    b = 43812385;
    int c;
    int d;
    
    cout << "Please Enter your Username:";
    cin >> c;
    
    cout << "Please Enter a Password:";
    cin >> d;
    
    if (c != b && d != a){
          cout << "Incorrect Username/Password Combintation \n";
          }
    if (c == b && d = a){
          cout << "What would you like to do";
          
          }
          
          system("Pause");
}

Dani AI

Generated

A few points that build on the replies already here: 's advice about clearer names and verifying input at runtime is useful for debugging; correctly called out a mistaken assignment inside a conditional; and pointed to the underlying logic issue. The immediate practical problems in the original code are (1) using integer variables for username/password input (text input will fail or be truncated), (2) accidental use of assignment where a comparison was intended, and (3) a condition that doesn't cleanly represent "accept when both match, reject otherwise." Fixing those and using a simple attempt loop makes the program more robust.

Example pattern to use (reads whole lines, compares strings, limits attempts):

#include <iostream>
#include <string>

int main() {
    const std::string correctUser = "admin";
    const std::string correctPass = "s3cret";

    std::string userIn, passIn;
    int tries = 0, maxTries = 3;

    while (tries < maxTries) {
        std::cout << "Username: ";
        std::getline(std::cin, userIn);

        std::cout << "Password: ";
        std::getline(std::cin, passIn);

        if (userIn == correctUser && passIn == correctPass) {
            std::cout << "Thanks\n";
            return 0;
        }

        std::cout << "Incorrect username/password\n";
        ++tries;
    }

    std::cout << "Too many failed attempts\n";
    return 0;
}

Troubleshooting tips: compile with warnings enabled (for example g++ -Wall -Wextra) to catch accidental assignments in conditionals; if you attempt to use numeric extraction and the input contains letters the stream will fail and later reads will be skipped; printing the stored and entered values while debugging helps diagnose mismatches (but avoid printing real passwords in production). Avoid nonportable calls like system("Pause").

Security notes: do not hard-code real credentials in applications—store and verify hashes, limit attempts, and use platform-specific APIs to avoid echoing passwords if you need concealment.

Recommended Answers

All 5 Replies

#1) Use better variables: PswdIn, Pswd, Name, NameIn would be much better.

#2) Display the values before the IF statements to make sure they are the same.

What do u mean write the values before the if statement?
And thanks for helping

You have a logic error in your code. Remember your logic tables ab = a'+b' meaning. Also you switched the terms

if (c != b && d != a){
          cout << "Incorrect Username/Password Combintation \n";
          }

should be

if (c != a || d != b){
          cout << "Incorrect Username/Password Combintation \n";
          }

Think about it like this. If you asked for both of them not to be equal to the proper values, then if the username is correct but the password is incorrect the program would continue, but if you say that when one is true (c!=a or d!=b) then the whole statement is true it will work.

Also you forgot an = sign:

if (c == b && d = a){
          cout << "What would you like to do";

should be

if (c == b && d == a){
          cout << "What would you like to do";

You test 2 conditions (lines 18 and 21). One of them is supposed to filter out incorrect attempts. Another is supposed to accept correct ones. Logically, they should be mutual negations. The way they are written they are not. Hint.
Besides, why do you want to test both?

You test 2 conditions (lines 18 and 21). One of them is supposed to filter out incorrect attempts. Another is supposed to accept correct ones. Logically, they should be mutual negations. The way they are written they are not. Hint.
Besides, why do you want to test both?

Well i want to test both so that its easier to understand, im a html coder and thats the way ive been taught in HTML, so i just incorporated it into C++,

And thanks everyone for helping me. Im only new to C++ and your replies have helped heaps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.