7B. Programs and Main


Main

A C++ program is a collection of functions, exactly one of which is called main. The program starts by running main, and stops when main returns. All of the other functions constitute a toolbox for accomplishing the desired task.

For now, the heading for main should be

  int main()
That is different from Java. This main function does not take any parameters and it returns an integer, which is a status value telling whether the program was able to complete its job without errors. A status value of 0 indicates that all went well. Any other status value indicates that there was an error. (The status value should be a number from −128 to 127. Typically, only 0 and 1 are used.) If you are sure that your program will work, just return 0.
  int main()
  {
    
    return 0;
  }
Here is a complete program that just writes Hello.
  #include <cstdio>
  using namespace std;

  int main()
  {
    printf("Hello\n");
    return 0;
  }

Later, we will see a more general version of main that takes some parameters, but the simple parameterless form will do for now.


Documenting a program

Production software has extensive documentation, often in the form of a book. Obviously, you don't have time for that in this course. But the standards for this course do require some documentation to be written in the program.

Write a comment near the beginning of every program telling what the program does, so that someone who looks at the program will immediately know the program's purpose and how to use the program without the need to reverse-engineer the program. The standards require such a comment.

That comment should describe the input format and provide a sample input. Provide all the detail needed for a person to read the comment and know how to write the input. Be precise, not vague. You are allowed to copy material from the assignment document into the initial comment. That will not be considered plagiarism.

Here is a sample.

  // This program reads a single positive integer, n, and writes 
  // information about the hailstone sequence that starts with n.
  // It shows the sequence, the sequence length and the largest
  // number in the sequence.
  //
  // Example:
  // Input:  
  //   5
  // Output:
  //   The hailstone sequence starting with 5 is
  //   5 16 8 4 2 1.
  //
  //   The sequence length is 6.
  //
  //   The largest number in the sequence is 16.

Notice that the comment gives a name, n, to the input, and refers to the input as n. It does not use awkward phrases such as "the number that was read from the user."

You can use the above comment in a program that you submit. It will not be considered plagiarism.


A complete program

Here is a complete C++ program. It uses function atan(x), which is tan−1(x), also called arctan(x), and which is available if you include <cmath>.

// Author:    Karl Abrahamson
// File:      example.cpp
// Tab stops: none

// This program reads two points (x1,y1) and (x2,y2)
// from the standard input.  The input format is
//   x1 y1
//   x2 y2
// It writes the angle between horizontal and the
// line from (x1,y1) to (x2,y2), in radians, on the
// standard output.
//
// For example, on input
//   1.0 1.0
//   2.0 2.0
// It shows:
//
// The two points are:
// A = (     1.000,      1.000)
// B = (     2.000,      2.000)
//
// The angle between horizontal and the line from A to B is
//     0.785 radians.

#include <cstdio>
#include <cmath>
using namespace std;

//================================================
//                    getAngle
//================================================
// getAngle(x1,y1,x2,y2) returns the angle between
// a horizontal line and the line from point (x1,y1)
// to (x2,y2).
//================================================

double getAngle(double x1, double y1, double x2, double y2)
{
  double rise = y2 - y1;
  double run  = x2 - x1;
  return atan(rise/run);
}

//================================================
//                    EchoInput
//================================================
// EchoInput(x1,y1,x2,y2) shows the input points
// (x1,y1) and (x2,y2) on the standard output.
//================================================

void EchoInput(double x1, double y1, double x2, double y2)
{
  printf("The two points are:\n");
  printf("A = (%10.3lf, %10.3lf)\n", x1, y1);
  printf("B = (%10.3lf, %10.3lf)\n\n", x2, y2);
}

//================================================
//                    ShowOutput
//================================================
// ShowOutput(angle) shows the result angle 'angle'
// on the standard output.
//================================================

void ShowOutput(double angle)
{
  printf("The angle between horizontal and the line from A to B is\n");
  printf("%10.3lf radians.\n", angle);
}

//================================================
//                      main
//================================================

int main()
{
  double x1, y1, x2, y2;

  printf("What are the points?\n");
  int numRead = scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
  if(numRead != 4)
  {
    printf("Could not read the points\n");
    return 1;
  }
  else
  {
    EchoInput(x1, y1, x2, y2);
    double angle = getAngle(x1, y1, x2, y2);
    ShowOutput(angle);
    return 0;
  }
}

Summary

Every program must have one function called main. For now, we will use heading

  int main()
The value returned by main is passed back to the operating system. A returned value of 0 indicates that the program did its job. Any other returned value indicates that something prevented the program from completing its job.

Write a comment near the beginning of a program telling (1) why a person might want to use the program, and (2) how to use it. Don't include information about how it works.


Exercises

  1. Write a complete program that reads a real number from the user (after a prompt) and writes the square root of that real number. Answer