Lesson 05 Sample Programs Fall 2015 - Lesson 05 Sample Programs\/SP5_1.cpp\/Prompt user for an integer number\/0 terminates program Uses while loop#include

Lesson 05 Sample Programs Fall 2015 - Lesson 05 Sample...

This preview shows page 1 - 7 out of 29 pages.

Lesson 05 – Sample Programs //SP5_1.cpp //Prompt user for an integer number. //0 terminates program. // Uses while loop #include <iostream> #include <cmath> using namespace std; int main() { int originalNumber; int calculatedAnswer; cout << "Enter a number to double or 0 to end: " ; cin >> originalNumber; cout << endl; while (originalNumber != 0) { calculatedAnswer = pow(originalNumber, 2.0); cout << originalNumber << " doubled is " << calculatedAnswer << endl; cout << "Enter a number to double or 0 to end: " ; cin >> originalNumber; cout << endl; } return 0; } /* Enter a number to double or 0 to end: 4 4 doubled is 16 Enter a number to double or 0 to end: 6 6 doubled is 36 Enter a number to double or 0 to end: 8 8 doubled is 64 Enter a number to double or 0 to end: -3 -3 doubled is 9 Enter a number to double or 0 to end: 0 */ 1
Image of page 1
Lesson 05 – Sample Programs //SP5_2.cpp // Counter-controlled repetition using while loop. #include <iostream> using namespace std; int main() { int counter = 1; // declare and initialize control variable while ( counter <= 5 ) // loop-continuation condition { cout << counter << " " ; counter++; // increment control variable by 1 } // end while cout << endl; // output a newline return 0; // successful termination } // end main /* 1 2 3 4 5 */ 2
Image of page 2
Lesson 05 – Sample Programs //SP5_3.cpp // Counter-controlled repetition using while loop. // This is an improved version of SP5_2.cpp. The variable // counter is referenced three times here. Note that the increment operation // is shifted to the while argument. #include <iostream> using namespace std; int main() { int counter = 0; // declare and initialize control variable while ( ++counter <= 5 ) // loop-continuation condition { cout << counter << " " ; } // end while cout << endl; // output a newline return 0; // successful termination } // end main /* 1 2 3 4 5 */ 3
Image of page 3
Lesson 05 – Sample Programs //SP5_4.cpp // Read, echo, and count characters with a while loop. # terminates //data entry. #include <iostream> using namespace std; int main() { char ch; int count = 0; // use basic input cout << "Enter characters; enter # to quit: " ; cin >> ch; // get a character while (ch != '#' ) // test the character { cout << ch << ' ' ; // echo the character ++count; // count the character cin >> ch; // get the next character } cout << endl << count << " characters read\n" ; return 0; } /* Enter characters; enter # to quit: 123abcdu 1 2 3 a b c d u 98rt# 9 8 r t 12 characters read */ 4
Image of page 4
Lesson 05 – Sample Programs //SP5_5.cpp // Read, echo, and count characters with a while loop. # terminates //data entry. //Using cin.get(ch) function #include <iostream> using namespace std; int main() { char ch; int count = 0; cout << "Enter characters; enter # to quit: " ; cin.get(ch); // use the cin.get(ch) function while (ch != '#' ) { cout << ch; ++count; cin.get(ch); // use it again } cout << endl << count << " characters read\n" ; return 0; } /* Enter characters; enter # to quit: we 89 gh we 89 gh 12 12 fg# fg 14 characters read */ 5
Image of page 5
Lesson 05 – Sample Programs // SP5_6.cpp //if statement inside a while loop. //Program counts number of spaces and characters. Terminates on ' . ' #include <iostream> using namespace std; int main() { char ch; int spaces = 0; int total = 0; cout << "Enter a sentence a terminate with a . " ; cin.get(ch); while (ch != '.' ) // quit at end of sentence { if (ch == ' ' ) // check if ch is a space ++spaces; ++total; // done every time cin.get(ch); } cout << spaces << " spaces, " << total; cout << " characters total in sentence\n" ; return 0; } /* Enter a sentence a terminate with a . if statement inside a while loop.
Image of page 6
Image of page 7

You've reached the end of your free preview.

Want to read all 29 pages?

  • Fall '15
  • Marinolent
  • Negative and non-negative numbers, cout

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture