November 12, 2012

Code Library

This is a project I've been working on recently

I've been trying to cram a buttload of information into my tiny little brain, and I got the idea to build a code library that would store a lot of pertinent information in a way that would be easy to access.  And it would be another way to delve into C++ (we're learning java in my cse class right now and I'd rather program in c++, despite all the problems c++ comes with)

This was an interesting project.. I'm used to being given an assignment in class and having all the functions and procedures mapped out for me, down to the arguments we were given, what the code was supposed to do and what it was supposed to output.  All the design problems were already done for us, we just had to code the functions or procedures.  This wasn't the case for ALL our assignments but for the majority of them, especially earlier on in the course sequence.

Doing my own project was nice.  No looming deadlines, no tricky documentation required, and no worrying about getting a million points taken off for not clearing my arguments!

It was fun trying to think of a design that would be efficient, yet easy to code.  I decided to create a separate text file for each major topic (Array, Set, Unix/Linux, etc.) and within those topics I have subtopics, denoted by a *.  Within the *Subtopics I have blocks of information, enclosed by <subtopic> information </subtopic> (I got the idea from XML.  I was thinking about enclosing my information blocks in parentheses, or ** or the like but that's not safe enough.  What if I have parentheses inside my information blocks?  Or I decide it's necessary to splatter a bunch of *****'s inside those blocks?)  The main program prompts the user for input, and if the user types in something dumb, the program gently asks for clarification or exits gracefully.  As of now, at this point in my program, for all of the testing I have done so far, the program does not crash, regardless of dumb input from the user (me)

Here's the main program:
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include "InputOutput.h"
using namespace std;

// *** Remember to update the set every time new text files are added!!! ***

int main() {

    bool continueAsking = true;
    while (continueAsking) {

        // create set of all possible topics
        set<string> topics;
        topics.insert("array");
        topics.insert("set");
        topics.insert("input output");
        topics.insert("general tips and tricks");
        topics.insert("nonspecific interview questions");
        topics.insert("contest problems");
        topics.insert("unix linux");

        // receive user input for topic
        cout << "Hello master, what would you like to look for?\n"
             << "You can choose from the following: \n";
        outputSet (topics);
        cout << "\n>> ";
        string topic;
        getline (cin, topic);
        cout << "\n";

        if ( matchSubjectTitle(topic, topics) ) {
            cout << "List of topics regarding " << topic <<  ":\n";
           
            // topic is matched, now output topic subtitles
            string title = topic + ".txt";
            outputSubTopics (title);

            // get subTopic
            cout << "\nPlease enter a topic of interest: \n>> ";
            string subtopic;
            getline (cin, subtopic);
            cout << '\n';

            // find subTopic, output information block
            outputSubTopicBlock (title, subtopic);
            cout << "\nContinue searching?\n>> ";

            string answer;
            getline (cin, answer);
            string answerLower = toLowerCase(answer);
            if (answerLower == "no") {
                continueAsking = false;
            }

            cout << '\n';
        }

        // answer did not match with Subject title
        // give user a chance to try again
        else {

            string answer;
            cout << "Would you like to try again?\n>> ";
            getline (cin, answer);
            string answerLower = toLowerCase(answer);
            if (answerLower == "no") {
                continueAsking = false;
            }

            cout << '\n';
        }
    }

    return 0;
}

Not including the helper file that has all my functions/procedures since it would be too much.

And here is an example of one of the text files:
SET

* Stuff
<stuff>
stuff about stuff
</stuff>
(I'll add actual information here, later)

Ran into a few tricksters while coding this.  I'm still used to osu's made-up c++ language that hid a lot of work from us.  I'm used to sets having a Is_Defined function, but no such luck with c++.  This is what I used for my Is_Defined function:
if ( topics.find(answerLowerCase) != topics.end() ) { return true; }

Every time my program receives user input, it translates it into lowercase, no matter how it came in.  Just makes it easier and a lot more generalized than checking for case errors.  So you could type in array or Array or arRaY, whatever strikes your fancy

And I'm used to one input from file operator that's perfect and does all the work for you.  C++ has a few.. it took me a couple tries to get the right one.  When I tested the main program and asked for "interview questions", it came up blank.  It took me a while to realize that cin >> stuff stops at the space character.  So I used getline (cin, stuff) instead to get the entire line.  But you can't use cin and then getline, something about the cin not getting rid of the space or newline character and the getline function reading the blank space.

Ugh.. I feel a bit crippled by resolve/c++.  I realize that the osu cse faculty put a lot of work into that language, drilling some very important concepts into our little minds, but it's hard to go from resolve to java or c++.

I was at acm's east regional whatever competition last weekend, and one of my group members said she realized she couldn't code without Google (while I couldn't code at ALL.  unfortunately resolve wasn't one of the c, c++, or java languages we were allowed to code in HA.  i basically drew pictures the entire five hours we were there).  And I'm scared of that reality.  I don't want to turn to Google every time I want to sit down and code something.  The code I had above, to find if an item is defined in a set?  I got that off Google.  I understand it but I wouldn't have been able to get that on my own.  I'm hoping fluency is something that comes with a lot of practice

Anyway..

I talked to a smart person about my little side project, and Smart Person told me it would be a good idea to use XML for this.  And I tried, I really did.  For at least a couple hours.  But there's a limit to how much I can learn at one time.. I also have schoolwork that I've been putting off to do this.  So I will save that for a future project.  For now I will keep updating and improving my code library since I think it's something that could be really useful to me.

And now to do ACTUAL schoolwork