我想在我的代碼中找到一種方法來搜索我使用過的向量中的一個對象。我已經將信息推回到不同的載體中,並且我知道.size來顯示信息。我希望用戶能夠輸入銀行賬號,如果這是正確的,則顯示其他向量的內容。在一個向量中搜索對象
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
//declaring four vectors
std::vector<string>names;
std::vector<string>address;
std::vector<int>age;
std::vector<double>accountnumber;
//forward declaration of functions
void namesInput();
void addressInput();
void ageInput();
void accountnumberInput();
int main()
{
//variable for switch statement
int choice;
system("title HSBC Online Banking");
system("color B6");
cout << "Please select from the following options" << endl;
cout << "----------------------------------------" << endl;
cout << "1. Enter profile " << endl;
cout << "2. Search for client " << endl;
cout << "3. Exit" << endl;
cin >> choice;
switch(choice)
{
case 1:
//calling functions for first switch case
namesInput();
addressInput();
ageInput();
accountnumberInput();
system("cls");
break;
case 2:
break;
case 3:
system("exit");
break;
}
return 0;
}
void namesInput()
{
system("cls");
for (int i = 1; i <= 3; i++)
{
string temp;//variable to give to vector
cout<<"Enter " << i << " first, middle and last names : ";
cin>>temp;
names.push_back(temp);//push back into vector
}
}
void addressInput()
{
system ("cls");
for (int i = 1; i <= 3; i++)
{
string temp;//variable to give to vector
cout<<"Enter " << i << " House Number, Street, Postcode : ";
cin>>temp;
address.push_back(temp);//push back into vector
}
}
void ageInput()
{
system ("cls");
for (int i = 1; i <= 3; i++)
{
int temp;//variable to give to vector
cout<<"Enter " << i << " Day, Month, Year : ";
cin>>temp;
age.push_back(temp);//push back into vector
}
}
void accountnumberInput()
{
system ("cls");
for (int i = 1; i <= 1; i++)
{
int temp;//variable to give to vector
cout<<"Enter " << i << " Account Number ";
cin>>temp;
accountnumber.push_back(temp);//push back into vector
}
main();
}
void findClient()
{
cout << "To find a client please enter their account number" << endl;
}
'std :: find'可能會有幫助。 – Jarod42
你應該使用銀行賬戶的矢量。如果沒有這種方式,將難以處理名稱,地址等各個向量之間的關係。 – harper
順便說一句,您可能對'accountnumber'具有'double'的準確性問題(如果按照確切值進行搜索,則更多)。 – Jarod42