Advertisement
TheLegend12

Untitled

Sep 2nd, 2023
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. /** ---------------------------------------------
  2. * This program demonstrates setw, setfill, and other
  3. * helpful concepts in C++ that students need to complete the
  4. * first project.
  5. * Class: CS 141, Fall 2023
  6. * System: ZyBook Lab *
  7. * @author Mark Hodges
  8. * @version August 23, 2023
  9. * ---------------------------------------------- */
  10.  
  11. #include <iostream>
  12. #include <iomanip>
  13.  
  14. using namespace std;
  15.  
  16. int main() {
  17. // Example output of an asterisk to the screen:
  18. cout << "*" << endl;
  19.  
  20. // If we want an asterisk with space in front of it, one way is to use setw.
  21. // This will output 4 spaces and then the asterisk (so that there will be 5 characters displayed: 4 spaces and 1 asterisk)
  22. // We need to include iomanip at the two for setw to work
  23. cout << setw(5) << "*" << endl;
  24.  
  25. // setw only impacts the thing displayed right after it--so this output will not have any spaces in front of it
  26. cout << "*" << endl;
  27.  
  28. // To have something other than spaces to get to the correct width, use setfill.
  29. // The following will have four periods instead of four spaces.
  30. // Notice that the period is in single quotes to indicate that it's a single character, rather than a whole string
  31. cout << setfill('.') << setw(5) << "*" << endl;
  32.  
  33. // setfill continues applying once it's been set. So, this will still have four periods, just like before
  34. cout << setw(5) << "*" << endl;
  35. // If we want to instead use spaces, we need to use setfill to switch back to spaces:
  36. cout << setfill(' ') << setw(5) << "*" << endl;
  37.  
  38. // A backslash \ is a special character in C++. For example, it can be used to output a new line: \n or quotes: \"
  39. // 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
  40. cout << "Here is a backslash: \\" << endl;
  41. cout << "Here is a quote: \"" << endl;
  42.  
  43. // 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:
  44. int counter = 1;
  45. while (counter <= 10) {
  46. cout << counter << endl;
  47. counter++; // Adds one to the counter variable
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement