简体   繁体   中英

changing printf to cout

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.

Inside for loop something like this:

std::cout << "[" << i << "]" << "-->" << letterGrades[i]; 
if (i == 3){ 
     std::cout << "\n"; 
}

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

eg:-

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.

eg:-

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM