6.4
#include<iostream>
int main()
{
using namespace std;
cout << "This program may reformat your hard disk\n"
"and destroy all your data.\n"
"Do you wish to continue?<y/n>";
char ch;
cin >> ch;
if (ch == 'y' || ch == 'Y')
cout << "You were warned!\a\a\n";
else if (ch == 'n' || ch == 'N')
cout << "A wise choise...bye\n";
else
cout << "That wasn't a y or n! Apparently you "
"can't follow\ninstrucions, so "
"I'll trash your disk anyway.\a\a\a\n";
system("pause");
return 0;
}
6.5
#include<iostream>
const int ArSize = 6;
int main()
{
using namespace std;
float naaq[ArSize];
cout << "Enter the NAAQs (New Age Awareness Quotients) "
<< "of\nyour neighbors. Program terminates "
<< "when you make\n" << ArSize << " entries "
<< " or enter a negative value.\n";
int i = 0;
float temp;
cout << "First value: ";
cin >> temp;
while (i < ArSize && temp >= 0)
{
naaq[i] = temp;
++i;
if (i < ArSize)
{
cout << "Next Value: ";
cin >> temp;
}
}
if (i == 0)
cout << "No data--bye\n";
else
{
cout << "Enter your NAAQ: ";
float you;
cin >> you;
int count = 0;
for (int j = 0; j < i; j++)
if (naaq[j] > you)
++count;
cout << count;
cout << " of your neighbours have greater awareness of\n"
<< "the New Age than you do.\n";
}
system("pause");
return 0;
}
6.6
#include<iostream>
const char*qualify[4] =
{
"10,000-meter race.\n",
"mud tug-of-war.\n",
"masters canoe jousting.\n",
"pie-throwing festival.\n"
};
int main()
{
using namespace std;
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];
system("pause");
return 0;
}
6.7
#include<iostream>
#include<climits>
bool is_int(double);
int main()
{
using namespace std;
double num;
cout << "Yo, dude! Enter an integer value: ";
cin >> num;
while (!is_int(num))
{
cout << "Out of range -- please try again: ";
cin >> num;
}
int val = int(num);
cout << "You've entered the integer " << val << "\nBue\n";
system("pause");
return 0;
}
bool is_int(double x)
{
if (x <= INT_MAX && x >= INT_MIN)
return true;
else
return false;
}