CIT-120 Week 3 Notes – Output Manipulators and Character Arrays
This week was all about output manipulators, character arrays, and an introduction to logic. We didn’t do a whole lot of review this week but Professor Delta did ask us what we thought about the logic games we did last week. Because most people responded positively to the games, she ended up giving us a homework related to another logic-based game.
Homework
Part I: In ours group, we’re supposed to work together to write out instructions on how to play a game called chain reaction. apparently, the instructions provided are poorly written and this is a good opportunity for future developers to strengthen their writing skills.
Part II: The second piece of homework is to write a program that imitates a program provided by Professor Delta. It basically asks for the price and quantity of five products and outputs a bunch of information in a properly formatted table.
Notes
Output Manipulators
Output manipulators do just what they sound like they do: they manipulate the output to the screen. To use output manipulators, you must include the header
- fixed – explicitly makes large numbers NOT display in scientific notation
- showpoint – shows points in whole numbers (2->2.0)
- left/right – fix the output to the left or right
- setprecision – specifies how many decimal points to show on floats/doubles
There are basically two ways to set some of these output flags: the old way and the new way. The newstyle is used when using namespace.
So, here are some examples:
cout.precision(2); //Old style of setting precision //Flags (all old style) cout.setf(ios:fixed); cout.setf(ios::left); cout.setf(ios::showpoint); cout << setprecision(2) << x << setprecision(3) << y;
setprecision is persistent which means that once you set it, it will be set that way for the rest of the program, unless you overwrite it. Remember: setprecision works for a double only or floats. It won't cause an int to do .00 unless you typecast the int to a double first.
another example:
int x=5; cout << setprecision(2) << x; //Displays 5 cout << double(x); // 5.00, double only works for this instance only cout << x; //5
Character Arrays
We know that to declare single character, we just need to do: char a;
But what if we want to store multiple characters, like a string of text? For that, we'll need a character array.
Example:
char NAME[20];
In this example, a 20 character array is setup. This will hold 19 characters and a null character that signifies the end of the array in memory.
To access a single cell in memory, you can do so within the output like so:
cout << NAME[2]; //Outputs the 2nd character in the array
A common practice is to capitalize the name of the character array. It isn't required, but it is good practice.
Introduction to Logic
Finally, we were introduced to logic. I'll talk more about this next week, but basically, we learned IF statements. Not new to me, but here's example in C++.
int a,b; if (a > b) cout << a is bigger; else cout << b is bigger;
notice that there are no curly brackets. those are only required when there is more than one line in the true or false section.
int a,b;
if (a > b) {
cout << a is bigger;
cout << a is better;
} else {
cout << b is bigger;
cout << b is better;
}
more on logic next week.
Enjoy this Post?
Spread the word by promoting this post on FaceBook and Twitter.
