I am still a bit unfamiliar with C++ and need some help with using cout.
int main()
{
char letterGrades[25] = { 'a', 'b', 'c', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', };
for (int i = 0; i < 25; i++)
{
printf("[%d] --> %c", i, letterGrades[i]);
if (i == 3) // how can I print \n when i == 7 , 11 , 15 , 19....
{
printf("\n");
}
}
}
This is what I am trying to do, and it works perfectly fine. However, I don't know how to write this code using cout.
Also, I would print the result in a 4 in a row tabulate format. so result can look something like this
[0] --> A [1] --> A [2] --> A [3] --> A
[4] --> A [5] --> A [6] --> A [7] --> A
[8] --> A [9] --> A [10] --> A [11] --> A
The class of which cout is an instance has clever overloads to << for many types, including char, int, and const char[] (for string literals):
So you can write
std::cout << "[" << i << "] --> " << letterGrades[i];
in place of your first printf and
std::cout << "\n";
for the second one. You can use "\t" to inject a tabulation character into the stream.
All this comes at a slight performance hit, which ought to be negligible cf. the I/O on your platform.
Also, consider reading C++: "std::endl" vs "\n" for further study.
Finally, use
if (i % 4 == 3){
// i is 3, 7, 11, 15, 19, etc
}
for your conditional check for the periodic newlines. % is the remainder operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With