C++ OOP segmentation fault

I have a task at the college. I'm making a chess board with chess pieces on it. I have a method step() to move them. But I need to print the result of the move. I get a segmentation fault after trying to print it. And are there any other methods to declare that chess board with symbols. It seems to me it is not properly declared. (Yes, chess pieces should be like "01""10". I will use that later to make rules for each piece type).

#include <iostream>
#include <stdio.h>
#include <string>
#define n 8
using namespace std;

int convert(char c) {
    return 0;
  else if(c=='b')
    return 1;
  else if(c=='c')
    return 2;
  else if(c=='d')
    return 3;
  else if(c=='e')
    return 4;
  else if(c=='f')
    return 5;
  else if(c=='g')
    return 6;
    return 7;

class chess {
  string board[n][n]= {{"02","03","04","06","05","04","03","02"}, //chessboard
                      {"01","01","01","01","01","01","01","01"}, //black
                      {"00","00","00","00","00","00","00","00"}, //00-empty cells
                      {"11","11","11","11","11","11","11","11"}, //white
  chess() {}

  void print() {
    cout<<"  _";  //board marking
    for (int i='a'; i<='h'; i++)

    for (int i=0; i<n; i++) { 
      printf("%d| ", i+1); //board marking
      for (int j=0; j<n; j++) {
        printf("%.2s ",board[i][j].c_str()); //print chess pieces

  void step() { //move on another cell
    char figc, fign, cellc, celln;
    cout<<"Piece, char: "; //choose a chess piece
    cout<<"Piece, num: ";
    cout<<"Step, char: "; //move on cell - ...
    cout<<"Step, num: ";

int main() {
  chess A;
  return 0;

Example. Screenshot

Compiling your code with

g++ -g -faddress=sanitize main.cpp

Shows the following output:

    #0 0x7fdc8e7c4b40 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x124b40)
    #1 0x7fdc8e7c4f18 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x124f18)
    #2 0x56310c7ad092 in chess::step() /z/bob.cpp:65
    #3 0x56310c7a9c10 in main /z/bob.cpp:72
    #4 0x7fdc8e0b8b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #5 0x56310c7a9a19 in _start (/z/a.out+0x1a19)

Line 65 is:


in which we see fign, which is a char used as an integer.

Since the ascii range of letters and numbers is somewhere in the range 48-122, this exceeds the size of your array.



would have told you this immediately because .at() does bounds checking.

C Programming Tutorial for Beginners, Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C) C or c is the third letter in the English and ISO basic Latin alphabets.Its name in English is cee (pronounced / ˈ s iː /), plural cees.

"C" Programming Language: Brian Kernighan, This course will give you a full introduction into all of the core concepts in the C programming Duration: 3:46:13 Posted: Aug 15, 2018 C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language.

You're using a char fign and doing -1 on it.

Better to use atoi in this case --


Outputs --

1| 02 03 04 06 05 04 03 02
2| 01 01 01 01 01 01 01 01
3| 00 00 00 00 00 00 00 00
4| 00 00 00 00 00 00 00 00
5| 00 00 00 00 00 00 00 00
6| 00 00 00 00 00 00 00 00
7| 11 11 11 11 11 11 11 00
8| 12 13 14 16 15 14 13 12

Why you should not perform char-int?

Well, I added a printf %d on that one, gave me this, which is quite self-explanatory.

printf ("\nThe result of bad operation : %d\n", fign - 1);
The result of bad operation : 54

C Tutorial, "C" is one of the most widely used programming languages of all time. Prof Brian Kernighan Duration: 8:26 Posted: Aug 18, 2015 C is a procedural programming language. It was initially developed by Dennis Ritchie as a system programming language to write operating system. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development.

Learn C, C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone� This is a list of operators in the C and C++ programming languages.All the operators listed exist in C++; the fourth column "Included in C", states whether an operator is also present in C. Note that C does not support operator overloading.. When not overloaded, for the operators &&, ||, and , (the comma operator), there is a sequence point after the evaluation of the first operand.

Learn C Programming, learn-c.org is a free interactive C tutorial for people who want to learn C, fast. As well as C and Simula's influences, other languages also influenced this new language, including ALGOL 68, Ada, CLU and ML . Initially, Stroustrup's "C with Classes" added features to the C compiler, Cpre, including classes, derived classes, strong typing, inlining and default arguments.

Cprogramming.com: Learn C and C++ Programming, C is a powerful general-purpose programming language. Our C tutorials will guide you to learn C programming one step at a time with the help of examples. C++ is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at Bell Labs.C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. This C++ tutorial adopts a simple and practical approach to describe the concepts of C++ for beginners to advanded software engineers.. Why to Learn C++. C++ is a MUST for students and working

  • fign and celln should be declared as int, instead of char
  • There is no OOP here. And very little C++ at all
  • please use tags for topics that are specific to the question. You also dont tag function, if-statement, private etc, just because your code uses them
  • Thanks for the suggestion!
  • atoi(&fign) has undefined behaviour, since atoi() assumes it receives a nul terminated string.
  • atoi is an unsafe C function. In C++ land it is better to use std::stoi.
  • Peter and Richard, thanks for the inputs :) Would it be safe if we purposefully create a null terminated char[] and then pass it to the function? Rather, how about (fign - '0') -1 instead of atoi?