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;
}

Dani AI

Generated

@/soche123: @/mike_2000_17 correctly points to the scoping root of that GCC message (the loop-local j isn't visible where it's used). Additional issues worth fixing so the program is both correct and portable:

  • Loop bounds: iterate with < (0..n-1), not <=, to avoid out-of-range indexing.
  • Storage: int record[players][years] relies on a compiler extension (variable-length arrays). Prefer std::vector for dynamic sizes.
  • Strings and escapes: sequences like \Year3 are malformed; use \t for tabs or write literals without stray backslashes.
  • Portability: conio.h/getch() are nonstandard; use standard input/output and std::cin.get() if a pause is needed.
  • Input validation: verify players and years are positive before allocating.

A compact, modern pattern is a 2D vector with strict bounds and input checks:

#include <iostream>
#include <vector>

int main() {
    int players = 3, years = 3;
    std::vector<std::vector<int>> record(players, std::vector<int>(years));

    for (int i = 0; i < players; ++i)
        for (int j = 0; j < years; ++j) {
            std::cout << "Player " << (i+1) << ", Year " << (j+1) << ": ";
            if (!(std::cin >> record[i][j])) return 0;
        }

    // print or process record safely...
}

As @/rubberman advised, always use braces for loops and conditionals to prevent scope and maintenance errors when adding lines later. Validate inputs and prefer standard containers for safer, clearer code.

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.