2015-12-02 57 views
1

處理此代碼,我有一個帳戶信息數組。我需要編寫一個函數,讓用戶輸入名稱的一部分,並在數組中搜索它。C++比較結構數組中的字符串

更具體地說:「搜索特定客戶帳戶的結構數組,它應接受客戶名稱的一部分作爲參數,然後搜索名稱與其匹配的帳戶,應顯示所有匹配的帳戶(包括所有客戶的信息),如果沒有帳戶匹配,就會顯示一條消息。「

我似乎無法正確比較功能,我想我只是不正確地接近這個問題,並會真正感謝一些指導。

下面的代碼,我工作的功能「searchAccount」

#include <iostream> 
#include <iomanip> 
#include <string> 
#include <cstring> 
#include <fstream> 

using namespace std; 

struct Customer 
{ 
    string name; 
    string address; 
    string city; 
    string state; 
    int zip; 
    string num; 
    string date; 
    double bal; 
}; 

void readData(Customer []); 
void displayData(Customer []); 
void changeInfo(Customer []); 
void searchAccount(Customer []); 


const int MAX = 10; 

int main() 
{ 
    int selection; 

    Customer data[MAX]; 

    do 
    { 
     cout << "Customer Accounts Menu" << endl; 
     cout << "------------------------------" << endl; 
     cout << "1. Read customer info into array. " << endl; 
     cout << "2. Change account information. " << endl; 
     cout << "3. Display information in array. " << endl; 
     cout << "4. Search for customer account. " << endl; 
     cout << "5. Sort customer balances in descending order. " << endl; 
     cout << "6. Sort customer names in ascending order. " << endl; 
     cout << "7. Compare names of two customers & replace smallest with address of largest" << endl; 
     cout << "8. Exit program" << endl; 
     cout << "------------------------------" << endl; 
     cout << "Enter a selection: " << endl; 
     cin >> selection; 

     switch (selection) 
     { 
      case (1): 
       readData(data); 
       break; 

      case (2): 
       changeInfo(data); 
       break; 

      case (3): 
       displayData(data); 
       break; 

      case (4): 
       searchAccount(data); 
       break; 

      case (5): 
       break; 

      case (6): 
       break; 

      case (7): 
       break; 

      default: cout << "Please enter a valid selection." << endl; 
     } 

    }while(selection != 8); 

    return 0; 
} 

void readData(Customer data[]) 
{ 
    int num = 0; 

    ifstream inFile; 

    inFile.open("customers.txt"); 

    if (!inFile) 
    { 
     cout << "Cannot open the file" << endl; 
    } 
    else 
    { 
     inFile >> data[num].name >> data[num].address >> data[num].city >> data[num].state >> data[num].zip >> data[num].num >> data[num].date >> data[num].bal; 

     while (inFile) 
     { 
      num++; 
      inFile >> data[num].name >> data[num].address >> data[num].city >> data[num].state >> data[num].zip >> data[num].num >> data[num].date >> data[num].bal; 
     } 
    } 
    inFile.close(); 
} 

void displayData(Customer data[]) 
{ 
    for (int num = 0; num < MAX; num++) 
    { 
     cout << data[num].name << " " << data[num].address << " " << data[num].city << " " << data[num].state << " " << data[num].zip << " " << data[num].num << " " << data[num].date << " " << data[num].bal << endl; 
    } 
} 

void changeInfo(Customer data[]) 
{ 
    string name; 

    cout << "Enter customer name to change" << endl; 
    cin >> name; 

    for (int i = 0; i < MAX; i++) 
    { 
     if (data[i].name == name) 
     { 
      cout << "Enter new information for " << name << endl; 
      cout << "Name: " << endl; 
      cin >> data[i].name; 
      cout << "Address: " << endl; 
      cin >> data[i].address; 
      cout << "City: " << endl; 
      cin >> data[i].city; 
      cout << "State: " << endl; 
      cin >> data[i].state; 
      cout << "Zip: " << endl; 
      cin >> data[i].zip; 
      cout << "Phone Number: " << endl; 
      cin >> data[i].num; 
      cout << "Date of Last Payment: " << endl; 
      cin >> data[i].date; 
      cout << "Account Balance: " << endl; 
      cin >> data[i].bal; 
     } 
    } 
} 

void searchAccount(Customer data[]) 
{ 
    string name; 

    cout << "Enter a name to search: " << endl; 
    cin >> name; 

    for (int i = 0; i < MAX; i++) 
    { 
     if (strcmp(name, "data[i].name") 
     //Not sure what to put here, the function above won't correctly as well 
    } 
} 
+0

很好提供示例,但[mcve](http://stackoverflow.com/help/mcve)用** M ** inimal代碼更好。 – Jarod42

回答

0

首先,包括包含標準庫中的算法頭:

#include <algorithm> 

然後,請採用以下您的searchAccount功能:

string name; 
std::string tempLowercase; 

cout << "Enter a name to search: " << endl; 
cin >> name; 

// Convert entered name to lower case 
string enteredLowercase = name; 
std::transform(enteredLowercase.begin(), enteredLowercase.end(), enteredLowercase.begin(), ::tolower); 

bool found = false; 
for(int i = 0; i < MAX; i++) 
{ 
    // Convert current name to lower case and compare with entered name 
    tempLowercase = data[i].name; 
    std::transform(tempLowercase.begin(), tempLowercase.end(), tempLowercase.begin(), ::tolower); 
    if (std::string::npos != tempLowercase.find(enteredLowercase)) 
    { 
     cout << data[i].name << " " << data[i].address << " " << data[i].city 
      << " " << data[i].state << " " << data[i].zip << " " << data[i].num 
      << " " << data[i].date << " " << data[i].bal << endl; 
     if(!found) 
      found = true; 
    } 
} 

if(!found) 
    cout << "No customers found" << endl; 
+0

此代碼有效,但並不完全符合說明。所有的名字都像Mike_Jones或Steve_Smith一樣列出。我應該能夠輸入麥克或史蒂夫,這些條目將彈出。我認爲這是我需要使用strcmp函數的地方,但我不太確定 – HOAX

+0

@HOAX你用'C++'標記了這個問題。因此,我使用了'std :: string :: find'函數。函數'strcmp'是C風格函數,不適合當前情況。就說明書而言,它明確指出:「它應該接受客戶名稱的一部分作爲參數,然後搜索名稱與其匹配的帳戶,並顯示所有匹配的帳戶」。這就是你發佈的問題,應該顯示所有包含輸入字符串的帳戶。我不明白這怎麼不能滿足你期望的行爲。 –

+0

因爲如果其中一個名稱是數組中的Mike_Jones,如果我只搜索「mike」,那麼您的代碼將顯示「找不到客戶」。 – HOAX

0

這聽起來像一個典型的數據庫ap折衷,所以我會考慮首先使用SQLite。標準C++不提供此功能。但是,如果你需要堅持使用普通的C++,這裏就是你要找我想什麼:

bool found_one = false; 
for(int i = 0; i < MAX; i++) 
{ 
    if(data[i].name.find(name) != std::string::npos) 
    { 
    found_one = true; 
    // output the record 
    } 
} 
if(!found_one) 
{ 
    // output error message 
} 

作爲一個側面說明:您的代碼看上去更象C比C++。我建議你考慮使用std容器,例如std :: vector來存儲你的數據。

+0

這不起作用,也爲其他評論添加了更多的說明,我也將它們放在這裏:「所有的名字都像Mike_Jones或Steve_Smith一樣列出,我應該能夠輸入麥克或史蒂夫和那些條目會彈出,我認爲這是我需要使用strcmp函數的地方,但我不太確定「 – HOAX