CSCI 159 Lab exercise 1, F24N01/N02

Introductory lab notes

Most lab exercises will consist of two parts:

Ideally you complete all of part 1 and get started on part 2 while you're in your weekly lab, then complete and submit it on your own time over the following two weeks (specific deadlines can be found on the main labs page).

For this first lab, however, the entire lab will be done in a very paint-by-numbers fashion to introduce the core tools and processes we'll need to follow for the rest of the term, and you'll finish and submit it prior to the start of your next lab (a week later).

In this first lab we'll also have to go through a couple of setup steps to get your account and files/directories set up and organized for the term.

For each lab, there are several key C++ syntax elements you'll need to be familiar with.
The core part 1 syntax elements can be found here.

NOTE THAT I'LL BE DISTRIBUTING YOUR CSCI LOGIN IDS IN THIS LAB
you will be unable to login on our linux servers to work on your labs until you have this information

The objectives for this lab are to master some basic linux skills, learn how to obtain and submit labs, and learn how to edit, compile, and run C++ programs in a linux environment,

The layout of instructions for this part of lab 1 will be quite different than for the remaining labs. In this lab, instructions are laid out in a very step-by-step fashion to walk you through an introduction to our linux labs, your accounts, and the use of C++.

Be sure to go through each of the steps carefully, and in the order given, successfully completing each step before moving on to the next one.


1. Get logged in to your CSCI account and open some needed tools

(you'll do this every time you want to work on one of your CSCI labs)


2. Practice some basic linux:


3. Get a special file that we'll use for the rest of the term

We're going to copy a special 'makefile' from the instructor's account to your home directory.

This allows us to bundle up sets of linux commands, and run them through a program called 'make'. This semester I'll be providing a variety of makefiles to simplify the process of obtaining, compiling, and submitting your lab exercises.

This very first makefile belongs in your home directory and will be named make159. Instructions for obtaining using it are as follows:


4. Go to today's lab directory and try out the starting code


5. Try editing, compiling, running, and submitting the starting code


6. Try closing things up and logging out (then we'll sign back in to do more work)

You've now completed one cycle of our typical lab process, so next we'll try logging out, logging back in, and getting back to our lab1 code to work on it further.


7. Sign back in and get set up to continue working

Now let's log back in (on the same machine or any other in the lab, they're all accessing the same shared file system, so your account contents will look the same on each).


8. Now let's turn our lab1.cpp into a real program and submit it

  1. For lab1 we're writing a short program to get the user to enter some information about a bunny sighting on campus: telling us what day they saw it, what colour it was, and how big it was. Our program will then display a summary of their sighting information back to them.

    The steps below will walk through the construction of the program

  2. We've got the basic structure of a C++ program in our lab1.cpp already,
    #include <iostream>
    
    int main()
    {
       std::cout << "Welcome to lab1!" << std::endl;
    
       return 0;
    }
    

  3. First we'll add another library we're going to need: string (we'll use string variables to hold the colour and size of the bunny and what building it was closed to). The top of the program will now look like:
    #include <iostream>
    #include <string>
    
    Change the file content, save, then try recompiling in the terminal window
    make lab1x
    If you get any compilation errors then go back to the editor and keep fixing/compiling until it's ok, then try running it. (For the moment, the behaviour will be exactly the same as before.)
    ./lab1x

  4. Next we'll display a short intro to the program so the user has some idea of what's going on.

    We'll add this using more cout statements, replacing the single cout we originally had, e.g.
       std::cout << "Welcome to BunnySight: the program that lets you enter information about a ";
       std::cout << "bunny sightings on the VIU campus." << std::endl;
       std::cout << "You will be asked to enter the date, ";
       std::cout << "the bunny size and colour, and what building it was closest to." << std::endl;
    

    Again try compiling/fixing until it compiles cleanly, and now when we run it we should see the message above.

  5. Now we can add some variables to hold the date, bunny size/colour, and the building number.

    The day (1-31), month (1-12), year, and building number will all be entered and stored as integers, whereas we'll have them enter the size (small, average, large) and colour (e.g. brown) as words. The two data types we'll use can thus be int and string.

    For each variable we'll pick a meaningful name and we'll put a descriptive comment beside the variable.

    We'll put all this just after the { at the start of the main routine, e.g.
    int main()
    {
        int day, month, year; // the date the bunny was seen (1-31 for day, 1-12 for month)
        std::string colour; // the colour of the bunny as a single word, e.g. "brown"
        std::string size; // the size of the bunny as one of "small", "average", "large"
        int building; // the number of the building closest to the bunny (e.g. 315)
    
        ... the cout's would be down here ...
    

    Again try compiling/fixing until it compiles cleanly other than warnings about unused variables (those will go away as we actually get the user to input some data).
    If we run it now (i.e. the ./lab1x command) it should still just give the descriptive message then end.

  6. Now, one-by-one, we'll prompt the user to enter part of the information and we'll read it into a variable, e.g.
       std::cout << "Enter the day you saw the bunny (1-31)" << std::endl;
       std::cin >> day;
         

    These will go below our earlier cout (so they see the program description *then* it starts asking for data) but above the return 0 (since that effectively ends a run of the main routine).

    Do similarly for each of the six variables, giving the user a clear description of what they should be entering for each, e.g.
       std::cout << "Enter the size of the bunny as either small, average, or large" << std::endl;
       std::cin >> size;
    
    As usual, compile and fix until it compiles ok. (Notice the warnings for the variables have changed, now it tells us we've set values for them but aren't using them. This will be fixed in the next step.)
    Now if we run the program it should prompt us to enter the various values, stopping and waiting for us to enter a value each time.

  7. Finally we'll add a section to display all the stored data back to the user in a meaningful way. This will go after the last of our cin statements (but before the return 0).
       std::cout << "To summarize your sighting:" << std::endl;
       std::cout << "   it was on " << day << "/" << month << "/" << year << std::endl;
       std::cout << "   the bunny was " << colour << " and " << size << std::endl;
       std::cout << "   and was near building " << building << std::endl;
    

    Compile and fix (as usual), and hopefully the program should behave as expected when run: giving the user a description of the program, prompting the user for each part of the sighting information and reading it in, then displaying all the information back to the user at the end.

    Keep tweaking and fixing the program until you're satisfied it is working correctly and you're happy with the appearance of the output.


9. Sample output

The example below shows a sample run of the program.

Just to make the output below clearer, things the user typed have been emphasized in bold italics. (That wouldn't be the case when you run your programs, it's just to make the example a little clearer.)

Welcome to BunnySight: the program that lets you enter information about bunny sightings on the VIU campus.
You will be asked to enter the date, the bunny size and colour, and what building it was closest to.

Enter the day you saw the bunny (1-31)
10 
Enter the month you saw the bunny (1-12)
9 
Enter the year you saw the bunny (e.g. 2024)
2024 
Enter the colour of the bunny as a single word, e.g. brown
blue 
Enter the size of the bunny as one of small, average, or large
large 
Enter the number of the building nearest the bunny, e.g. 315
300 

To summarize your sighting:
   it was on 10/9/2024
   the bunny was blue and large
   and was near building 300


10. Code standards

In future labs your code will also be required to meet the course code standards, but for this first lab we'll focus on just getting the functionality correct and making sure we can submit it successfully.


11. When you're done:


12. Deadlines and late penalties

Submission deadlines and late penalties for all lab exercises are provided on the main labs page.


13. Supporting tools

Most of the time we won't complete the entire lab in the two-hour lab block, so you'll want to come back to the lab later in the week or connect from home.

Discussion of the basic tools and steps for doing so can be found here.