程序C++运行时错误

Run Time Error When Program is Running C++

本文关键字:运行时错误 C++ 程序      更新时间:2023-10-16

程序运行时我收到运行时错误,它需要用户名,但是当它涉及到密码时,它向我显示: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;
}

你绝对应该了解更多关于TypesSTL Streams的信息。但是假设你在这里进行实验是更有意义的代码版本:

#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;
}

请注意,我们在阅读int后使用cin.ignore(),如此处所述