Chapter 6 brings us to branching statements, also know as if and else statements. We are also introduced to the cctype library which makes light work of some basic character operations. Exercise 1 ask us to accept keyboard input to @, not to display numbers, convert uppercase to lowercase and vice versa. One other thing you will notice this time is I have not pulled in the entire standard library, I except use it in a case by case basis. This can help solve non-obvious ambiguity errors in our later problems. See my source below for a simple solution:
1. Write a program that reads keyboard input to the @ symbol and that echoes the input
except for digits, converting each uppercase character to lowercase, and vice versa.
(Don’t forget the cctype family.)
#include <iostream> #include <cctype> int main() { char ch; std::cout << "Enter your characters: "; while(std::cin.get(ch) && ch != '@') // While inputting and not an '@' { if(isdigit(ch)) continue; // Ignore digits if(isalpha(ch)) // Is it in the alphabet? if(islower(ch)) std::cout << char(toupper(ch)); else std::cout << char(tolower(ch)); else std::cout << ch; } return 0; }