C++,结构数据库程序问题

C++, structure database program issues

如果这是错误的地方,我深表歉意。我真的没有看到 post 特定代码问题的区域。

我的教授要求我创建一个程序,允许用户插入、编辑和打印客户数据库。我的一个功能有问题,它似乎没有很好地比较某些东西。该函数是第 85 行的 EditClient 函数,我遇到的逻辑问题是第 93 行。我在代码中包含了一些测试,使我确定它是第 93 行,例如在 else 语句中第 99 行我让它打印出比较中使用的相同参数。哪个有效!我不知道为什么它不理解比较。

#include <iostream>
#include <iomanip>

using namespace std;


struct ClientInfo{ // the structure template for the client info
    char name[40]; //client name
    char state[40]; //client state
    char city[40];
    int PhoneNumber;
    long double AccountBalance; //accounts balance
    char PaymentDate; //payment date

};

ClientInfo ClientList[10]; //intializes an array with the data type clientinfo


void MainMenu();

void NewClient(ClientInfo List[]); // prototypes
void ViewClient(ClientInfo List[]);
void EditClient(ClientInfo List[]);
int main(){

    MainMenu();
    system("pause");
}


void MainMenu(){ //this function is the main menu function
    char choice = 4;
    bool loop = true;
    while (loop){
        cout << "Welcome to the client database, enter 1 to view the clients, 2 to edit a client , and 3 to enter an entire new client. 0 to quit" << endl; //main menu prompt
        cin >> choice;
        if (choice == '1'){
            ViewClient(ClientList);
        }
        else if (choice == '2'){
            EditClient(ClientList);
        }
        else if (choice == '3'){
            NewClient(ClientList);
        }
        else if (choice == '0'){
            cout << "thank you for using the client database, closing out now" << endl;
            loop = false;
        }
        else{
            cout << "invalid number" << endl;

        }
    }
}


void NewClient(ClientInfo List[]){//function that goes through cins to insert client data

        int desiredTimes = 0; // the number of clients the person wish to enter
        cout << "how many clients are you entering ?, your list current has "<<endl;
        cin >> desiredTimes;
        cout << "entering new client function..." << endl;
            for (int cnt = 0; cnt < desiredTimes; cnt++){ // runs the program exactly the amount of times the person wished.
                cout << "please enter client name" << endl;
                cin.ignore();
                cin.getline(List[cnt].name, 40);
                cout << "please enter client state" << endl; // the getline is used here because there may be spacings within these first 3 catagories 
                cin.getline(List[cnt].state, 40);
                cout << "please enter client city" << endl;
                cin.getline(List[cnt].city, 40);
                cout << "please enter client Phone Number" << endl;
                cin.ignore(); // this is used to prevent the getline from causing issues with the cin
                cin >> List[cnt].PhoneNumber;
                cout << "please enter client Account Balance" << endl;
                cin >> List[cnt].AccountBalance;
                cout << "please enter client Payment Date" << endl;
                cin >> List[cnt].PaymentDate;
            }

}

void EditClient(ClientInfo List[]){ // function to search for a name requested and display the info
    char name[40];
    cout << "what is the name of the client you wish to view (be specific)?";
    cin >> name;
    bool loop = true; // boolean for the loop
    int cnt = 0; // position in the array
    while (loop){

        if (cnt < 11){
            if (name == List[cnt].name){ //if true, prints out the client's info
                cout << "true";
                /*NewClient(List[cnt]);*/
                loop = false; // ends the loop
            }

            else{
                cout << name << " " << List[cnt].name << endl;
                cnt++;
            }
        }
        else{
            cout << "your client isn't in the database M8" << endl;
            loop = false;
        }
    }


}







void ViewClient(ClientInfo List[]){//this function goes through the client list and displays a particular client
    cout << "the following is a huge overrun of all the data inside this list, prepare your self." << endl;
    for (int cnt = 0; cnt <= 10; cnt++){//goes through until the counter is greater than the size of the list in the parameter 
        cout << endl;
        cout << List[cnt].name;
        cout << List[cnt].state;
        cout << List[cnt].city;
        cout << List[cnt].PhoneNumber;
        cout << List[cnt].AccountBalance;
        cout << List[cnt].PaymentDate;

    }
}

这是我遇到比较错误的特定行。

if (name == List[cnt].name){ //if true, prints out the client's info

if (name == List[cnt].name) 是指针比较,不是字符串比较。仅当 nameList[cnt].name 指向相同的内存位置时才会如此,但它们永远不会。您可能应该使用 std::string 而不是 char[],但是如果您出于某种原因需要使用 char[],则需要使用 strcmp() 函数来比较它们。

尝试使用此语句,而不是您正在使用的 if 语句,

 if (strcmp(name,List[cnt].name)==0){

它应该可以正常工作:)