We shall write a program to guess a number. The interaction
is intended to be as follows.
> guessIn the above, guess is the name of the program and all program output is italicized. What you type is in boldface.
Think of a number.
Add 7 to it.
Give me the result : 10
The number you thought of was 3
We start out by opening a file in which to write the "source code" or program. Type the following in the Cygwin window.
gvim guess.cppTo guess the right number, the program has to do the following steps in addition to those is Part 1.
/* Program to guess a numberNow save the file and exit the editor. Compile the program by typing the following.
* Assignment : Lab 2 Program 1
* Author : your name
* Date : mm-dd-yyyy */#include <iostream>
using namespace std;
int main()
{// Ask user to think of a number and add 7 to it.
cout << "Think of a number" << endl;
cout << "Add " << 7 << " to it." << endl;// Prompt the user for the result
cout << "Give me the result : ";// Input the result
int result;
cin >> result;// Calculate and print original number
cout << "The number you thought of was " << result - 7 << endl;return 0;
}
g++ guess.cpp -o guessRun the program by typing
guess
>rectareaShow the demo to the TA. Print out your source code and hand it in to the TA.
Enter the length : 10
Enter the width : 5
The area of the rectangle is 50