Unhandled exception in 0x7A47E727

1
#include "pch.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{

    //system("cls");
    const int x = 30;
    const int y = 15;
    string tabella[y][x];
    char bordo = '#';
    for (int i = 0; i < x; i++)
        tabella[0][i] = bordo;
    for (int i = 0; i < y; i++)
        tabella[i][0] = bordo;
    for (int i = 0; i < x; i++)
    {
        for (int j = 0; j < y; j++)
        {
            std::cout << tabella[i][j];
        }
        std::cout << "\n";
    }

}

I don't why it gaves me this problem:

Eccezione non gestita in 0x7A47E727 (ucrtbased.dll) in Prova1.exe: 0xC0000005: violazione di accesso durante la lettura del percorso 0xCCCCCCCC.

Here it is translated in English:

Unhandled exception in 0x7A47E727 (ucrtbased.dll) in Test1.exe: 0xC0000005: access violation while reading path 0xCCCCCCCC.

The problem seems to be in this line: std::cout << tabella[i][j];

I don't know but it started when I used the x e y variables. I'm using Visual Studio 2017 btw.

c++
visual-studio
unhandled-exception
asked on Stack Overflow Jun 16, 2020 by Antonio Riverso • edited Jun 16, 2020 by kaylum

2 Answers

2

Look at your array bounds. You have i and j the wrong way round.

std::cout << tabella[i][j];

should be

std::cout << tabella[j][i];

Or maybe you have x and y the wrong way round in the first place.

answered on Stack Overflow Jun 16, 2020 by john • edited Jun 16, 2020 by john
1

You declare the array

const int x = 30;
const int y = 15;
string tabella[y][x];

but here you use the indices wrong:

std::cout << tabella[i][j];

because i counts from 0 to 29 and j counts from 0 to 14.

So you have to use:

std::cout << tabella[j][i];

This will solve your problem

answered on Stack Overflow Jun 16, 2020 by Ctx

User contributions licensed under CC BY-SA 3.0