Output to cout

Our first C++ programs must tell the computer to produce output; otherwise we would never know if the programs were executed correctly, or even if they were executed at all.

dime1.C:

  1. A program is written as one or more files (documents). On our Fedora Linux server storm.cis.fordham.com, the filename ends with .C (uppercase); on other machines, the filename might end with .CPP or .CXX.
  2. A C++ program is case-sensitive: it makes a difference if you write in uppercase or lowercase.
  3. Produce output to verify that the program ran.
  4. std::cout means “the outside world, considered as a destination for output”. This output will be a stream of characters; that’s what the c in cout stands for. A namespace is a family of things that share the same last name, in this case the name std. Last name first, first name last. No space between the two colons. #include a header file.
  5. The << (“put to”) operator points towards the destination. No space between <’s; type it exactly the way I do.
  6. "double-quoted" strings and the invisible newline (\n). 24 characters, followed by the newline character, followed by the terminating zero character.
  7. Semicolon (;) at the end of each statement.
  8. A C++ program consists of sections called functions. Even if you have no desire to divide the program into functions, the program must still consist of one big function. The one big function must be named main. The lines
    int main()
    {
    
    mark the start of the main function; the line
    }
    
    marks the end. The body of the function is the statements between the two curly braces. Conventional to indent the body of the function by one tab. The parentheses will not be used until later; we still have to write them even though they’re empty.
  9. Return an int from the main function to the operating system: EXIT_SUCCESS for success, EXIT_FAILURE for failure. Another header file.
    1. Unix Korn and Bash shells (including Macintosh Terminal): echo $?
    2. Unix C shell: echo $status
    3. Windows Command Prompt: echo %errorlevel%
    Windows: system("PAUSE");
  10. Header files, where to find them on storm.cis.fordham.edu:
    1. /usr/include/c++/14/iostream
    2. /usr/include/c++/14/cstdlib for EXIT_SUCCESS
    We have verison 14.2.1 of the C++ compiler:
    c++ -v
    Search for a file with a given name on storm.cis.fordham.edu:
    find / -type f -name iostream 2> /dev/null
  11. Comments:
    1. // for single-line
    2. /* */ for multi-line
  12. What happens if you accidentally omit a semicolon, double quote, comment delimiter, etc.?
  13. Log into the Fedora Linux server storm.cis.fordham.edu and explore your account. See below.
  14. variations on dime1.C:
    1. dime2.C no newlines
    2. dime3.C double spaced
    3. dime4.C using namespace std (p. 52)
    4. dime5.C perform all output with one statement (only one semicolon)
    5. dime6.C one multi-line statement
  15. Homework: flag.C: Put in a using directive to get on a first-name basis with cout. Do all the output with only one statement, not ten statements. Run the program and deposit the output into a file named flag.txt. Copy the program and flag.txt into your public_html directory so everybody can see these two files.
  16. Exercise: invisible characters (pp. 53–54). Can your hear three beeps when you run the following program? Turn up the volume if necessary.
    	//This is only one statement, not a complete C++ program.
    	//Output three copies of the ASCII "alarm" character.
    
    	std::cout << "\a\a\a\n";
    
  17. Exercise. Unreachable code. What happens if you insert a statement after the return?
    	//Not a complete C++ program, just a fragment.
    
    	return EXIT_SUCCESS;
    	std::cout << "Never reaches this point.\n";
    }
    
  18. chinese.C: an attempt to output the Chinese characters 上 下 川 (up, down, stream).