I don't get what this error mean in line 17 or 18..

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int players;
     int years=3;
    cout<<"Enter no. of players";
    cin>>players;

    int record[players][years];
    for(int i=0; i<=players; i++)
    {
        for(int j=0; j<=years; j++)
        cout<<"Enter runs scored by player"<<":"<<i+1<<": ";
        cin>>record[i][j];


    }
    cout<<"\n\tYear1\tYear2\Year3";
    cout<<"\n\tRuns\tRuns\Runs";
    cout<<"\n*************************************"<<endl;
    for(int i=0; i<=players; i++)
    {
        cout<<"\nPlayer"<<i+1<<"\t"<<endl;
        for(int j=0; j<=years; j++)
        cout<<record[i][j]<<"\t";
        cout<<endl;
    }

    getch();
    return 0;
}

Recommended Answers

All 2 Replies

You are missing the curly braces in this for loop:

    for(int j=0; j<=years; j++)
    cout<<"Enter runs scored by player"<<":"<<i+1<<": ";
    cin>>record[i][j];

You'll see it better if you space it out and indent properly:

    for(int j = 0; j <= years; j++)
        cout << "Enter runs scored by player" << ":" << i+1 << ": ";

    cin >> record[i][j];

As you see, the cin >> record[i][j]; is outside the scope of the for-loop. The error message is telling you that the variable j is undefined at that point. The reason the error message is weird is because it use to be (before the ISO standard) that the variable declared in the for-loop would exist in the scope enclosing the for-loop (e.g., holding the value it had at the last iteration), this rule has changed and is no longer allowed.

To fix the error, you need this:

    for(int j = 0; j <= years; j++) {
        cout << "Enter runs scored by player" << ":" << i+1 << ": ";
        cin >> record[i][j];
    };

Mike2K points out a common coding issue - proper scoping of loops. My personal process says "scope everything" - loops, conditionals (if/then/else), etc. IE, place proper brackets around all loops and conditional blocks of code. Then, when you need to add a line to them, they will still be properly constrained! :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.