Branching Statements and Logical Operators |
|---|
// if.cpp -- using the if statement
#include <iostream.h>
int main(void)
{
char ch;
int spaces = 0;
int total = 0;
while (cin.get(ch))
{
if (ch == ' ') // check if ch is a space
spaces++;
total++; // done every time
}
cout << spaces << " spaces, " << total;
cout << " characters total\n";
return 0;
}
// ifelse.cpp -- using the if else statement
#include <iostream.h>
int main(void)
{
char ch;
cout << "Type, and I shall repeat.\n";
while (cin.get(ch))
{
if (ch == '\n')
cout << ch; // done if newline
else
cout << ++ch; // done otherwise
}
// try ch + 1 instead of ++ch for interesting effect
cout << "Please excuse the slight confusion.\n";
return 0;
}
// ifelseif.cpp -- using if else if else
#include <iostream.h>
const int Fave = 27;
int main(void)
{
int n;
cout << "Enter a number in the range 1-100 to find ";
cout << "my favorite number: ";
do
{
cin >> n;
if (n < Fave)
cout << "Too low -- guess again: ";
else if (n > Fave)
cout << "Too high -- guess again: ";
else
cout << Fave << " is right!\n";
} while (n != Fave);
return 0;
}
// or.cpp -- use logical OR operator
#include <iostream.h>
int main(void)
{
cout << "This program may reformat your hard disk\n"
"and destroy all your data.\n"
"Do you wish to continue? ";
char ch;
cin >> ch;
if (ch == 'y' || ch == 'Y') // y or Y
cout << "You were warned!\a\a\n";
else if (ch == 'n' || ch == 'N')
cout << "A wise choice ... bye\n";
else
cout << "That wasn't a y or an n, so I guess I'll "
"trash your disk anyway.\n";
return 0;
}
// and.cpp -- use logical AND operator
#include <iostream.h>
const int ArSize = 6;
int main(void)
{
float iq[ArSize];
cout << "Enter the IQs of your in-laws. Program term"
"inates when you make\n" << ArSize << " entries ";
cout << "or enter a negative value.\n";
int i = 0;
float temp;
cin >> temp;
while (i < ArSize && temp >= 0) // 2 quitting criteria
{
iq[i++] = temp;
if (i < ArSize) // room left in the array,
cin >> temp; // so get next value
}
if (i == 0)
cout << "No data--bye\n";
else
{
cout << "Enter your IQ: ";
float you;
cin >> you;
int count = 0;
for (int j = 0; j < i; j++)
if (iq[j] > you)
count++;
cout << count;
cout << " of your in-laws are smarter than you.\n";
}
return 0;
}
// more_and.cpp -- use logical AND operator
#include <iostream.h>
const char * qualify[4] = // an array of pointers
{ // to strings
"10,000-meter race.\n",
"mud tug-of-war.\n",
"masters canoe jousting.\n",
"pie-throwing festival.\n"
};
int main(void)
{
int age;
cout << "Enter your age in years: ";
cin >> age;
int index;
if (age > 17 && age < 35)
index = 0;
else if (age >= 35 && age < 50)
index = 1;
else if (age >= 50 && age < 65)
index = 2;
else
index = 3;
cout << "You qualify for the " << qualify[index];
return 0;
}
// not.cpp -- using the not operator
#include <iostream.h>
#include <limits.h>
int is_int(double);
int main(void)
{
double num;
cout << "Yo, dude! Enter an integer value: ";
cin >> num;
while (!is_int(num)) // continue while num is not int-able
{
cout << "Out of range -- please try again: ";
cin >> num;
}
int val = num;
cout << "You've entered the integer " << val << "\n";
return 0;
}
int is_int(double x)
{
if (x <= INT_MAX && x >= INT_MIN) // use limits.h values
return 1;
else
return 0;
}
// ctypes.cpp--use ctype.h library
#include <iostream.h>
#include <ctype.h> // prototypes for character functions
int main(void)
{
cout << "Enter text for analysis, and simulate EOF"
" to terminate input.\n";
char ch;
int whitespace = 0;
int digits = 0;
int chars = 0;
int punct = 0;
int others = 0;
while(cin.get(ch))
{
if(isalpha(ch)) // is it an alphabetic character?
chars++;
else if(isspace(ch)) // is it a whitespace character?
whitespace++;
else if(isdigit(ch)) // is it a digit?
digits++;
else if(ispunct(ch)) // is it punctuation?
punct++;
else
others++;
}
cout << chars << " letters, "
<< whitespace << " whitespace, "
<< digits << " digits, "
<< punct << " punctuations, "
<< others << " others.\n";
return 0;
}
// condit.cpp -- using the conditional operator
#include <iostream.h>
int main(void)
{
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "The larger of " << a << " and " << b;
int c = a > b ? a : b; // c = a if a > b, else c = b
cout << " is " << c << "\n";
return 0;
}
// switch.cpp -- use the switch statement
#include <iostream.h>
void showmenu(void); // function prototypes
void report(void);
void comfort(void);
int main(void)
{
showmenu();
int choice;
cin >> choice;
while (choice != 5)
{
switch(choice)
{
case 1 : cout << "\a\n";
break;
case 2 : report();
break;
case 3 : cout << "The boss was in all day.\n";
break;
case 4 : comfort();
break;
default: cout << "That's not a choice.\n";
}
showmenu();
cin >> choice;
}
cout << "Bye!\n";
return 0;
}
void showmenu(void)
{
cout << "Please enter 1, 2, 3, 4, or 5:\n"
"1) alarm 2) report\n"
"3) alibi 4) comfort\n"
"5) quit\n";
}
void report(void)
{
cout << "It's been an excellent week for business.\n"
"Sales are up 120%. Expenses are down 35%.\n";
}
void comfort(void)
{
cout << "Your employees think you are the finest CEO\n"
"in the industry. The board of directors think\n"
"you are the finest CEO in the industry.\n";
}
// enum.cpp -- use enum
#include <iostream.h>
// create named constants for 0 - 6
enum {red, orange, yellow, green, blue, purple, indigo};
int main(void)
{
cout << "Enter color code: ";
int code;
cin >> code;
while (code >= red && code <= indigo)
{
switch (code)
{
case red : cout << "Her lips were red.\n"; break;
case orange : cout << "Her hair was orange.\n"; break;
case yellow : cout << "Her shoes were yellow.\n"; break;
case green : cout << "Her nails were green.\n"; break;
case blue : cout << "Her sweatsuit was blue.\n"; break;
case purple : cout << "Her eyes were purple.\n"; break;
case indigo : cout << "Her mood was indigo.\n"; break;
}
cout << "Enter color code: ";
cin >> code;
}
cout << "Bye\n";
return 0;
}
// jump.cpp -- using continue and break
#include <iostream.h>
const int ArSize = 80;
int main(void)
{
char line[ArSize];
int spaces = 0;
cout << "Enter a line of text:\n";
cin.get(line, ArSize);
for (int i = 0; line[i] != '\0'; i++)
{
cout << line[i]; // display character
if (line[i] == '.') // quit if it's a period
break;
if (line[i] != ' ') // skip rest of loop
continue;
spaces++;
}
cout << "\n" << spaces << " spaces\n";
return 0;
}
// cinfish.cpp -- non-numeric input terminates loop
#include <iostream.h>
const int Max = 5;
int main(void)
{
// get data
double fish[Max];
cout << "Please enter the weights of your fish.\n";
cout << "You may enter up to " << Max
<< " fish .\n";
cout << "fish #1: ";
int i = 0;
while (i < Max && cin >> fish[i]) {
if (++i < Max)
cout << "fish #" << i+1 << ": ";
}
// calculate average
double total = 0.0;
for (int j = 0; j < i; j++)
total += fish[j];
// report results
if (i == 0)
cout << "No fish\n";
else
cout << total / i << " = average weight of "
<< i << " fish\n";
return 0;
}
// cingolf.cpp -- non-numeric input skipped
#include <iostream.h>
const int Max = 5;
int main(void)
{
// get data
int golf[Max];
cout << "Please enter your golf scores.\n";
cout << "You must enter " << Max << " rounds.\n";
for (int i = 0; i < Max; i++)
{
cout << "round #" << i+1 << ": ";
while (!(cin >> golf[i])) {
cin.clear(); // reset input
while (cin.get() != '\n')
continue; // get rid of bad input
cout << "Please enter a number: ";
}
}
// calculate average
double total = 0.0;
for (i = 0; i < Max; i++)
total += golf[i];
// report results
cout << total / Max << " = average score "
<< Max << " rounds\n";
return 0;
}