Thread: sorting an array of structs

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    Angry sorting an array of structs

    I need some serious help. I am trying to make this dictionary program and i cannot figure out how to take the input list which consists of a command then the word then the definition and then insert it into an array of structs and make the array alphabetized.
    Here is my code. Please help!

    /* PROGRAM Dictionary

    AUTHOR: Joseph Westlake
    UNIQNAME: jwestlak
    GSI NAME: Clay Wood
    EECS/CMPTRSC 183 - Winter 2002
    Project Number: 6
    Due Date: Monday 4/15/02

    SUMMARY

    This program will function like an interactive library. The data values will be
    inputted from a text file that is specified by the user.

    INPUT

    - a file with the filename that is specified by the user
    - the file will contain
    - one of four commands
    - ADD (add a word to the dictionary)
    -the word and its definition
    - LOOKUP (look up a word in the dictionary)
    -the word that is to be looked up
    - DUMP (print a table of the current dictionary contents)
    - EXIT (exit the program)

    BAD DATA CHECKING

    The program should make sure that the data file has opened successfully, if not the
    program must terminate and display a descriptive error message. It also needs to make
    sure that the input commands are valid.

    OUTPUT

    The program will tell the user what the input is and what the program is doing for
    each command. All valid input will be printed.

    */
    //included header files
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstring>
    using namespace std;
    const int INSTRUCTIONS = 50,
    MAX_LENGTH = 25;
    struct Definition
    {
    string Word;
    string Meaning;
    };
    typedef Definition Instruction [INSTRUCTIONS];
    //user defined functions
    int main()
    {
    Instruction Data;
    int line;
    string Comm,
    fileName,
    Command[INSTRUCTIONS],
    Word,
    Meaning;


    cout << "============================================= " << endl;
    cout << " Dictionary " << endl;
    cout << "============================================= " << endl << endl;
    cout << "Please input the file name of the dictionary ";
    cin >> fileName;

    ifstream inFile;
    inFile.open (fileName.c_str());
    do
    {
    for (line = 0; line < INSTRUCTIONS; line++)
    {
    getline (inFile, Command[line]);
    Comm = Command[line].substr (0,6);
    if (Comm == "ADD ")
    {
    Word = Command[line].substr (7,15);
    Meaning = Command[line].substr (23,80);
    cout << "I will add the word " << Word << endl;
    cout << "It means: " << Meaning << endl;
    SORT (Data, Word);
    }
    else if (Comm == "LOOKUP")
    {
    cout << "Lookup" << endl;
    }
    else if (Comm == "DUMP")
    {
    cout << "Dump" << endl;
    }
    else if (Comm == "EXIT")
    {
    cout << "Exit" << endl;
    inFile.clear();
    inFile.close();
    }
    else
    cout << "The instruction " << Comm << " is invalid" << endl;


    }

    inFile.clear();
    inFile.close();
    }
    while (inFile);
    for (line = 0; line < INSTRUCTIONS; line++)


    return 0;
    }

  2. #2
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    I have no idea but please use [c o d e][/c o d e] tags

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    I agree use the [code] tagz when typing code, to damn hard to read it otherwise. Read the FAQ and or the Sticky posts at the top of this forum plz.

    kwigibo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  2. better way to scanf of a array of structs member?
    By stabu in forum C Programming
    Replies: 3
    Last Post: 07-17-2008, 04:51 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Replies: 12
    Last Post: 12-06-2005, 08:30 PM
  5. sorting an array of structs
    By singletrackmind in forum C++ Programming
    Replies: 8
    Last Post: 11-13-2001, 03:33 AM