Control structure: if / else

This example gives a 20% tip if the value of service is 'Y' or 'y'. When the user enters an answer as a character, it is important to test for both upper case and lower case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Tip based on service
#include <iostream>
using namespace std;
int main(void) {
  double cost, tip;
  char service;
  cout<<"Enter cost of meal:$";
  cin>>cost;
  cout<<"Was the service good (Y/N)?";
  cin>>service;
  if(service=='Y' || service=='y')
    tip=cost*0.20; //20% for good service
  else 
    tip=cost*0.15; //15% if not
  cout<<"The tip is $"<<tip<<"\n";
  system("pause");
} //main
CODE
Enter cost of meal:$20
Was the service good (Y/N)?Y
The tip is $4
Press any key to continue . . .
flowchart

NEXT: Find a letter grade