0

I am getting a runtime error when the program is running it takes the username but then when it comes for password it shows me: Debug Error Run Time Check Failure #3-T.

#include <iostream>
using namespace std;

int main()
{
    int choice;
    float username, password; //login
    int name, age, gender, dob, address, workinfo;

    cout << "Welcome To HDFC Bank" << endl;
    //Menu Option
    cout << "Choose an option: " << endl;
    cout << "===========================" << endl;
    cout << "1. Login" << endl;
    cout << "2. Register" << endl;
    cout << "===========================" << endl;
    cin >> choice;
    if (choice == 1) {
        cout << "Please Enter Your Username: " << endl;
        cin >> username;
        cout << "Please Enter your Password: " << endl;
        cin >> password;
        if (choice == 1 || password = 2) {
            cout << "Welcome To The Program!!!" << endl;
        }
        else {
            cout << "Wrong Details!!" << endl;
        }
    }
    else if (choice == 2) {
        cout << "Enter Your Full Name: " << endl;
        cin >> name;
        cout << "Enter Your Age" << endl;
        cin >> age;
        cout << "Enter Your Date of Birth(dd/mm/yyyy): " << endl;
        cin >> dob;
        cout << "Enter Your Gender(M/F)" << endl;
        cin >> gender;
        cout << "Enter Your Address: " << endl;
        cin >> address;
        cout << "Enter Your Work Details: " << endl;
        cin >> workinfo;
    }
    if (age < 21) {
        cout << "Sorry You cannot Register as you are below 21 years. Please try later." << endl;
    }
    else {
        cout << "You have succesfully registered. Please check your email." << endl;
    }
    return 0;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64

1 Answers1

0

You should definitely learn more about Types and STL Streams. But assuming you're experimenting here is little bit more meaningful version of your code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int choice = 0;
    std::string username, password; //login
    int age = 0;
    std::string name, gender, dob, address, workinfo;

    cout << "Welcome To HDFC Bank" << endl;
    //Menu Option
    cout << "Choose an option: " << endl;
    cout << "===========================" << endl;
    cout << "1. Login" << endl;
    cout << "2. Register" << endl;
    cout << "===========================" << endl;
    cin >> choice;
    cin.ignore();
    if (choice == 1) {
        cout << "Please Enter Your Username: " << endl;     
        getline(cin, username);
        cout << "Please Enter your Password: " << endl;
        getline(cin, password);
        if (password == "1" || password == "2") {
            cout << "Welcome To The Program!!!" << endl;
        }
        else {
            cout << "Wrong Details!!" << endl;
            return 0;
        }
    }
    else if (choice == 2) {
        cout << "Enter Your Full Name: " << endl;
        getline(cin, name);
        cout << "Enter Your Age: " << endl;
        cin >> age;
        cin.ignore();
        cout << "Enter Your Date of Birth(dd/mm/yyyy): " << endl;
        getline(cin, dob);
        cout << "Enter Your Gender(M/F)" << endl;
        getline(cin, gender);
        cout << "Enter Your Address: " << endl;
        getline(cin, address);
        cout << "Enter Your Work Details: " << endl;
        getline(cin, workinfo);
    }
    if (age < 21) {
        cout << "Sorry You cannot Register as you are below 21 years. Please try later." << endl;
    }
    else {
        cout << "You have successfully registered. Please check your email." << endl;
    }
    return 0;
}

Note that we use cin.ignore() after reading int as described here

Community
  • 1
  • 1
Dmitriy Zapevalov
  • 1,357
  • 8
  • 13