【问题标题】:C++, structure database program issuesC++,结构数据库程序问题
【发布时间】:2015-06-18 08:29:42
【问题描述】:

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

我的教授要求我创建一个程序,允许用户插入、编辑和打印出客户数据库。我的一个功能有问题,它似乎不能很好地比较某些东西。该函数是第 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

【问题讨论】:

  • 为了以后参考,在引用没有行号的代码时不要使用行号。我没有否决这个问题。
  • 对于那些想要输出的人,在使用 newclient 函数成功插入客户端后。我将输入一个名字,比如说 bob。输出:bob bob bob bob bob bob bob bob bob
  • @JonnyHenly 抱歉,我将编辑问题以包含行号
  • 我会避免所有行号,而是坚持使用代码 sn-ps 和与所需输出匹配的程序输出来解释问题。
  • @JonnyHenly 我找不到为标准控制台输出插入片段的选项。由于我的声誉低,上传图片也不是解决方案。但我确实拔出了我遇到问题的确切线路。感谢您以正确的方式提出有关 stackoverflow 的问题。

标签: c++ function comparison structure conditional


【解决方案1】:

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

【讨论】:

  • 非常感谢您详细解释 strcmp 函数的工作原理!这解决了我的问题,我很感激:)。
【解决方案2】:

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

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

它应该可以正常工作:)

【讨论】:

  • 非常感谢! :) 我希望我能支持你,但显然我也需要声誉。猜猜我最好开始回答问题
猜你喜欢
  • 2013-01-20
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 1970-01-01
  • 2018-08-30
  • 2011-03-19
相关资源
最近更新 更多