程序运行C ++时运行时错误

时间:2016-04-28 15:23:35

标签: c++ debugging

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

1 个答案:

答案 0 :(得分: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;
}

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