C++ has two relatively simple, but perhaps interesting because of their daily usability, types: char (single character) and bool (true and false). Both are often used in conditions, in input, and simple on/off logic:
A char stores a single character, like a letter, number, or symbol.
#include <iostream>
int main()
{
char letter{'A'};
char digit{'7'};
char symbol{'#'};
std::cout << letter << '\n';
std::cout << digit << '\n';
std::cout << symbol << '\n';
}
Character literals are enclosed in single quotes. Double quotes denote a string, which is a different type, so most of the time will produce a compile error if used interchangeably.
Alternatively, you can also assign it a specific character from its numerical value, but most of the time this form is less clear than the single quote one.
#include <iostream>
int main()
{
char character{65};
std::cout << character << '\n';
}
A boolean can be either true or false:
#include <iostream>
int main()
{
bool is_open{true};
bool is_raining{false};
std::cout << is_open << '\n';
std::cout << is_raining << '\n';
}
Also, std::cout will print bool values as 1 (true) and 0 (false) by default. Once you get used to this it is useful, but it can be quite confusing at the beginning:
#include <iostream>
int main()
{
char grade{'B'};
bool passed{true};
std::cout << "Grade: " << grade << '\n';
std::cout << "Passed: " << passed << '\n';
if (passed)
{
std::cout << "Congratulations!\n";
}
}
A char can store a letter, a bool makes a simple choice. With good naming and initialization at definition, they all remain very readable.
Exercise: Characters and Boolean ValuesStarter code, hints and solutionOpenClose
Practice: Characters and Boolean Values
Work through the mission, reveal hints only when needed, and compare your result with the model solution.
Your challenge
Change grade from B to A without changing passed. Predict the character, Boolean output, and whether the congratulations branch runs.
Mission checklist
- Run the starter as a baseline, then predict the change requested in Your challenge.
- Make that change in your editor; use the lesson-specific hints if you need help.
- Compile and run your answer, then compare with the worked solution and explain the result.
Starter program
This is the working baseline, not the completed answer. Compile as C++20, then make the change described above.
#include <iostream>
int main()
{
char grade{'B'};
bool passed{true};
std::cout << "Grade: " << grade << '\n';
std::cout << "Passed: " << passed << '\n';
if (passed)
{
std::cout << "Congratulations!\n";
}
}Hints
grade holds one character in single quotes, while passed holds true or false. Without boolalpha, std::cout prints this Boolean as a number.
Leave passed unchanged. Edit only the single-quoted grade initializer; grade does not automatically determine whether the congratulations branch runs.
Worked solution
The text-like value in this starter is a single char, not a std::string. I changed grade from B to A. passed remains true, so it still prints as 1 and the congratulations branch still runs.
#include <iostream>
int main()
{
char grade{'A'};
bool passed{true};
std::cout << "Grade: " << grade << '\n';
std::cout << "Passed: " << passed << '\n';
if (passed)
{
std::cout << "Congratulations!\n";
}
}
Example output
Grade: A
Passed: 1
Congratulations!
0 comments
No comments yet. Be the first.