So this is for my C++ class and the assignment has us reading a pre-made file to which we will grab data from, depending on the selection made by the user (referring to the status N,I,R,B) and create a mailing list out of the specified data the user requested.
I'm working on this step-by-step but have already put the prototypes and bodies of the other functions in my code so please ignore 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;
}
The text file:
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
The problem I'm dealing with is understanding how to search the file with Supplier search_file()
. My understanding is that I have read the file using void read_file()
. The data from that file is stored in Supplier supplier_list[50];
. I need to access this data in the Supplier search_file()
so that when the user makes their selection it will search the file for that selection and store those specific lines to be written in Supplier make_supplier_record()
.
All I am asking for help with is understanding the search function. I know I can write the make file function.