Loops and Arrays

Hello, I am learning C++ as next year I will start college classes and I want to have an advantage of already knowing the basics.

I have the following code

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
29
30
31
32
33
34

#include  <iostream>
#include <cstdlib>
#include <cstdio>
     using namespace std; // Shortens my code work by avoiding the use of std;
 int main () 
 
 {

     int myArray [3] [5] = 
     { 
         {0, 1, 2, 3, 4 } ,
         { 0, 2, 4, 6, 8 },
         { 0, 4, 6, 8, 10 }
         
     }; // Using a bidimensional array to create a 3 x 5 space;
     
    int i, j; // Declaring two integers;
     
     for ( int i = 0 ; i < 2; i++ ) // Loop starts here, I try to use i as the first dimension;
     
     {
         for ( int j = 0 ; int < 5; j++ ) // The second loop, uses j as the second dimension, 
                                          //This loop where the problem appears when I try to compile the file;
         
         {
             cout << myArray [i] [j] << endl; // Should display my results in the output of the console;
         
         }
             
     }
 return 0;
 }


I get an error saying int is not a template when I try to compile at the first for loop.
Any idea how to fix this?
I am trying to learn how to use arrays and in this exercise I'd have to make an output but even if I copy paste the text from the source I get the same error.
I use Dev-C++.
closed account (1vRz3TCk)
for ( int j = 0 ; int < 5; j++ )

try:
for ( int j = 0 ; j < 5; j++ )
Thanks a lot, it worked, I can't believe I didn't spot that, guess I'm a bit tired, weird thing though, I kept proof reading the code from the book, and I can't find anything wrong with it, if you wanna take a shot at it, here it is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
2: using namespace std;
3:
4: int main()
5: {
6: int SomeArray[2][5] = { {0,1,2,3,4}, {0,2,4,6,8}};
7: for (int i = 0; i<2; i++)
8: {
9: for (int j=0; j<5; j++)
10: {
11: cout << “SomeArray[“ << i << “][“ << j << “]: “;
12: cout << SomeArray[i][j]<< endl;
13: }
14: }
15: return 0;
16: }

In your first post, shouldn't for ( int i = 0 ; i < 2; i++ ) be for ( int i = 0 ; i < 3; i++ ), or do you just want the first two rows ?

Also, you shouldn't declare integers i and j twice.
closed account (1vRz3TCk)
zeustacos

This will be a hard one to explain
Line 11:
cout << “SomeArray[“ << i << “][“ << j << “]: “;
should be:
cout << "SomeArray[" << i << "][" << j << "]: ";

Look closely at the: "
Those aren't the right commas? I did wonder why the text in my compiler editor wasn't the right color when I tried to paste it to test, thank you very much CodeMonkey, I was really confused.

Wisely Done, yeah I'm still confused with the couting starting either from 0 or with 1 yet. Although that had nothing to do with my issues thanks for pointing it out, it actually clarified some more things for me.
Last edited on
Topic archived. No new replies allowed.