2

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
phuclv
  • 37,963
  • 15
  • 156
  • 475
Rain Rise
  • 31
  • 1
  • 6

4 Answers4

4

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.

Community
  • 1
  • 1
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Inside for loop something like this:

std::cout << "[" << i << "]" << "-->" << letterGrades[i]; 
if (i == 3){ 
     std::cout << "\n"; 
}
O'Neil
  • 3,790
  • 4
  • 16
  • 30
sam
  • 335
  • 2
  • 5
  • 19
0

its pretty easy actually

for (int i = 0; i < 25; i++)
{
 cout << "[" << i << "] --> " << letterGrades[i] << (i%3==0)?endl:""; 
}

Let me explain the code

  1. cout takes bitwise operations to pass in a stream. What that basically means is that you need to use "<<" operator to tell it what to print.

  2. The last bit (i%3==0)?endl:"". If you're not familiar with the ? operator, what that does is if the condition that appears within () is true, it'll evaluate the part before :, if not then it'll evaluate the later

e.g.:-

print((1>0)?"Hello":"World") \\ Output: Hello
print((false)?"Hello":"World") \\ Output: World
  1. % is also a neat operator we can use. It divides a number and returns the remainder.

e.g.:-

print(10%5) \\ Output: 0
print(5%2) \\ Output: 1

So I used that to see if there should be a line terminator endl ("\n") after the third column,

Hope this helps

xxfast
  • 697
  • 5
  • 14
  • Drop the first sentence and I'll upvote. By the release of the film "Interstellar", where the GCI guys used General Relativity equations to model the wormhole graphics, they found that easy. – Bathsheba Feb 25 '17 at 09:27
0

The easiest way to insert a newline (or std::endl) after each 3rd, 7th, 11th, etc, line is probably to have an int to count each step

int endl_count = 1;
for (int i = 0; i < 25; i++)
  {
    std::cout << '[' << i << ']' << " --> " << letterGrades[i];
    if (endl_count == 3) {
      endl_count = 0;
      std::cout << std::endl;
    }
    endl_count++;
  }

Also: to get the size of an array you can do

(sizeof(array) / sizeof(datatype))

So in your case you can do

for (int i = 0; i < (sizeof(letterGrades) / sizeof(char)); i++)

instead of hardcoding the 25

Lunatoid
  • 68
  • 9