Structured Programming in C++
Camden County
College
Prof. Gregory Safko
All of your programs should begin with a descriptive comment similar to the one here.
In addition, returning EXIT_SUCCESS (defined in <cstdlib>) is a portable way for main to indicate to the operating system that your program ran successfully. (You can also choose to return EXIT_FAILURE when appropriate.) The textbook's "return 0;" is a little white lie: 0 happens to be the "success" indicator on Windows and Unix systems; but EXIT_SUCCESS and EXIT_FAILURE will always be properly defined, no matter which operating system you move your program to.
// Filename: sum.cpp
// Author: Your Name <you@yourISP.zone>
// Structure Programming in C++, Lab 01, part x
// Description: Reads two integers; prints their sum
// Written:
// Modified: January 31, 2004
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
/* ... Your main program goes here ... */
return EXIT_SUCCESS;
}
Write, compile, and run a C++ program that reads in two integers and then outputs their sum, difference, product, integer-division quotient, remainder, and floating-point division.
Use a cout-statement without "\n" or endl before reading an input number, so the user's input will appear on the same line as the prompt. The output should redisplay the integers that were typed in, as well as showing the arithmetic results in a nice human-readable form.
Here is one possible output format: (user input is underlined, for illustration purposes only)
Please enter an integer: 6
Please enter another integer: 9
6 + 9 is 15
6 - 9 is -3
6 * 9 is 54
6 / 9 (integer division) is 0
6 % 9 is 6
6 / 9 (floating-point division) is 0.666667
Remember that cout displays numbers with no space or punctuation before or after them. The strings such as " + " and " is " contain extra spaces to separate the numbers from the words.
Multiple items in a cout statement must all have << between them. There is one semicolon, after the last item. The cout statement may extend across several lines in the program file, for example,
cout << first << " + " << second
<< " is " << first+second << endl;
(The general rule is that except in string constants -- literal pieces of text in double quotes like "Hello world!\n" -- any combination of spaces and line breaks is allowed wherever one space is allowed. You can use this to make a program more or less readable. Be kind to yourself and other humans who read your programs.)
What happens when the second integer is 0?
Optional this week (will be required later): Your program
may skip the divisions and remainder if the second integer is zero.
(You will need to use an
if statement; see Section 2.4 of
the textbook, "Simple Flow of Control".)
Write a program that asks for numbers from the user. These numbers will be used to generate a simple story. You may use the following story as a template, or make up your own.
Here is one possible output format: (user input is underlined, for illustration purposes only)
Please enter an integer between 2 and 5: 4
Please enter an integer between 1 and 11: 7
Please enter a number of type double between 1.0 and 4.0: 2.1
Once upon a time there lived a dragon with 4 heads, who terrorized the land of Rohrer. One day at 7 in the morning this dragon came to the campus and demanded a grade point average of 2.1 for his final exam.