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

  • Home
  • SEARCH
  • 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 8872931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:20:22+00:00 2026-06-14T18:20:22+00:00

I am writing simple program for solving crosswords. I am using getline to load

  • 0

I am writing simple program for solving crosswords. I am using getline to load crossword from input, but I am getting weird “Segmenation fault” messages and I really dont know what to do with it. Here is the code:

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;


int** allocateCross(int rows, int columns)
{
    int ** cross = new int*[rows];
    for(int i=0;i<rows;i++)
    {
        cross[i] = new int[columns];    
    }
    return cross;
}
void changeValue(int **cross, int rowPosition, int columnPosition, int value)
{
    cross[rowPosition][columnPosition] = value;
}
void printCross(int ** cross, int rows, int columns)
{
    for(int i=0;i<rows;i++){
        for(int j=0;j<columns;j++){
            cout << cross[i][j];
        }
        cout << endl;
    }

}
void setCross(int ** cross, int rows,int columns, int spaces[], int numberOfSpaces)
{
        for(int i=0;i<6;i++){
        for(int j=0;j<6;j++){
            changeValue(cross, i, j, 0);

        }
        for(int i=0;i<(numberOfSpaces*2);i=i+2)
        {
            changeValue(cross, spaces[i], spaces[i+1],1);
        }

    }
}
int testString(string stringToTest, const string allowedChars)
{
    size_t found = stringToTest.find_first_not_of(allowedChars);
    if(found != string::npos)
        return false;
    else
        return true;
}

void wrongInput()
{
    cout << "Wrong input." << endl;
    exit(0);
}
int main()
{
    string line;
    string top;
    string line_r;
    bool stringTester;
    int columns = 0;
    int rows = 0;
    getline(cin,line);
    if(line!="Enter crossword:")
        wrongInput();
    getline(cin,line);
    stringTester = testString(line,"+-");
    if(stringTester != true)
        wrongInput();
    top = line;
    top.erase(top.begin());
    top.erase(top.begin()+(top.size()-1));
    rows = top.size();
    top = line;
    getline(cin,line);
    while(line != top)
    {
        columns++;
        getline(cin,line);
    }


    int ** cross = allocateCross(rows,columns);
    //int spaces[]={};
    setCross(cross,rows,columns,spaces,0);
    printCross(cross,rows,columns);


    return 0;
}

The only important part is the while cycle. When i enter this input:

Enter crossword:
+----+
|  * |
|    |
| *  |
+----+

Everything is fine until i enter the second +—-+ where the script shoud stop, then i get segmenation fault. Can someone help me please?

  • 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-06-14T18:20:23+00:00Added an answer on June 14, 2026 at 6:20 pm

    First of all, read the comments written.

    The problem seems here:

    for(int i=0;i<6;i++){
        for(int j=0;j<6;j++){
            changeValue(cross, i, j, 0);
        }
    }
    

    Why this value (6) is hardcoded? Why 6 and not 42? You crossword in the example has three lines and four columns (3×4), but the cycle goes through 6×6 matrix.

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

Sidebar

Related Questions

I'm writing a simple program using SDL, but I'm running into a strange problem.
I am writing a simple Java program to send mail, but am getting errors.
I am writing a simple shell program that must take input from a file
I am writing a simple program to filter out stock quotes from a given
I am writing simple parallel program in C++ using OpenMP. I am working on
I'm writing a simple program to call a Java function from my C program.
I'm writing a simple program that reads and processes file content using a BufferedReader
I'm writing a really simple program using GLUT and C in XCode 4.2. int
Writing a simple program that will find exact duplicate files on my computer, but
I'm writing a simple program for file encryption. Mostly as an academic exercise but

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.