=====
CS 111 - Week 9 Lecture 1 - 2026-10-20
=====

=====
*** NOTE: added notes after class summarizing
    what we discussed about C++ compiling,

    more C++ coding standards,

    how to redirect C++ file output and
    C++ ERROR messages to a file,

    some debugging tips/suggestions,

    and a bit more...
=====

=====
TODAY WE WILL:
=====
*   announcements
*   continue intro to C++
*   prep for next class

=====
*   should be working on Homework 7!

=====
*   please DO download copies -- of your .cpp files especially --
    from the CS50 IDE regularly!

=====
NOTE:
*   the CS50 IDE is the official class C++ programming environment
=====
*   in labs, you are REQUIRED to use the CS50 IDE for the 
    pair-programmed lab exercises 

*   for homeworks, it is preferred that you use the CS50 IDE, but it
    is not required --

    HOWEVER!!

    *   Your submitted code MUST WORK when I compile and run it in the
        CS50 IDE, or it will NOT be accepted.

    *   If you are using the GNU C++ compiler (g++), you will *probably* be
        fine,

	but it is YOUR responsibility to double-check that your resulting 
        programs compile and run in the CS50 IDE!

=====
C++ identifier syntax
=====
*   more restricted than Racket's identifier syntax

*   must start with a letter
    then followed by 0 or more letters, digits, or underscores _

    (and it cannot be something already defined, such as a C++
    keyword, another function, etc.)

    *   course style is still:
        *   choose to be meaningful, descriptive, not too generic,
	    not misleading!

        *   named constants should be ALL_UPPERCASE
	*   function and parameter names should be
	    all_lowercase or initialThenCamelCase

*   which of these follow C++ identifier syntax?

    1st_prize    - NO, can't start with a digit
    prize2       - YES
    third-prize  - NO, can't contain a - (looks like a minus operator to C++ here)
    fourth prize - NO, can't have a blank within an identifier
    string       - NO, that's an already-defined name (the modern C++ string class)
    a_string     - YES

=====
C++ return statement
=====
*   syntax:

    return desired_expr;

    *   the type of the desired_expr must be compatible
        with the function's declared return type

*   important semantics to note:

    *   when a return statement is run,
        its expression is the value of this function call
	and the function ENDS at this point,
	returns this value,
	and returns to the caller

*   what if a C++ function HAS no return statement?
    *   if the function is not INTENDED to return anything --
        if it has the special return type of void --
	that's fine, the function will end when it reaches
	the end of the function body (its closing } )

    *   (if the function IS intended to return something,
        I believe you'll get an error -- if a C++ function is
	to return something, it SHOULD have a return statement...)

=====
definition of a C++ block
and some related CS 111 class coding standards!
=====
*   this is a C++ block:
    {
        statement;
	...
	statement;
    }

*   the C++ compiler considers that to be a single statement!
    and it can be put anywhere a single statement is allowed
    to go

*   notice, then, that in defining a function, you have a
    function header, and the function body is a block!

*   and CS 111 class style regarding blocks is as follows:

    1. Put { and } each on their OWN line (as shown above)
    2. They should be lined up with the preceding statement
    3. INDENT the statements within { and } by at least 3 spaces
    4. For statements that are intended to be sequential within
       a block, line up the beginning of these statements

    5. (But for a long statement, indent its continuation(s)
       on subsequent lines... like we did for long compound
       expressions in Racket)

=====
AND a CS 111 class coding standard for comments:
=====
*   put a blank line before and after each comment/set of comments

=====
Compilers and Interpreters
=====
*   BOTH of these are kinds of programs that are used to translate
    high-level programming language expressions and statements into
    lower-level instructions the computer can execute

*   an interpreter (DrRacket provides (mostly) an interpreted environment)
    translates AND then executes each expression EACH time you ask
    the interpreter to run

    a compiler, on the other hand,
    translates and then saves the lower-level instructions into a
    separate file,
    which can be linked and loaded into an executable file;
    then, the user runs that executable whenever and as many times
    as they would like (without having to RE-translate each time)

=====
*   NOTE:
    for a given programming language,
    it may have BOTH interpreters and compilers available for it!

    *   there ARE Racket compilers out there!
    *   I've heard there are C++ interpreters out there!

    ...but we happened to use an interpreted-style environment for BSL Racket,
    ...and we will be happening to use an environment for C++
       that includes a compiler for C++.

*   so, when using a compiler:

    ---------|     |----------|   |-------------|
    | source |     |          |   | executable  |
    |  code  |---->| compiler |-->|    code     |
    | file(s)|     | program  |   |   file      |
    |--------|     |          |   |-------------|
                   |----------|

    *   your source code file(s) are the INPUT to the compiler program!
    *   the compiler OUTPUTS a (linked-and-loaded) executable file

    *   if you have a syntax error in your source code,
        the compiler complains, and does NOT output any executable file!!!

    *   IN C++, the source code file(s) typically end in .cpp
    *   IN C++, the executable file typically ends with NO suffix
        (Linux/UNIX typical practice)

=====
*   a compiled executable file will usually only run on
    the operating system and computer hardware it was designed to
    run on --

    to run a C++ source code program on different operating systems
    and on different computers, you will tend to need to compile it
    on the desired computer

    *   but we'll be using a browser-based environment for our C++,
        which should (fingers crossed!) keep things simpler!

=====
*   so, typical work flow in CS 111's C++ portion:
    *   using the design recipe, you design your C++ functions
        in a C++ source code file whose name ends in .cpp
        *   and include a main function in that file

        *   (this is not the ONLY option, but it is a good way
	    to start!)

    *   you COMPILE this .cpp file
        and if your syntax is correct, you get an executable file
	as a result
	*   if your syntax is not correct, you try to correct it
	    and then you compile it again -- repeat until you get
	    an executable file!

    *   you then run that executable file to run your program
        and see if it worked!
	*   and debug your source code and recompile as needed.

=====
REMINDER: redirecting a program's OUTPUT to a file,
          and redirecting ERROR MESSAGES to a file
=====
*   in a Linux bash shell -- which the CS50 Terminal is --
    you can REDIRECT a program's so-called standard output
    (for example, what is printed by cout statments)
    to a file using > followed by the desired file to write to

    ./111lect09-1 > 111lect09-1-out.txt

*   BUT, > does NOT redirect error messages!

    You CAN use &> to redirect error messages to a file:

    g++ 111lect09-1.cpp -o 111lect09-1 &> error-msgs.txt

    *   IF there were any compiler error messages,
        they have now been written to a file error-msgs.txt
	instead of to the screen

    *   this is USEFUL if a tiny error has caused MANY error
        messages (as the C++ compiler got CONFUSED) --

        you can open a file such as error-msgs.txt in the 
	CS50 IDE and more easily get to the FIRST error message,
	which is where you should look FIRST when debugging.

    *   also: you can send me this file of error messages along with
        your .cpp file as attachments, and I can help with
	debugging!

=====
some ideas for DEBUGGING...
=====
*   compile failed?
    *   look at the FIRST error message,
        NOTE that it includes the line number in the source code
	where the compiler got confused!

    *   (it may have gotten confused AFTER the actual error --
        start at that line and work upward!)

*   compile succeeded, and got some falses?
    *   it can be useful to add some cout statement printing the
        function call's result and the expected result

    *   (especially for double values, remember that cout by
        default only displays about the first 6-7 significant
	digits -- so if the displayed digits are the same,
	you likely need to change your test to the style
	of seeing if the absolute value of the difference
	is small enough --

        abs( ex_call(3, 4) - expected_val ) < 0.001

	OR feel free to copy over and use demo.cpp's and 111hw7.cpp's
	check_within function if you would like!)

=====
*   and we walked through the design recipe steps for
    two C++ functions, circ_area and grade_in_range,
    using the CS50 IDE --

    see 111lect09-1.cpp posted with these notes