Joker
Joker

Reputation: 19

Cout an array on 1 line in C++

I'm not sure if it's possible since I didn't really find anything, but I have a linear array called "Grades" and I'm trying to output it all in one line.

int Grades[5] = { 3, 2, 5, 2 };
cout << "Grades:" << Grades << "\n";

I know the above doesn't work, but I want something like:

Grades: 3, 2, 5, 2

(minus the formatting/commas of course)

I know you can loop through it, but it just ends up printing it on a new line which I don't want.

Upvotes: 0

Views: 13276

Answers (4)

Richardson Ansong
Richardson Ansong

Reputation: 790

int Grades[5] = { 3, 2, 5, 2 };

cout << "Grades: ";
for (int i = 0; i < sizeof(Grades)/sizeof(int); i++) {
    cout << Grades[i] << ", "; //minus the commas, remove (<< ", ") or to space out the grades, just remove the comma
}

Or

Based on juanchopanza's suggestion, you can do it this way;

int Grades[] = { 3, 2, 5, 2 };

cout << "Grades: ";

for (auto g : Grades) {
    cout << g << ", "; //minus the commas, remove (<< ", ") or to space out the grades, just remove the comma

}

Upvotes: 2

rahnema1
rahnema1

Reputation: 15837

You can define an overload for operator<< that takes a pointer to array:

#include <iostream>

template <typename T, int i>
std::ostream& operator<<(std::ostream& os, T (*arr)[i] )
{
    for (auto e: *arr)
        os << e << ",";
   os << std::endl;
    return os;
}
int main()
{
    int Grades[5] = {2,34,4,5,6};
    std::cout<<& Grades;
}

Upvotes: 1

Pravar Jawalekar
Pravar Jawalekar

Reputation: 605

If you have latest compiler supporting C++11/C++14 then You can use range-based for loop,

#include <iostream>

using namespace std;

int main()
{
  int Grades[5] = { 3, 2, 5, 2, 1 };

  bool bFirst = true;

  for (int & i : Grades)
  {
      std::cout << (bFirst ? "Grades: " : ", ") << i;
      bFirst = false;
  }

  return 0;
}

It shows output like,

Grades: 3, 2, 5, 2, 1

Upvotes: 1

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

You can cout a container on a single line by using std::ostream_iterator and std::copy. Something like this will do the task you need:

#include <iostream>     // std::cout
#include <iterator>     // std::ostream_iterator
#include <vector>       // std::vector
#include <algorithm>    // std::copy

int main () {
  int Grades[5] = { 3, 2, 5, 2 };

  std::copy ( Grades, Grades + 4, std::ostream_iterator<int>(std::cout, ", ") );
  return 0;
}

This is the example from std::ostream_iterator's documentation adapted a bit to fit the particular example. Also see the result on ideone.

(Contributed by Rerito) If you are using c++11 or later you can also make use of std::begin and std::end from <iterator> to make the code cleaner:

#include <iostream>     // std::cout
#include <iterator>     // std::ostream_iterator
#include <vector>       // std::vector
#include <algorithm>    // std::copy
#include <iterator>

int main () {
  int Grades[5] = { 3, 2, 5, 2 };

  std::copy (std::begin(Grades), std::end(Grades), std::ostream_iterator<int>(std::cout, ", ") );
  return 0;
}

And the result on ideone.

Upvotes: 4

Related Questions