COSC1315 Fundamentals of Programming
Instructor: Prof. Richard G. Baldwin
Assignment Number: 21
Object-Based program that illustrates a sentinel loop using a do-while loop.
Making certain that you comply with the General Requirements, write a C++ program that meets the following specifications:
BEGIN WRITTEN SPECIFICATIONS
This program must use Object-Based Programming Syntax.
The doSomething function shown below implements a sentinel loop using a while loop structure. Rewrite the doSomething function and write all of the additional code necessary to create an Object-Based program that will produce exactly the same results using a do-while loop structure instead of a while loop structure.
//Begin program code
//This is a while-loop version of a sentinel loop.
void doSomething(){
cout << "Put your name here" << endl;
cout << "Enter a positive integer or -1 to quit."
<< endl;
//Do a priming read
int temp = 0;
cin >> temp;
while(temp != -1){
cout << "You entered: " << temp << endl;
cout << "Enter a positive integer or -1 to quit."
<< endl;
cin >> temp;
}//end while loop
cout << "You entered: " << temp << endl;
cout << "Bye" << endl;
}//end doSomething function
//End program code
Program Output:
Put your name here Enter a positive integer or -1 to quit. 11 You entered: 11 Enter a positive integer or -1 to quit. 22 You entered: 22 Enter a positive integer or -1 to quit. 33 You entered: 33 Enter a positive integer or -1 to quit. -1 You entered: -1 Bye
END WRITTEN SPECIFICATIONS
As specified in the General Requirements, copy the above specifications, including the two lines of upper-case characters and paste the specifications into a comment block at the beginning of your source code file.
Note: Sometimes when copying C++ source code into HTML documents (like this one) the code will become corrupted. This is particularly true for code that involves left and right angle brackets such as in:
#include <iostream>
if(x <= y){
Although Prof. Baldwin has made an effort avoid such problems, it is possible that some corruption of source code may have occurred during the creation of these HTML documents. If you see anything that you believe might be corrupted source code, please notify Prof. Baldwin so that he can look into the problem, correct the code if necessary, and make all of the students aware of the problem.
-end-