Computer Science 236

Intro Lab Session


In this lab you will:

  1. Access a computer remotely
  2. Get strings from the command line
  3. Read from an input file into a string
  4. Count some types of characters in the input file
  5. Test your code

Important Note


The ssh and scp commands described below will only work from within the CS network. There are two ways to access the CS lab machines from outside the CS network.

  1. DUO

    This is the preferred approach. With this approach you will connect to "moat.cs.byu.edu". Your ssh command should look like this:

    ssh user@moat.cs.byu.edu
    

    The first time you run this command you will need to complete some setup steps described here:
    https://docs.cs.byu.edu/doku.php?id=remote-access-home
    After the first time, you will need to accept a DUO push on your phone each time you connect.

  2. VPN

    You may want to use this approach if the DUO approach is not working for you. With this approach you will connect to the CS VPN. Instructions for using the VPN are found here:
    https://docs.cs.byu.edu/doku.php?id=vpn-configuration-and-use
    After connecting to the VPN, you can ssh to schizo as described below.


Part 1: Remote Access


  1. Open a terminal window

    If you're using a Windows system run either the "Windows PowerShell" app or the "Command Prompt" app. For a MacOS or Linux system run the "Terminal" app.

  2. Login to a CS lab machine

    The "ssh" command is used to login to another machine. Replace "user" in the command below with your BYU netid. Type your BYU CS account password at the 'Password:' prompt. (note there is no feedback while you type the password)

      $ ssh user@schizo.cs.byu.edu
    

    If you have trouble with your CS account, look first at:
    https://docs.cs.byu.edu/doku.php?id=setting-up-your-account-with-the-cs-authentication-system
    If that doesn't solve the problem, submit a ticket at:
    https://ticket.cs.byu.edu
    The System Administrators may be contacted in-person in 1140 TMCB or at system@cs.byu.edu if needed.

  3. Make a file (hello.cpp)

    The "nano" command allows you to write code in a file. (You are welcome to use another editor if you prefer.)

       $ nano hello.cpp
    

    Enter the following code into the file:

         #include <iostream>
         using namespace std;
         int main() {
           cout << "Hello" << endl;
         }
    
  4. Compile the code (hello.cpp)

    The "g++" command runs the C++ compiler and produces an executable file.

       $ g++ -Wall -std=c++17 -g hello.cpp -o lab0
    
  5. Run the code

    The name of the executable file is "lab0" and "./" allows the terminal to find the file in the current folder.

       $ ./lab0
    

    The output should look something like this:

    Hello
    

Part 2: Command Line Parameters


  1. When running a program such as "lab0" in the previous example, the strings typed on the command line are passed into the "main" function of your program. This functionality is often used to pass a file name to the program.

    The following declaration of the "main" function shows the parameters "argc" and "argv" that are passed to the function when it is called.

    int main(int argc, char* argv[]) {
       ...
    }
    

    Suppose the program is run from the command prompt and a file name is also given on the command line like this:

    $ ./lab0 filename.txt
    

    In this case, the values passed to main would be 2 for "argc", ./lab0 for "argv[0]", and filename.txt for "argv[1]".

  2. Add code to your main function to print "argc" and "argv[0]" and "argv[1]".

    Compile and run.

       $ g++ -Wall -std=c++17 -g hello.cpp -o lab0
       $ ./lab0 filename.txt
    

    The output should look something like this:

    argc: 2
    argv[0]: ./lab0
    argv[1]: filename.txt
    
  3. When we run your code for the projects in this class, we'll give the name of the input file on the command line. Your code should use "argv[1]" to get the name of the input file.


Part 3: Reading from a file


  1. One way to read input files is to read the entire input file into a string. The nice thing about this approach is that you can use string operations to process the input which is often easier than using file operations. This approach could cause problems for large files, but we don't expect to use input files that are too large.

  2. Add the following file reading code to your main function. This code reads from a file and puts the text that was read from the file in a string variable named "input". The variable "filename" should be a string variable and should contain the name of the file you want to read. (You could get that from "argv[1]".) You'll also need #include's for "fstream" and "sstream".

      ifstream in;
      in.open(filename);
      stringstream ss;
      ss << in.rdbuf();
      string input = ss.str();
      in.close();
    
  3. Add some code to print the text that was read.

    First print the line:

    *** Start of Input ***
    

    Then print the "input" string that was read.

    Finally print the line:

    *** End of Input ***
    

    Compile and run. (If you need a file to test with you might use "hello.cpp")

    The output should look something like this:

    *** Start of Input ***
    (lines from the file that was read)
    *** End of Input ***
    
  4. Take a screenshot showing your terminal and the resulting output.


Part 4: Counting Characters


  1. Some kinds of input characters will be important for the projects in this class. One of these is the newline character. You will need to recognize newline characters so you can keep track of line numbers. In the C++ language the newline character is represented with the following character literal.

    '\n'
    

    Other kinds of characters you will need to recognize in the input are whitespace characters, digits, and letters. The C++ library has the following functions that will help you recognize these kinds of characters.

    isspace(c)
    isdigit(c)
    isalpha(c)
    

    The "isspace" function returns "true" when 'c' is a whitespace character.
    The "isdigit" function returns "true" when 'c' is a digit.
    The "isalpha" function returns "true" when 'c' is a letter.

    To use these functions you need to include the "cctype" header file.

  2. Add code to your program to count the total number of characters in the input, as well as the number of newline characters, whitespace characters, digits, and letters. Print the each of these counts to the output.

    Compile and run.

    The output should look something like this:
    (Your counts will be different. They depend on the file you are reading.)

    chars: 32
    lines: 4
    spaces: 8
    digits: 2
    letters: 16
    
  3. Take a screenshot showing your terminal and the resulting output.


Part 5: Test Your Code


  1. We have prepared some files for testing your character counting. Download the Lab 0 test files from this link:

    https://faculty.cs.byu.edu/~barker/cs236/labs/lab0-tests.zip

  2. Copy the Lab 0 files to a CS lab machine

    The "scp" command is used to copy files to another machine. (The "scp" below is copying from your local computer to the remote. You should run it in a terminal window on your local computer.) Replace "user" in the "scp" command below with your BYU netid. Type your BYU CS account password at the 'Password:' prompt. (note there is no feedback while you type the password)

       $ scp lab0-tests.zip user@schizo.cs.byu.edu:
       (You should run "scp" on the local computer.)
       (Don't forget the colon ':' character on the end.)
    
  3. Unpack the files (on the remote machine)

       $ ssh user@schizo.cs.byu.edu
       $ unzip lab0-tests.zip
       (You should run "unzip" on the remote computer.)
    
  4. Run your code and capture the output

       $ ./lab0 lab0-tests/input01.txt >actual.txt
    
  5. Compare the expected output with the actual output

    Each input file has an expected output file. Your output should have the same counts as the expected output. The "diff" command will tell you what's different. (There is no output when the files are the same.)

       $ diff lab0-tests/answer01.txt actual.txt
    
  6. Run all the tests

    The file "run-tests.sh" contains a script that will run all the inputs and compare the actual outputs with the expected outputs.

       $ sh run-tests.sh
    
  7. Take a screenshot showing your terminal and the resulting output.


Part 6: Submission


  1. Zip your code (on the remote machine)

    The "zip" command below packages all .h files and .cpp files into a single zip file. (You probably don't have any .h files for this lab, but this command will work for other labs where you do have .h files.)

       $ zip lab0-code.zip *.h *.cpp
       (You should run "zip" on the remote computer.)
    
  2. Copy the zip file to your local computer

    The "scp" command below is copying from the remote to the local computer. You should run it in a terminal window on your local computer. Replace "user" in the "scp" command below with your BYU netid.

       $ scp user@schizo.cs.byu.edu:lab0-code.zip .
       (You should run "scp" on the local computer.)
       (Don't forget the period '.' character on the end.)
    
  3. Submit your screenshots and the zip file containing the code you wrote during this session to Learning Suite.