运营商在学校项目中遇到的问题

时间:2019-05-31 12:21:51

标签: c++

此程序用于管理库,但在运行中情况2存在问题 在添加新成员的情况2中是中断的,我想检查新成员与其他成员是否具有相同的代码。

我为我的Member类添加了运算符==来比较x.code和y.code

我认为问题在这里:

if(M[counter+1].code == M[i].code){
    cout << "/n1) This code has been used please try again." << endl;
    break;
}

这是我的代码:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

using namespace std;

class Date{
public:
    int year, month, day;
};

class Members{
public:
    char name[20], famili[20], adress[100], fader_name[20];
    int code, age;
    long long int phone, melli_code;
    Date birthday;
};

istream& operator>>(istream& x, Date &a){
    cout << "\n Enter Year : "; cin >> a.year;
    cout << "\n Enter Month: "; cin >> a.month;
    cout << "\n Enter Day  : "; cin >> a.day;
}

istream& operator>>(istream& x, Members &a){
    cout << "\n Enter Name         : "; cin >> a.name;
    cout << "\n Enter Family name  : "; cin >> a.famili;
    cout << "\n Enter Adress       : "; cin >> a.adress;
    cout << "\n Enter fdther name  : "; cin >> a.fader_name;
    cout << "\n Enter date of birth: "; cin >> a.birthday;
    cout << "\n Enter age          : "; cin >> a.age;
    cout << "\n Enter phone number : "; cin >> a.phone;
    cout << "\n Enter melli code   : "; cin >> a.melli_code;
}

bool operator==(Members x, Members y){
    if(y.code == x.code)
        return true;
    else
        return false;
}

int main(){
    Book B[1000];
    Members M[1000];

    while(true){
        cout << "\n1) Add new book";
        cout << "\n2) Add new members";
        cout << "\n3) Remove a book";
        cout << "\n4) Remove a member";
        cout << "\n5) Issue a book";
        cout << "\n6) Deposite a book";
        cout << "\n7) View member Account";
        cout << "\n8) List of book issued";
        cout << "\n8) List of book issued";
        cout << "\n9) Exit:";
        cout << "\n\n Enter your choice : ";
        cin >> ch;

        switch(ch){
        case 1:
            cin >> B[counter];
            counter++;
            break;

        case 2:
            cout << "\n Enter a four digit code: ;";
            cin >> M[counter].code;

            //check the other members_code
            for(i; i < counter; i++){
                if(M[counter+1].code == M[i].code){
                    cout << "/n1) This code has been used please try again . " << endl;
                    break;
                }

                cin >> M[counter];
                counter++;
                break;
            }

            break;
        }
    }

    return 0;
}

0 个答案:

没有答案