C++ Array's last element ends up with ";" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

C++ Array's last element ends up with ";"

Should I ask people about code somewhere else? or this is right place? But still, could anyone help with my code. Point: After the array outputs, I want that the last element will end with ";" There's the example what I meant: #include <iostream> using namespace std; int main() { const int SizeOne = 5; int Nambers[SizeOne] = {2,4,6,8,10}; cout << "Numbers of array = "; for (int Three = 0; Three < SizeOne; ++Three) cout << Nambers[Three] << ", "; cout << endl; return 0; } Output: Numbers of array = 2, 4, 6, 8, 10,

25th Oct 2018, 5:36 PM
gotta feel the vibe
gotta feel the vibe - avatar
3 Answers
+ 3
You could extract the last loop iteration. Like this: int i; for(i=0; i<size-1; i++){ cout<<array[i]<<", "; } cout<<array[i]<<";"<<endl; or array[size-1], if you don't wan't the i to be outside the loop. You could also use an if condition inside the loop, but this is inefficient
25th Oct 2018, 6:14 PM
Matthias
Matthias - avatar
+ 2
you are right, silly mistake 😂
26th Oct 2018, 5:04 AM
Matthias
Matthias - avatar
+ 1
Matthias the last iteration of the loop already incremented i, so array[++i] is accessing out of bounds. Otherwise, you put it nicely.
26th Oct 2018, 12:11 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar