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

时间:2015-04-13 05:26:31

标签: c++ function comparison structure conditional

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

我的任务是由我的教授创建一个程序,允许用户插入,编辑和打印客户数据库。我的一个功能有问题,似乎没有好好比较。该函数是第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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

它应该正常工作:)