Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7536843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:41:13+00:00 2026-05-30T06:41:13+00:00

For some reason my program is crashing in the loop, and im unsure what

  • 0

For some reason my program is crashing in the loop, and im unsure what is causing the problem

Here is my code.

#include<iostream>
#include<string>
#include<fstream>
using namespace std;



int main()
{
    ifstream fin;
    ofstream fout;

    fin.open("dice.in");
    fout.open("dice.out");

    string line;
    int boardsize;
    int top;
    int side;

    fin >> boardsize >> top >> side;
    while(boardsize != 0)
    {
        int ** board;
        board = new int*[boardsize];

        //creates  a multi dimensional dynamic array
        for(int i = 0; i < boardsize; i++)
        {
            board[i] = new int[boardsize];
        }

        //loop through the 2d array
        for(int i = 0; i <= boardsize; i++)
        {
            getline(fin, line);
            for(int j = 0; j < boardsize; j++)
            {
                if(i != 0)
                board[i][j] = (int)line[j];
            }
            line.clear();
        }


        fin >> boardsize >> top >> side;
    }

    fin.close();
    fout.close();
}

Here is the input file i am using

3 6 4
632
562
463
3 6 4
632
556
423
7 6 4
4156*64
624*112
23632**
4555621
6*42313
4231*4*
6154562
0 0 0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:41:15+00:00Added an answer on May 30, 2026 at 6:41 am
        //loop through the 2d array
        for(int i = 0; i <= boardsize; i++)
        {
            getline(fin, line);
            for(int j = 0; j < boardsize; j++)
            {
                if(i != 0)
                board[i][j] = (int)line[j];
            }
            line.clear();
        }
    

    needs to be

        //loop through the 2d array
        for(int i = 0; i < boardsize; i++)         // note < instead of <=
        {
            getline(fin, line);
            for(int j = 0; j < boardsize; j++)
            {                                      // not I deleted if statement
                board[i][j] = (int)line[j];
            }
            line.clear();
        }
    

    This is because arrays in C++ start at the index 0, so when you allocate an array of size three, as you do above with board = new int*[boardsize];, the indices of this array will be 0, 1, 2, ... boardsize-1, whereas your algorithm was using 1, 2, 3, ... boardsize which will be an out of bounds access because you only have n blocks allocated and you are trying to access (modify, actually) block n + 1, which will result in a segmentation fault.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason even though I have declared a string in Program.cs static and
For some reason, using SearchText := 'Program Files'; ReplaceText := 'Program Files (x86)'; SearchAndReplace(SearchText,
For some reason I'm having trouble getting my program to run a while loop.
For some reason when I run the following code the program prints out JDN4
for some reason when running a program it refuses to enter a for loop,
For some reason my Heapsort is not working correctly. Using the following test program:
For some reason this program is saying that 'switch' is not defined. What is
For some reason the following doesn't crash like my program does, but I'm pretty
I'm writing a program in C++ and for some reason I'm getting a segmentation
In my program, I'm grep-ing via NSTask. For some reason, sometimes I would get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.