所以这是我的C ++类,并且赋值让我们读取我们将从中获取数据的预制文件,具体取决于用户所做的选择(参考状态N,I,R,B)并根据用户请求的指定数据创建邮件列表。
我正在逐步完成这项工作,但已经将其他功能的原型和主体放在我的代码中,所以请忽略Supplier make_supplier_record()
& void rtrim(string &s)
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
/**** Structure ****/
struct Supplier
{
string last_name;
string first_name;
string street_addr;
string city;
string state;
string zipcode;
char status;
};
Supplier s1, s2;
/*** Function Prototypes ***/
void read_file();
Supplier search_file();
Supplier make_supplier_record();
void rtrim(string &s);
/***************************/
// Read Function
void read_file()
{
Supplier supplier_list[50];
int num_records = 0;
fstream file("supplier.data.txt", ios::in);
if (file.fail())
{
cout << "File failed to open." << endl;
}
while (1)
{
if (file.eof() != 0)
{
break;
}
string line;
getline(file,line);
if (line.length() > 0)
{
supplier_list[num_records] = make_supplier_record();
num_records++;
}
}
file.close();
};
// Search Function (STATUS)
Supplier search_file()
{
Supplier supplier_list[50];
int num_records = 0;
char line_status;
Supplier s1;
return s1;
};
// Create Mailing List Function
Supplier make_supplier_record()
{
Supplier s2;
return s2;
};
// Eliminate White Space Function
void rtrim(string &s)
{
s.erase(s.find_last_not_of(" \n\r\t") + 1);
}
int main()
{
char prompt;
// Title
cout << "Welcome to the WSU Mailing List Generator!" << endl;
cout << endl;
// Prompting User to initiate calculator
cout << "Supplier Status:" << endl;
cout << setw(20) << right << "N - new supplier" << endl;
cout << setw(27) << right << "I - infrequent supplier" << endl;
cout << setw(24) << right << "R - regular supplier" << endl;
cout << setw(21) << right << "B - bulk supplier" << endl;
cout << "Please select a Supplier Status for the mailing list you wish to generate: " << "";
cin >> prompt;
cout << endl;
switch (prompt)
{
case 'n':
case 'N': cout << "Your selection was: New Supplier." << endl; break;
case 'i':
case 'I': cout << "Your selection was: Infrequent Supplier." << endl; break;
case 'r':
case 'R': cout << "Your selection was: Regular Supplier." << endl; break;
case 'b':
case 'B': cout << "Your selection was: Bulk Supplier." << endl; break;
default: cout << "I'm sorry that was an invalid selection.\n" "Please try again." << endl; break;
}
return 0;
}
文本文件:
Johnson Owen 1643 N. Business Blvd Wichita KS67122B
Spock Mr. 12333 E. Star Trek Blvd San Francisco CA99543R
Wilson Dylan 12345 N. Malibu Beach Blvd Irvine CA97894I
Stewart Connor 13456 E. Wilshire Blvd Santa Monica CA97845I
Knight Evan 14567 W. All Hollows Wichita KS67122B
Smithson David 1256 Center Court Wichita KS67236I
Sanders Nathan 1778 N. Deer Run Detroit MI45634B
Gray Tyler 12331 E. Star Trek Blvd San Francisco CA99543R
Stephenson Thomas 13345 E. Diamond Road Wichita KS67223I
Graves Joshua 12345 E. MoneyManager Topeka KS67012B
O'Niell Shaquille 1233 SkyScrapper, Penthouse 2 Los Angeles CA97865N
Vedder Eddie 4356 W. Avalanche Seattle WA92546R
Letterman David 12345 Trump Tower, Suite 34 New York NY12345I
Carson Johnny 76889 E. Casino Road Las Vegas NE87796N
MacGwire Mark 76567 S.E. Cardinal Lane St. Loius MO62231I
McCoy Leonard 12337 E. Star Trek Blvd San Francisco CA99543R
Jordan Michael 97896 E. Windy City Chicago IL45678R
Johnsonbaugh Richard 9856 N. Riverbank Upper Saddle River NJ12345R
Kalin Martin 1345 E. Riverbend Upper Saddle River NJ12346I
Roberts Grace 1728 Pennsylvania Drive Washington, D.C. MA14569R
Harris James 12345 N. Viagra Falls Raleigh NC34532I
Wright Sophie 4456 W. Fish Creek Road Washington, D.C. MA14569R
Clark Nolan 45545 S.E. Saratoga Palm Springs FL34343N
Morrison Jim 12345 Santa Anna Freeway Glendale CA96543I
Green Clara 3456 N. Star Drive Austin TX59987B
Ortiz Natalie 1234 S. Star Circle Hollywood CA99768R
Reyes Sean 45789 S. Tunesmith Tuscon AZ87789I
Warren Jason 34567 S. Tower of Power Los Angeles CA99786I
Stone Miles 98456 N.E. Rappers Lane San Francisco CA98876R
Picard Jean-Luc 12349 E. Star Trek Blvd San Francisco CA99543N
Jackson Janet 4567 N. Songbird Los Angeles CA99782I
Flores Albert 45345 N. Ambitions Lane Clarksville TN23345I
Williams Andy 45679 E. Star Drive Branson MO54467I
Kirk James T. 12333 E. Star Trek blvd San Fransico CS99543I
Riker William 12345 E. Star Trek Blvd San Fransisco CA99543R
McCoy Leonard 12337 E. Star Trek Blvd San Francisco CA99543R
Kirk James T. 12333 E. Star Trek blvd San Fransisco CS99543I
我正在处理的问题是了解如何使用Supplier search_file()
搜索文件。我的理解是我使用void read_file()
读取了该文件。该文件中的数据存储在Supplier supplier_list[50];
中。我需要在Supplier search_file()
中访问此数据,以便当用户进行选择时,它将在文件中搜索该选择并将这些特定行存储在Supplier make_supplier_record()
中。
我要求帮助的是理解搜索功能。我知道我可以编写make文件函数。