For loop doesn't display array, Help

Hello my name is Fred. I've tried a couple of times to learn c++ but I just keep giving up, and I don't want to do that again that's why I'm asking for help. I'd be glad if anyone can help me. So what I'm trying to do is to output every element in an array with a for loop but it doesn't show anything is it a typo?? Help! Thxs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <iostream>
using namespace std;


int cool(){
  
  int arr[5]={4,2,6,4,3};
  
  for(int a = 0; a==4; a++){
      cout << a << ":"<< arr[a]<< endl;
      return 0;
  }
 }
int main() {
    cool();
    return 0;
Your code has a lot of errors. The correct code for what you are trying to do could be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

void cool()
{
    int arr[5] = {4, 2, 6, 4, 3};
    
    for(int a = 0; a <= 4; a++)
    {
        cout << a << ":" << arr[a] << endl;
    }

int main()
{
    cool();
    return 0;
}


Now the problems with your code:
1. You set return 0; in the body of the for loop.
2. The test condition for the for loop is not logical. You will not get a compilation error because of it but your test condition will always come out to be false.
3. You didn't close your int main() function block with parenthesis }.
Hey Fred and welcome to the forum.

Every function ends the moment it hits a return statement. Therefore, I imagine your code only prints out the very first element of the array before it terminates. To fix your problem, simply remove the return 0; statement that is currently contained inside of the for-loop.

Also, to keep your CMD open during code execution, add the following right under Line 15:
1
2
int temp;
cin >> temp; 


EDIT: Oh, I just spotted another mistake in your loop. So, the for loop lets you repeat a section of code multiple times (depending on the condition), and it is particularly suited for counting and moving through a sequence of things (like arrays). Your loop initialization and action are valid. However, your condition is incorrect.

Last edited on
Omg, this so much for your anwser, im new to this so I did make stupid errors, but THXS! Cya
closed account (48bpfSEw)
2 errors found.

a == 4 is false if a = 1 -> left loop
why return 0 in the for loop?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include <iostream>
using namespace std;


int cool(){
  
  int arr[5]={4,2,6,4,3};
  
  for(int a = 0; a<=4; a++){
      cout << a << ":"<< arr[a]<< endl;
      // return 0;
  }
 }

int main() {
    cool();
    return 0;
}
I was trying to make the loop repeat until the array was fully displayed Necip, Thxs for your answer though :) UK marine I didn't mean to put the return 0 in the for loop I meant it to be at the end of the func. Cool but I didnt know it would stop the loop but this for your answer as well :)
Hi,

The usual for loop idiom:

for(int a = 0; a < 5; a++)

It is convenient to know how big the array is so make it a const variable:

1
2
3
4
const  int Size = 5;
int arr[Size] = {4,2,6,4,3};
// ......
for (int a = 0; a < Size; ++a)


So this avoids having magic numbers like 5 throughout the code.

Investigate using std::vector rather than arrays.
http://www.cplusplus.com/reference/vector/vector/vector/

Avoid using namespace std; - it defeats the purpose of namespaces, just put std:: before each std thing, that is what all the experienced coders do. Might as well get ahead of the game now, before that particular bad practise causes you a problem - and it will one day :+)

Good Luck !!
Last edited on
Topic archived. No new replies allowed.