cs31day9 Wk 4 W

# cs31day9 Wk 4 W - Boolean expressions expressions that...

This preview shows pages 1–3. Sign up to view the full content.

Boolean expressions - expressions that evaluate to either true or false == <= etc. use bool to store true or false values bool canDrink = (age >=21); // either true or false C++ defines two constants true and false, which you can directly assign to a bool bool aceMidtermm = true; bool flunkTheCourse = false; _____________ int age; cout << "Enter your age: "; cin >> age; bool canDrink = (age >=21); if ( canDrink ) cout << "You can drink!: << endl; else cout << "Stay away!" << endl; ______________ int sum (int begin, int end) ( int temp = 0; for (int i = begin; i <=end; i++) ( temp +=i; ) return temp; ) this computes the sum of numbers begin and end _________________ previous example from day 8 cout << "Enter a phone number: "; string t; getline(cin, t); int number Of Digits = 0; for (int k = 0; k != t.size(); k++) {

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
if (isdigit(t.at(k)) numberOfDigits++; { if (numberOfDigits != 10) cout << "A phone number must have 10 digits." << endl; ___________________ change to bool bool isValidPhoneNumber(string pn) ( int number Of Digits = 0; for (int k = 0; k != t.size(); k++) { if (isdigit(t.at(k)) numberOfDigits++; } if (numberOfDigits == 10) return true; else return false; ) ____________________ simplify the last return part bool isValidPhoneNumber(string pn) ( int number Of Digits = 0; for (int k = 0; k != t.size(); k++) { if (isdigit(t.at(k)) numberOfDigits++; } if (numberOfDigits == 10) return numberOfDigits == 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 7

cs31day9 Wk 4 W - Boolean expressions expressions that...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online