lesson 9 get yn cpp
[[lesson_9_get_yn_cpp]] last edit on
Aug 10, 2005
3:45 AM
by rescued_by_c
[user02@FC4 Lesson09]$ cat -n get_yn.cpp
[user02@FC4 Lesson09]$ ./get_yn
Type Y or N and press Enter to continue: y
The letter you typed was y
[user02@FC4 Lesson09]$ ./get_yn
Type Y or N and press Enter to continue: a
Type Y or N and press Enter to continue: No
The letter you typed was N
[user02@FC4 Lesson09]$
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int done = 0; // Set to true when Y or N is encountered
7 char letter;
8
9 while (! done)
10 {
11 cout << "\nType Y or N and press Enter to continue: ";
12 cin >> letter;
13
14 if ((letter == 'Y') || (letter == 'y'))
15 done = 1;
16 else if ((letter == 'N') || (letter == 'n'))
17 done = 1;
18 else
19 cout << '\a'; // Sound the speakers bell for
20 // invalid character
21 }
22 cout << "The letter you typed was " << letter << endl;
23
24 return 0;
25 }
[user02@FC4 Lesson09]$ g++ -o get_yn get_yn.cpp[user02@FC4 Lesson09]$ ./get_yn
Type Y or N and press Enter to continue: y
The letter you typed was y
[user02@FC4 Lesson09]$ ./get_yn
Type Y or N and press Enter to continue: a
Type Y or N and press Enter to continue: No
The letter you typed was N
[user02@FC4 Lesson09]$