6.8
#include<iostream>
#include<cctype>
int main()
{
using namespace std;
cout << "Enter text for analysis, and type @"
" to terminate input.\n";
char ch;
int whitespace = 0;
int digits = 0;
int chars = 0;
int punct = 0;
int others = 0;
cin.get(ch);
while (ch != '@')
{
if (isalpha(ch))
chars++;
else if (isspace(ch))
whitespace++;
else if (isdigit(ch))
digits++;
else if (ispunct(ch))
punct++;
else
others++;
cin.get(ch);
}
cout << chars << " letters, "
<< whitespace << " whitespace, "
<< digits << " digits, "
<< punct << " punctuations, "
<< others << " others.\n";
system("pause");
return 0;
}
6.9
#include<iostream>
int main()
{
using namespace std;
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;
cout << "The larger of " << a << " and " << b;
int c = a > b ? a : b;
cout << " is " << c << endl;
system("pause");
return 0;
}
6.10
#include<iostream>
using namespace std;
void showmenu();
void report();
void comfort();
int main()
{
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";
system("pause");
return 0;
}
void showmenu()
{
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()
{
cout << "It's been an excellent week for business.\n"
"Sales are up 120%. Expenses are down 35%.\n";
}
void comfort()
{
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";
}
6.11
#include<iostream>
enum { red, orange, yellow, green, blue, violet, indigo };
int main()
{
using namespace std;
cout << "Enter color code (0-6): ";
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 seatsuit was blue.\n"; break;
case violet: cout << "Her eyes were violet,\n"; break;
case indigo: cout << "Her mood was indigo.\n"; break;
}
cout << "Enter color code (0-6): ";
cin >> code;
}
cout << "Bye\n";
system("pause");
return 0;
}