-3

I am trying to learn C++ by doing a simple Login program (3rd day). The program below just shows "enter username" and "enter password"), but does not provide the expected output.

#include <iostream>

int main(int argc, char const *argv[])
{
int username, password, User, Pass;

std::cout << "enter username: " << std::endl;
std::cout << "enter password: " << std::endl;

std::cin >> username;
username = User;
std::cin >> password;
password = Pass;

if (username != User && password != Pass)
    {std::cout << "error";}
if (username == User && password != Pass)
    {std::cout << "error";}
if (username != User && password == Pass)
    {std::cout << "error";}
if (username == User && password == Pass)
    {std::cout << "success";}

return 0;

}

Sam M
  • 4,136
  • 4
  • 29
  • 42
Fcas
  • 1
  • 2
    `cin >> username; username = User;`. You're overwriting your input immediately after you get it. – scohe001 Jan 05 '18 at 19:48
  • 2
    Furthermore, if you set `username = User;` and `password = Pass`, then all of your checks are kind of useless, aren't they? – scohe001 Jan 05 '18 at 19:48
  • 1
    The checks are pointless either way because you are checking against uninitialized variables.. And all your variables are of the wrong type. Shouldn't they be `std::string` instead of `int`. – drescherjm Jan 05 '18 at 19:51
  • 3
    Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list. – Ron Jan 05 '18 at 20:30

1 Answers1

0

You can try the below code , it will run perfectly I am modify your code & following are some mistakes in your code ! 1) You can use the wrong data type , the password & username are not integer data type .They should be in string formats. 2) You take the input in variable 'username' & 'password' then you code overwrite these variable with the uninitialized variable 'User' & 'Pass'. 3) Then your code compare the username & password variable values with the uninitialized values of 'User' & 'Pass' .

#include <iostream>
#include <string>
#include<conio.h>

using namespace std;

int main() {

    string username, password, User="usman", Pass="12345";

    //username variable is used to take input from the user
    //password variable is used to take input from the user 
    //User variable has the hardcode value of username 'usman' which is used to compare in if condition
    //Pass variable has the hardcode value of password '12345' which is used to compare in if condition


    //taking username from user through keyboard in 'username' variable
    std::cout << "enter username: " << std::endl;
    std::cin >> username;


    //taking password from user through keyboard in 'password' variable
    std::cout << "enter password: " << std::endl;
    std::cin >> password;


    //Now compare the 'username' & 'password' variable value with the hardcode values of 'User' & 'Pass'
    if (username != User && password != Pass)
    {
        std::cout << "error";
    }
    if (username == User && password != Pass)
    {
        std::cout << "error";
    }
    if (username != User && password == Pass)
    {
        std::cout << "error";
    }
    if (username == User && password == Pass)
    {
        std::cout << "success";
    }

    _getch();
    return 0;

}

Output of code with correct username & password

enter image description here

Usman
  • 1,983
  • 15
  • 28