我是C ++新手,参加我的第一门课程,作为其中一项任务,我们必须创建一个记录管理程序。
它必须做的一件事是能够对记录进行排序并按特定顺序显示它们。
我正在创建一个地址簿程序,到目前为止,我可以添加联系人,显示它们,将它们保存到文件中,然后打开文件。我现在正试图按字母顺序按名字排序联系人,但我遇到了问题。主要的一点是程序必须使用struct,我试图按结构中的字符串排序,这是我以前从未做过的。
我也在努力寻找和显示特定的记录以进行编辑或删除,尽管我已经取得了一些进展。
如果有人能帮助我指出正确的方向,我真的很感激。我已经和它斗争了好几天。
这是我目前的代码:
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
struct AddressBook {
string firstname;
string lastname;
string addr;
string phone;
};
// Function prototypes
char getMenuResponse(); // Function to allow user to navigate program.
void addContacts(vector<AddressBook>&); // Function to add contacts.
void dispContacts(const vector<AddressBook>&); // Function to display contacts.
void saveFile(const vector<AddressBook>&); // Function to save to file.
void openFile(vector<AddressBook>&); // Function to read from file.
int findTargetFirstName(vector<AddressBook>& list, string target); // Function to find a target index
void findContact(vector<AddressBook>&); // Function to find a contact.
void sortFirstNames(vector<AddressBook>& list);
int main() {
// To operate, to avoid losing previous contacts, Open File first
vector<AddressBook> list;
bool run = true;
cout << "Welcome To My Address Book Program For Assignment 2!\n" << endl;
do {
switch (getMenuResponse()) {
case 'A': addContacts(list); break;
case 'D': dispContacts(list); break;
case 'M': sortFirstNames(list); break;
case 'O': openFile(list); break;
case 'S': saveFile(list); break;
case 'F': findContact(list); break;
case 'Q': run = false; break;
default : cout << "That is NOT a valid choice" << endl;
}
} while (run);
cout << endl << "Program Terminated" << endl;
// system("PAUSE"); // Commented out so the program exits upon request
return EXIT_SUCCESS;
}
void sortFirstNames(vector<AddressBook>& list) {
string firstname;
int i, j, min;
bool sorted = false;
bool run = true;
if(list[i].firstname > 1) {
for (i = 0; i < (list[i].firstname-1); i++) {
min = i;
for (j = (i+1); j < list[i].firstname; j++) {
if(list[j] < list[min]) min = j;
}
if (i != min) swap(list[i], list[min]);
}
sorted = true;
}
}
int findTargetFirstName(vector<AddressBook>& list, string target) {
// linear search:
for (int i = 0; i < list.size(); i++)
if (list[i].firstname == target) return i; // return ndx of target
return -1; // not in the list
}
void findContact(vector<AddressBook>& list) {
if(list.size() > 0) {
string firstname;
cout << "Enter the first name to find: ";
getline(cin, firstname);
int ndx = findTargetFirstName(list, firstname);
if (ndx >= 0) {
cout << firstname << " is at index " << ndx << "." << endl;
} else cout << firstname << " is not found!" << endl;
} else cout << "That name is not found!" << endl;
}
void addContacts(vector<AddressBook>& list) {
AddressBook tmp; // Declare a temp contact that we will load before putting in the array.
char response;
string str; // Needed for cin.getline; we are going to use a char array.
bool run = true;
do {
system("cls");
cout << "Enter Contact Information" << endl;
cout << "Enter 'quit' at Name to exit." << endl << endl;
cout << "First Name: "; // Entering first name.
getline(cin, str);
tmp.firstname = str;
cout << endl;
if (str == "quit") break;
cout << "Last Name: "; // Entering last name.
getline(cin, str);
tmp.lastname = str;
cout << endl;
cout << "Address: "; // Entering Address
getline(cin, str);
tmp.addr = str;
cout << endl;
cout << "Phone Number: "; // Entering phone number.
getline(cin, str);
tmp.phone = str;
cout << endl;
// See if this record should be added to the array.
cout << "Add Contact to Address Book? (y/n) ";
cin >> response;
cin.ignore(256, '\n');
if (toupper(response) == 'Y') {
list.push_back(tmp);
cout << "Contact added!"<< endl;
}else {
cout << "Contact not added!" << endl;
}
} while (run);
system("cls");
}
void dispContacts(const vector<AddressBook>& list) {
system("cls");
// If there is nothing in the list, statement that there are no contacts.
// Otherwise, continue.
if(list.size() < 1) {
cout << "Nothing to display" << endl;
} else {
cout << "Contacts :" << endl << endl;
cout << fixed << setprecision(2);
cout << "Contact Name Address Phone No." << endl;
cout << "*******************************************" << endl;
cout << left;
for (int i = 0; i < list.size(); i++) {
cout << setw(15) << list[i].firstname << left
<< setw(15) << list[i].lastname << left
<< setw(30) << list[i].addr << left
<< setw(15) << list[i].phone << left << endl;
}
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << right << setw(3) << list.size();
cout << " Contacts"<< endl;
}
system("PAUSE");
system("cls");
}
// Save records to file.
void saveFile(const vector<AddressBook>& list) {
string fileName;
ofstream outfi;
cout<<"Enter file name (please use addressbook.txt for now): ";
// Have set file name to addressbook.txt for now.
// Might change code to allow selection of file to load.
getline(cin,fileName);
outfi.open(fileName.c_str());
// To make sure the file stream is open before doing IO.
if (!outfi.fail()) {
system("cls");
cout << "Saving Address Book to the disc ";
for(int i = 0; i < list.size(); i++) {
outfi << list[i].firstname << ';'
<< list[i].lastname << ';'
<< list[i].addr << ';'
<< list[i].phone;
// Start a new line after all but the last record.
// Simplifies reading the file as EOF is at end of last line.
if (i < list.size()-1) outfi << endl;
}
cout << endl << list.size() << " Address Book written to the disc." << endl;
outfi.close();
system("PAUSE");
system("cls");
} else { // Something went wrong with writing to the file.
cout << "ERROR: problem with file" << endl;
system("PAUSE");
system("cls");
}
}
// Open file and load array
void openFile(vector <AddressBook>& list) {
AddressBook tmp;
// If Open File is selected after contacts are added, they will be overwritten.
// Warning user that this will occur.
// If 'N' selected, Open File will not continue, and will return to GetMenuResponse.
if (list.size() > 0) {
char response;
cout << "Are you sure? If you have already added contacts, this will "
<< "overwrite what you have entered with a previously saved "
<< "addressbook.txt." << endl;
cout << "Do you wish to proceed? (Y/N) : ";
cin >> response;
cin.ignore(256, '\n');
if(toupper(response) == 'N') return;
}
ifstream infi("addressbook.txt"); // Have set file name to addressbook.txt for now.
// Might change code to allow selection of file to load.
string str;
// Make sure the file stream is open before doing IO.
if (!infi.fail()) {
system("cls");
cout << "Reading Address Book from the disc ";
list.clear(); // Overwrite any existing records
getline(infi, str, ';');
while(!infi.eof()) {
// Store the first name.
tmp.firstname = str;
// Store the last name.
getline(infi, str, ';');
tmp.lastname = str;
// Store the address.
getline(infi, str, ';');
tmp.addr = str;
// Store the phone number.
getline(infi, str);
tmp.phone = str;
// Put tmp into the vector.
list.push_back(tmp);
getline(infi, str, ';');
}
cout << endl << list.size() << " Contacts read from the disc." << endl;
system("PAUSE");
system("cls");
} else { // Something went wrong with opening the file.
cout << "ERROR: problem with file" << endl;
system("PAUSE");
system("cls");
}
}
// Get's user's input from keyboard.
char getMenuResponse() {
char response;
cout << endl << "Make your selection" << endl
<< "(A)dd contact, \n(D)isplay Contacts, \n(M)Sort By First Name, "
<< "\n(O)pen File, \n(S)ave File, "
<<"\n(F)ind Index Of Contact By First Name, \n(Q)uit" << endl
<< "> ";
cin >> response;
cin.ignore(256, '\n');
// Clean-up up to 256 chars including the delimiter specified (\n, the endl)
// OR stop when the \n is encountered after removing it.
return toupper(response);
}
到目前为止,我已尝试过两种不同的方法。 第一个给我错误,如: 69 25 [错误]不匹配'operator&gt;'在'(&amp; list) - &gt; std :: vector&lt; _Tp,_Alloc&gt; :: operator []&gt;(((std :: vector :: size_type)i))。AddressBook :: firstname&gt; 1'
void sortFirstNames(vector<AddressBook>& list) {
string firstname;
int i, j, min;
bool sorted = false;
bool run = true;
if(list[i].firstname > 1) {
for (i = 0; i < (list[i].firstname-1); i++) {
min = i;
for (j = (i+1); j < list[i].firstname; j++) {
if(list[j] < list[min]) min = j;
}
if (i != min) swap(list[i], list[min]);
}
sorted = true;
}
}
第二个(对于我使用#include的这个)给了我错误,如: 66 23 [错误]'class std :: vector'没有名为'firstname'的成员
void sortFirstNames(vector<AddressBook>& list) {
string firstname;
char i;
for (i = 0; i < list.firstname; i++) {
sort(list.begin(), list.end());
}
}
在帮助下,我正在尝试使用std :: sort函数,但是在代码编译时它实际上并没有显示任何内容。
struct AddrCmpFirstNames {
bool operator ()(const AddressBook& lhs, const AddressBook& rhs)
{
return lhs.firstname < rhs.firstname;
}
};
void sortFirstNames(std::vector<AddressBook>& vec) {
std::sort(vec.begin(), vec.end(), AddrCmpFirstNames());
cout << endl;
}
我在main中声明了向量,并调用了函数,但它只显示一个空行。 我知道我遗漏了一些非常基本的东西。