Cant cout an array

Hi I've made the array cs1 to be equal to array cs.
But I cant get the output. The compiler tells that there is a problem at this line: cout << cs; So what is the problem? Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "stdafx.h"
#include <iostream>
#include <cstdlib> 
#include <cmath>
#include <array>
using namespace std;

int main()
{
	int j;
	array<double,4> cs1 = { 0.1, 0.1, 0.4, 0.2 };
	array<double, 4> cs;
	cin >> j;
	if (j == 1) {
		cs=cs1;
	};
	cout << cs;
	system("pause\n");
	return 0;
}
Hello Alex009988,

In the future when you have errors that you do not understand post the whole error message.

I do not need this:
Severity Code Description Project File Line Suppression State
Error
C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::array<double,4>' (or there is no acceptable conversion) Cant cout an array EP f:\vs 2017\cant cout an array ep\cant cout an array ep\main v1.cpp 23

It did not copy well, but the part in bold is what is important.

Even with out that I can see that you are trying to output the whole array with just the variable name and this will not work.

You will need a for loop to output each element of the array like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::cout << "\n Enter 1 to continue: ";
std::cin >> j;
	
if (j == 1)
	cs = cs1;
	
std::cout << '\n';

for (auto lc : cs)
	std::cout << lc << "  ";

std::cout << '\n' << std::endl;

system("pause");  // <--- The \n is not used with this command. 

The first time I ran the program the cursor just sat there blinking at me and I thought something was wrong because my focus was elsewhere in the program, so I added line 1. It really does help to have a prompt before input.

For a single line with an if statement, also for a while loop and for loop, the {}s are not needed and the semi-colon at the end of your line 16 does not seem to be a problem, but again it is not needed.

The for loop is what you need to print out each element of the array as the "cout" statement is not designed to do this.

Hope that helps,

Andy
Topic archived. No new replies allowed.