Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** ---------------------------------------------
- * This program demonstrates setw, setfill, and other
- * helpful concepts in C++ that students need to complete the
- * first project.
- * Class: CS 141, Fall 2023
- * System: ZyBook Lab *
- * @author Mark Hodges
- * @version August 23, 2023
- * ---------------------------------------------- */
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int main() {
- // Example output of an asterisk to the screen:
- cout << "*" << endl;
- // If we want an asterisk with space in front of it, one way is to use setw.
- // This will output 4 spaces and then the asterisk (so that there will be 5 characters displayed: 4 spaces and 1 asterisk)
- // We need to include iomanip at the two for setw to work
- cout << setw(5) << "*" << endl;
- // setw only impacts the thing displayed right after it--so this output will not have any spaces in front of it
- cout << "*" << endl;
- // To have something other than spaces to get to the correct width, use setfill.
- // The following will have four periods instead of four spaces.
- // Notice that the period is in single quotes to indicate that it's a single character, rather than a whole string
- cout << setfill('.') << setw(5) << "*" << endl;
- // setfill continues applying once it's been set. So, this will still have four periods, just like before
- cout << setw(5) << "*" << endl;
- // If we want to instead use spaces, we need to use setfill to switch back to spaces:
- cout << setfill(' ') << setw(5) << "*" << endl;
- // A backslash \ is a special character in C++. For example, it can be used to output a new line: \n or quotes: \"
- // If we want to display a backslash we actually need to write it twice: \\ to tell C++ we actually want a backslash and not something special
- cout << "Here is a backslash: \\" << endl;
- cout << "Here is a quote: \"" << endl;
- // You'll need to use loops to complete the project. Here is an example of a while loop to display the numbers 1 to 10:
- int counter = 1;
- while (counter <= 10) {
- cout << counter << endl;
- counter++; // Adds one to the counter variable
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement