Help for beginner

When I make the program for this given task how do I print only non zero elements of array not the whole array and can anyone tell how to terminate if array is non zero. Sorry i am just a beginner !!

[code]
Put the code you need help with here.
Write a program in C++ to identify array in which no zero present, and print those numbers. If user input a value without zero program should terminate.
I print only non zero elements of array


Iterate the array and test if the element is non-zero and print only if this is true.

eg:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main() {
	const int myarray[] { 1, 2, 0, 4, 0, 5 };

	for (const auto e : myarray)
		if (e)
			std::cout << e << ' ';

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



1 2 4 5


to terminate if array is non zero


Terminate what? What do you mean by 'array is non-zero'???
By Terminate,I mean terminate the program. On that I will be using "IF " and Else If" to terminate the program when no zero in array is entered by user. Sorry that was typo i mean elements of Array,if no zero is present in elements entered by user of Array then terminate the program.

Shawnamous (2)
By Terminate,I mean terminate the program. On that I will be using "IF " and Else If" to terminate the program when no zero in array is entered by user. Sorry that was typo i mean elements of Array,if no zero is present in elements entered by user of Array then terminate the program.



track it as you read them in.
1
2
3
4
5
6
7
8
9

main
bool quitnz{true};
for(..)
 {
   read in numbers[i]
   quitnz &= numbers[i];   
}
if(quitnz) return;  //because its in main, the return quits the program cleanly.  add a print to tell them why?  


there is another account asking the same question in broken english. Hmm.
Last edited on
#include <iostream>
using namespace std ;
int main ( ) {
int n [ 5 ] ;
int y =1 ;
for ( int x = 0 ;
x < 5 ; x ++ ) {
cout << " Enter the << x << element >> n [ x ] ;
cin >>n[x];
if ( n [ x ] == 0 ) {
y==0;
break }
}
if ( y== 1 ) {
y = 0 ;
cout << " \ nElements of array are";
for ( int x = 0 ; x < 5 ; x ++ ) {
cout << n [ x ] << " , " ;
}
}
else if ( y == 0 ) {
cout <<" PROGRAM IS TERMINATED " << endl ;

}
}


Is this correct ?
Last edited on
When posting post, use code tags so that the code is readable!


[code]
// the code goes here
[/code]


There are several syntax errors. Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

int main() {
	int n[5] {};
	bool term {};

	for (int x = 0; !term && x < 5; ++x) {
		cout << "Enter the " << x << " element: ";
		cin >> n[x];

		if (n[x] == 0)
			term = true;
	}

	if (!term) {
		cout << "\nElements of array are\n";

		for (int x = 0; x < 5; ++x) {
			if (x != 0)
				cout << ", ";

			cout << n[x];
		}
	} else
		cout << "PROGRAM IS TERMINATED\n";
}


PS. The OP's code was modified between my first look and this post.
OK. Thank you very much buddy !
Topic archived. No new replies allowed.