0

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.

  • OT as I've not yet read through all the code, but - `while (1) { if (file.eof() != 0) { break; }` - what? This is very redundant when you could just do `while (!file.eof()) {` - i.e. properly use the `while` keyword. `break` has its uses; this is not one. – underscore_d Jul 20 '15 at 19:57
  • @underscore_d [No don't suggest that](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). The OP should use the read operation to control the loop so the first read failure will stop the loop. – NathanOliver Jul 20 '15 at 19:59
  • 1
    You are going to get a segmentation fault when `num_records` is greater than 49 in the while loop. – vincentleest Jul 20 '15 at 20:00
  • How much of the standard library are you allowed to use for this assignment? `std::vector supplier_list` in place of the array will solve the problem @vincentleest pointed out, but [std::map](http://en.cppreference.com/w/cpp/container/map) may make short work of your searching requirement. – user4581301 Jul 20 '15 at 20:11
  • @user4581301 unfortunately we have never discussed either of those so I probably shouldn't use it unless I can back it up with comments stating I know what those actually do. – Cody Brunner Jul 20 '15 at 20:23
  • @vincentleest yea I realized that too but the file is only 37 strings long so I'm not sweating it for the assignment. Thanks for pointing it out though! – Cody Brunner Jul 20 '15 at 20:29
  • A few other gotchas: in `read_file()` you read into `Supplier supplier_list[50];` which is local. Your list will vanish as soon as the the function ends. For an assignment like this just make the sucker global. In real world you'd want to call read_file with a container you can pass around. Also in read_file, [give this a read.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – user4581301 Jul 20 '15 at 21:28
  • @user4581301 I wish I could man. No globals allowed. My prof. said the way this should work is that `read_file()` should read the file once and store the data in `supplier_list[50]`. That data should be accessed through `search_file()`. – Cody Brunner Jul 20 '15 at 21:36
  • OK. Supplier supplier_list[50] goes into main so you can call the sub-functions with it as a parameter. read_file and search_file need to take a reference to supplier_list. Looks like `void read_file(Supplier (&supplier_list)[50])`. If you pass the list around without the reference, you'll be passing copies, changing the copy, and discarding the copy when the function ends. You can also use pointers (`void read_file(Supplier * supplier_list)`) but they are more cumbersome to use, more than a bit less safe, and lose the array's size information. – user4581301 Jul 20 '15 at 23:21
  • Come to think of it, it might be worth wrapping supplier_list in it's own little structure like a poor man's non-resizable vector. `struct Supplier_List { Supplier supplier_list[50]; size_t num_suppliers;};` to keep the list and the number of elements in the list in one clean package. read_file becomes `void read_file(Supplier_List & list)` and now you know how many suppliers you really have in the list and can stop searching early. – user4581301 Jul 20 '15 at 23:26
  • @user4581301 Sorry man had some stuff I had to take care of thanks for your help I got it figured out! – Cody Brunner Jul 21 '15 at 02:31

0 Answers0