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

C++, structure database program issues

本文关键字:程序 问题 数据库 结构 C++      更新时间:2023-10-16

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

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

#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){

它应该正常工作:)