Skip to end of metadata
Go to start of metadata

The goal of this project was to investigate File I/O, functions and how to create a Word Counter in C++

These are all extensions. 

Task 1: File I/O

Reading in files and standard input from the command line requires some of the standard C++ libraries to be included in the program.  Many different types of files can be read in, including text files, pdfs (which are binary), and csv (another type of ASCII text file):

The user can interact with a program using the terminal and cin.getline(), which gets a line from the terminal standard input:

This asks the user a question and then waits until they type a response and then respond using the response:

In the example above, the user input is copied into a char array, which is different than a string type in C++.  Thus, strings and standard input are slightly different in C++.  In the other examples of reading files, they are read character by character to eliminate the need to deal with the string vs char array challenge when reading in the file.  Since char is a basic type in C++ is can very simply be outputted to the terminal.  

And the output of writing to a text file:

Is:

Task 2: Functions

Functions in C++ need to have a return type in its function declaration.  For example, this returns an int:

It can have a void return type in which case there is no need to have a return statement in the function:

There are a few ways to overload functions: 

The first is to make the function generic so it can be used on different types.  C++ uses templates to make generic functions:

There can also be different functions with the same name as long as they have different signatures - different parameters or return value.  For example:

And finally, C++ operators can be overloaded, usually within classes:

You can create functions with an unknown number of arguments as well.  To do this, the first argument of the function needs to be the number of arguments given to the function and then the ... denotes the rest of the arguments.  This is an example of how to create a function that finds the max of any number of arguments:

There is no way to dynamically create functions in C++. 

There is no way to override functions with the same signature in C++:

If the second function is uncommented, it throws a compile time error.  

Functions can be called in other functions as long as they have been previously defined.  Header (.h) files are used to call functions declared in another file.  To do so, the file with function declaration (in my case task2b.cpp) needs to include the header (task2b.h) which includes the function signature, and the file that wants to call the function needs to include the header file as well.  Then when the file is compiled using g++ task2c task2c.cpp task2b.cpp, ./task2c can be called an execute the function:

 

Task 3: Word Counter

The third task was to create a word counter that counts the number of occurrences of each word in a text file.  It opens a text file given on the command line, then reads each word from the text file, makes all characters lowercase and eliminates all punctuation.  Then it creates a final list of words by checking if the word is already in the list and if so increments its associated number and if not, adds it to the lsit with a number 1. Then I used the quicksort algorithm that I made in Project 4 with a new compare function that compare the numbers associated with the word to get the list in order from most common to least common.  Then it prints either the first twenty items in the list or the whole list if the list is less than 20 items long. 

The output from calling this with wctest.txt is:

Other Extensions:

4. The fourth extension I completed was to write a compilable and runnable haiku in C++.  It is about getting a line from stdin in the terminal:

When run (if the user types in Kayla):

5.  I added to my word counter so that it checks to make sure exactly one file is given and that the file is opened properly.  If not, it throws an error:

Labels
  • No labels