0000 // ============================================================================ 0001 // File: Sample.cpp 0002 // ============================================================================ 0003 // Programmer: Scott Edwards 0004 // Date: 08/14/99 0005 // Class: CSCI 123 ("Intro to Programming Using C++") 0006 // Time: MW 11:00am 0007 // Instructor: Mr. Edwards 0008 // Project: LoopSum 0009 // 0010 // Description: 0011 // This program is a contrived example to illustrate the coding style 0012 // guidelines handout. The user is prompted for a positive integer, which 0013 // is used as an upper bound for a loop. Then the sum of all intgers from 0014 // one to the upper bound is calculated and displayed to the standard 0015 // output stream. 0016 // 0017 // ============================================================================ 0018 0019 0020 #include 0021 #include 0022 0023 0024 // function prototypes 0025 int CalcSum(int limit); 0026 bool TestIntValue(int value); 0027 0028 0029 0030 // ==== main ================================================================== 0031 // 0032 // ============================================================================ 0033 0034 void main(void) 0035 { 0036 auto bool bIsValid; 0037 auto int sum; 0038 auto int upperBound; 0039 0040 // get the upper bound from the user 0041 cout << "Please enter a positive integer value: "; 0042 cin >> upperBound; 0043 0044 // make sure the value is valid 0045 bIsValid = TestIntValue(upperBound); 0046 if (false == bIsValid) 0047 { 0048 cout << "Sorry, you must enter a positive value\n"; 0049 exit(EXIT_FAILURE); 0050 } 0051 0052 // calculate the sum from one to the upper bound 0053 sum = CalcSum(upperBound); 0054 0055 // display the results to stdout 0056 cout << "The sum is " << sum << endl; 0057 0058 } // end of "main" 0059 0060 0061 0062 // ==== CalcSum =============================================================== 0063 // 0064 // This function calculates the sum from one to the limit parameter 0065 // (inclusive), then returns the value to the caller. 0066 // 0067 // Input: 0068 // limit -- an integer value containing the upper bound for the 0069 // calculation of the sum 0070 // 0071 // Output: 0072 // The sum of all integers from one to the value of the limit parameter. 0073 // 0074 // ============================================================================ 0075 0076 int CalcSum(int limit) 0077 { 0078 auto int counter; 0079 auto int total; 0080 0081 for (counter = 1, total = 0; counter <= limit; ++counter) 0082 { 0083 total = total + counter; 0084 } 0085 0086 return (total); 0087 0088 } // end of "CalcSum" 0089 0090 0091 0092 // ==== TestIntValue ========================================================== 0093 // 0094 // This function tests the input value to make sure that it is positive. If it 0095 // is, a value of "true" is returned; else, a value of "false" is returned. 0096 // 0097 // Input: 0098 // value -- an integer value to be tested 0099 // 0100 // Output: 0101 // A value of "true" is returned if the input parameter is positive, 0102 // otherwise a value of "false" is returned. 0103 // 0104 // ============================================================================ 0105 0106 bool TestIntValue(int value) 0107 { 0108 auto bool bRetVal; 0109 0110 // if the input value is less than zero, return "false", else return "true" 0111 if (value < 0) 0112 { 0113 bRetVal = false; 0114 } 0115 else 0116 { 0117 bRetVal = true; 0118 } 0119 0120 return (bRetVal); 0121 0122 } // end of "TestIntValue" 0123 0124