printf("%d", b[i]);

C++ is a super-set of C. Which makes the code valid C++ as well.

If you want to use a strictly C++ construct, you'll have to use the <iostream> header instead of the <stdio.h> header (<cstdio> in c++) and use the cout object:

std::cout << b[i] << std::endl;

Edited 5 Years Ago by Fbody: n/a

std::cout<<int(b[i]);

It means output b as an integer. If b could not converse to int, it causes a compile-error.
In C language, if b is not a integer type, the output dose not make sense


std::cout<<b[i];

It outputs the b by its own type.

char c = 'A';
printf("%d", c); //Not A, treat c as an integer
cout<<int(c);   //Not A, treat c as an integer
cout<<c;       //It is A

It would be the same type of situation, but you would use the cin object instead:

std::cin >> n;

Also, notice the use of the '>>' operator instead of the '<<' operator.

You have to be cautious about this situation though. If 'n' is a numeric type (like in your C code) and the user inputs char type data (i.e. "hello", or 'a') you will wind up with a corrupted input stream. If that happens, read this.

There are other input operators/functions available which help avoid the situation (such as get()), but this is probably the most-direct translation of the posted code.

Edited 5 Years Ago by Fbody: n/a

This question has already been answered. Start a new discussion instead.