Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

C++ Nightmares

Shoe_Eater
 Share

Before I start, i wish to point out that most of the code (the current messy version) i wrote below was from the 2 hours of me teaching myself how to use C++ by trying to recreate a program i made in python a while back that draws braille characters to the screen, the main issue i'm having is Segmentation Faults... AND I SPENT 6 HOURS TRYING TO FIX IT YESTERDAY AND I CANNOT FIND WHAT IS CAUSING THE ERROR.

 

Also one thing to note is that i have commented out several things for testing purposes to try to find the cause of the error, and it is of course nowhere near done, mostly because i cant test the output if i cant get it to run without segmentation faults constantly. Also if it helps (probably wont) I am using linux, and GCC is the compiler i'm using, and yes the terminal i'm using (and the font) are unicode compatable as i have been able to run my original python version of this program in it perfectly fine, after painfully realising xterm wasn't having any of it. Also i should probably point out that i have absolutely no idea what pointers and that sort of stuff do in C++, since i was kinda teaching myself C++ by writing this program, in 2 hours, and then 6 hours of hitting my head against the table.

#include <iostream>
using namespace std;

class braille
{
	private:
		//int cellsize[2] = {8, 4};
		int cellwidth,cellheight;
		int arraywidth,arrayheight;
		int xoffset,yoffset;
		//wchar_t charset[258] = L"⠀⠁⠂⠃⠄⠅⠆⠇⡀⡁⡂⡃⡄⡅⡆⡇⠈⠉⠊⠋⠌⠍⠎⠏⡈⡉⡊⡋⡌⡍⡎⡏⠐⠑⠒⠓⠔⠕⠖⠗⡐⡑⡒⡓⡔⡕⡖⡗⠘⠙⠚⠛⠜⠝⠞⠟⡘⡙⡚⡛⡜⡝⡞⡟⠠⠡⠢⠣⠤⠥⠦⠧⡠⡡⡢⡣⡤⡥⡦⡧⠨⠩⠪⠫⠬⠭⠮⠯⡨⡩⡪⡫⡬⡭⡮⡯⠰⠱⠲⠳⠴⠵⠶⠷⡰⡱⡲⡳⡴⡵⡶⡷⠸⠹⠺⠻⠼⠽⠾⠿⡸⡹⡺⡻⡼⡽⡾⡿⢀⢁⢂⢃⢄⢅⢆⢇⣀⣁⣂⣃⣄⣅⣆⣇⢈⢉⢊⢋⢌⢍⢎⢏⣈⣉⣊⣋⣌⣍⣎⣏⢐⢑⢒⢓⢔⢕⢖⢗⣐⣑⣒⣓⣔⣕⣖⣗⢘⢙⢚⢛⢜⢝⢞⢟⣘⣙⣚⣛⣜⣝⣞⣟⢠⢡⢢⢣⢤⢥⢦⢧⣠⣡⣢⣣⣤⣥⣦⣧⢨⢩⢪⢫⢬⢭⢮⢯⣨⣩⣪⣫⣬⣭⣮⣯⢰⢱⢲⢳⢴⢵⢶⢷⣰⣱⣲⣳⣴⣵⣶⣷⢸⢹⢺⢻⢼⢽⢾⢿⣸⣹⣺⣻⣼⣽⣾⣿";
		char charset[769] = "⠀⠁⠂⠃⠄⠅⠆⠇⡀⡁⡂⡃⡄⡅⡆⡇⠈⠉⠊⠋⠌⠍⠎⠏⡈⡉⡊⡋⡌⡍⡎⡏⠐⠑⠒⠓⠔⠕⠖⠗⡐⡑⡒⡓⡔⡕⡖⡗⠘⠙⠚⠛⠜⠝⠞⠟⡘⡙⡚⡛⡜⡝⡞⡟⠠⠡⠢⠣⠤⠥⠦⠧⡠⡡⡢⡣⡤⡥⡦⡧⠨⠩⠪⠫⠬⠭⠮⠯⡨⡩⡪⡫⡬⡭⡮⡯⠰⠱⠲⠳⠴⠵⠶⠷⡰⡱⡲⡳⡴⡵⡶⡷⠸⠹⠺⠻⠼⠽⠾⠿⡸⡹⡺⡻⡼⡽⡾⡿⢀⢁⢂⢃⢄⢅⢆⢇⣀⣁⣂⣃⣄⣅⣆⣇⢈⢉⢊⢋⢌⢍⢎⢏⣈⣉⣊⣋⣌⣍⣎⣏⢐⢑⢒⢓⢔⢕⢖⢗⣐⣑⣒⣓⣔⣕⣖⣗⢘⢙⢚⢛⢜⢝⢞⢟⣘⣙⣚⣛⣜⣝⣞⣟⢠⢡⢢⢣⢤⢥⢦⢧⣠⣡⣢⣣⣤⣥⣦⣧⢨⢩⢪⢫⢬⢭⢮⢯⣨⣩⣪⣫⣬⣭⣮⣯⢰⢱⢲⢳⢴⢵⢶⢷⣰⣱⣲⣳⣴⣵⣶⣷⢸⢹⢺⢻⢼⢽⢾⢿⣸⣹⣺⣻⣼⣽⣾⣿";
		//wchar_t charset[259] = {'⠀','⠁','⠂','⠃','⠄','⠅','⠆','⠇','⡀','⡁','⡂','⡃','⡄','⡅','⡆','⡇','⠈','⠉','⠊','⠋','⠌','⠍','⠎','⠏','⡈','⡉','⡊','⡋','⡌','⡍','⡎','⡏','⠐','⠑','⠒','⠓','⠔','⠕','⠖','⠗','⡐','⡑','⡒','⡓','⡔','⡕','⡖','⡗','⠘','⠙','⠚','⠛','⠜','⠝','⠞','⠟','⡘','⡙','⡚','⡛','⡜','⡝','⡞','⡟','⠠','⠡','⠢','⠣','⠤','⠥','⠦','⠧','⡠','⡡','⡢','⡣','⡤','⡥','⡦','⡧','⠨','⠩','⠪','⠫','⠬','⠭','⠮','⠯','⡨','⡩','⡪','⡫','⡬','⡭','⡮','⡯','⠰','⠱','⠲','⠳','⠴','⠵','⠶','⠷','⡰','⡱','⡲','⡳','⡴','⡵','⡶','⡷','⠸','⠹','⠺','⠻','⠼','⠽','⠾','⠿','⡸','⡹','⡺','⡻','⡼','⡽','⡾','⡿','⢀','⢁','⢂','⢃','⢄','⢅','⢆','⢇','⣀','⣁','⣂','⣃','⣄','⣅','⣆','⣇','⢈','⢉','⢊','⢋','⢌','⢍','⢎','⢏','⣈','⣉','⣊','⣋','⣌','⣍','⣎','⣏','⢐','⢑','⢒','⢓','⢔','⢕','⢖','⢗','⣐','⣑','⣒','⣓','⣔','⣕','⣖','⣗','⢘','⢙','⢚','⢛','⢜','⢝','⢞','⢟','⣘','⣙','⣚','⣛','⣜','⣝','⣞','⣟','⢠','⢡','⢢','⢣','⢤','⢥','⢦','⢧','⣠','⣡','⣢','⣣','⣤','⣥','⣦','⣧','⢨','⢩','⢪','⢫','⢬','⢭','⢮','⢯','⣨','⣩','⣪','⣫','⣬','⣭','⣮','⣯','⢰','⢱','⢲','⢳','⢴','⢵','⢶','⢷','⣰','⣱','⣲','⣳','⣴','⣵','⣶','⣷','⢸','⢹','⢺','⢻','⢼','⢽','⢾','⢿','⣸','⣹','⣺','⣻','⣼','⣽','⣾','⣿','\0'};
		//wchar_t charset[260]=L"\u2800\u2801\u2802\u2803\u2804\u2805\u2806\u2807\u2840\u2841\u2842\u2843\u2844\u2845\u2846\u2847\u2808\u2809\u280A\u280B\u280C\u280D\u280E\u280F\u2848\u2849\u284A\u284B\u284C\u284D\u284E\u284F\u2810\u2811\u2812\u2813\u2814\u2815\u2816\u2817\u2850\u2851\u2852\u2853\u2854\u2855\u2856\u2857\u2818\u2819\u281A\u281B\u281C\u281D\u281E\u281F\u2858\u2859\u285A\u285B\u285C\u285D\u285E\u285F\u2820\u2821\u2822\u2823\u2824\u2825\u2826\u2827\u2860\u2861\u2862\u2863\u2864\u2865\u2866\u2867\u2828\u2829\u282A\u282B\u282C\u282D\u282E\u282F\u2868\u2869\u286A\u286B\u286C\u286D\u286E\u286F\u2830\u2831\u2832\u2833\u2834\u2835\u2836\u2837\u2870\u2871\u2872\u2873\u2874\u2875\u2876\u2877\u2838\u2839\u283A\u283B\u283C\u283D\u283E\u283F\u2878\u2879\u287A\u287B\u287C\u287D\u287E\u287F\u2880\u2881\u2882\u2883\u2884\u2885\u2886\u2887\u28C0\u28C1\u28C2\u28C3\u28C4\u28C5\u28C6\u28C7\u2888\u2889\u288A\u288B\u288C\u288D\u288E\u288F\u28C8\u28C9\u28CA\u28CB\u28CC\u28CD\u28CE\u28CF\u2890\u2891\u2892\u2893\u2894\u2895\u2896\u2897\u28D0\u28D1\u28D2\u28D3\u28D4\u28D5\u28D6\u28D7\u2898\u2899\u289A\u289B\u289C\u289D\u289E\u289F\u28D8\u28D9\u28DA\u28DB\u28DC\u28DD\u28DE\u28DF\u28A0\u28A1\u28A2\u28A3\u28A4\u28A5\u28A6\u28A7\u28E0\u28E1\u28E2\u28E3\u28E4\u28E5\u28E6\u28E7\u28A8\u28A9\u28AA\u28AB\u28AC\u28AD\u28AE\u28AF\u28E8\u28E9\u28EA\u28EB\u28EC\u28ED\u28EE\u28EF\u28B0\u28B1\u28B2\u28B3\u28B4\u28B5\u28B6\u28B7\u28F0\u28F1\u28F2\u28F3\u28F4\u28F5\u28F6\u28F7\u28B8\u28B9\u28BA\u28BB\u28BC\u28BD\u28BE\u28BF\u28F8\u28F9\u28FA\u28FB\u28FC\u28FD\u28FE\u28FF";
		int array[10][10];
		
	public:
		braille (int cw,int ch,int x,int y)
		{
			cellwidth=cw;
			cellheight=ch;
			xoffset=x;
			yoffset=y;
			arraywidth=cellwidth*2;
			arrayheight=cellheight*4;
			int array[arrayheight][arraywidth]={0}; /*each braille char to be printed is 3 bytes e.g. cout<<charset[a*3]<<charset[a*3+1]<<charset[a*3+2];*/
		}
		void test ()
		{
			cout<<"Charset: {"<<charset<<"}"<<endl;
			cout<<"Total Array Slots: "<<array<<endl;
			cout<<"Cell Width: "<<cellwidth<<endl;
			cout<<"Cell Height: "<<cellheight<<endl;
			//cout<<"Chars: "<<charset<<endl<<"Array: "<<array<<endl<<"CellSize: "<<cellsize<<endl;
		}
		void update ()
		{
			int id;
			for (int h=0;h<cellheight;h+=1)
			{
				//char line[cellwidth*3+1];
				//line[cellwidth*3]='\0';
				cout<<"\033["<<h+yoffset<<";"<<xoffset<<"H";
				for (int w=0;w<cellwidth;w+=1)
				{
					//id=array[h][w]+array[h+1][w]*2+array[h+2][w]*4+array[h+3][w]*8+array[h][w+1]*16+array[h+1][w+1]*32+array[h+2][w+1]*64+array[h+3][w+1]*128;
					id=(array[h*4][w*2])+(array[h*4+1][w*2]*2)+(array[h*4+2][w*2]*4)+(array[h*4+3][w*2]*8)+(array[h*4][w*2+1]*16)+(array[h*4+1][w*2+1]*32)+(array[h*4+2][w+1]*64)+(array[h*4+3][w+1]*128);
					//line[w/2]=charset[90];
					//line[w/2]=charset[88];
					//line[w/2]=charset[array[h][w]+array[h+1][w]*2+array[h+2][w]*4+array[h+3][w]*8+array[h][w+1]*16+array[h+1][w+1]*32+array[h+2][w+1]*64+array[h+3][w+1]*128];
					//line[w]=charset[id*3];
					//line[w+1]=charset[id*3+1];
					//line[w+2]=charset[id*3+2];
					cout<<charset[id];
				}
				//cout<<"\033["<<h+yoffset<<";"<<xoffset<<"H";
				//cout<<line;
			}
		}
		void enable (int x,int y)
		{
			if (((x>=0)&&(x<arraywidth))&&((y>=0)&&(y<arraywidth)))
			{
				array[y][x]=true;
			}
		}
		void disable (int x,int y)
		{
			if (((x>=0)&&(x<arraywidth))&&((y>=0)&&(y<arraywidth)))
			{
				array[y][x]=false;
			}
		}
};

int main()
{
	braille br (8,4,2,4);
	br.enable(2,1);
	br.enable(1,4);
	br.update();
	/*int cellsize[] = {8,4};
	char charset[] = "⠀⠁⠂⠃⠄⠅⠆⠇⡀⡁⡂⡃⡄⡅⡆⡇⠈⠉⠊⠋⠌⠍⠎⠏⡈⡉⡊⡋⡌⡍⡎⡏⠐⠑⠒⠓⠔⠕⠖⠗⡐⡑⡒⡓⡔⡕⡖⡗⠘⠙⠚⠛⠜⠝⠞⠟⡘⡙⡚⡛⡜⡝⡞⡟⠠⠡⠢⠣⠤⠥⠦⠧⡠⡡⡢⡣⡤⡥⡦⡧⠨⠩⠪⠫⠬⠭⠮⠯⡨⡩⡪⡫⡬⡭⡮⡯⠰⠱⠲⠳⠴⠵⠶⠷⡰⡱⡲⡳⡴⡵⡶⡷⠸⠹⠺⠻⠼⠽⠾⠿⡸⡹⡺⡻⡼⡽⡾⡿⢀⢁⢂⢃⢄⢅⢆⢇⣀⣁⣂⣃⣄⣅⣆⣇⢈⢉⢊⢋⢌⢍⢎⢏⣈⣉⣊⣋⣌⣍⣎⣏⢐⢑⢒⢓⢔⢕⢖⢗⣐⣑⣒⣓⣔⣕⣖⣗⢘⢙⢚⢛⢜⢝⢞⢟⣘⣙⣚⣛⣜⣝⣞⣟⢠⢡⢢⢣⢤⢥⢦⢧⣠⣡⣢⣣⣤⣥⣦⣧⢨⢩⢪⢫⢬⢭⢮⢯⣨⣩⣪⣫⣬⣭⣮⣯⢰⢱⢲⢳⢴⢵⢶⢷⣰⣱⣲⣳⣴⣵⣶⣷⢸⢹⢺⢻⢼⢽⢾⢿⣸⣹⣺⣻⣼⣽⣾⣿";
	bool array[cellsize[1]*4][cellsize[0]*2]; //each braille char to be printed is 3 bytes e.g. cout<<charset[a*3]<<charset[a*3+1]<<charset[a*3+2];
	cout<<charset<<endl;*/
	return 0;
}

 

and ANY help is appreciated, and no i probably wont send my python version of the thing since it works fine but i feel self conscious about my code and it isnt fully optimised as i have been meaning to change a small thing in it to make it slightly faster at updating.

 

Also C++ hurts my head, it has fits about everything.

Sadness is the one true emotion, and happiness, well, that's just a lie, sadness is all many of us feel, and is all we need to feel, because having it any other way, would just be wrong, why be happy when you can just be miserable like myself. 

Link to comment
Share on other sites

Link to post
Share on other sites

This should help you get started:

#include <iostream>
#include <vector>
  
class Braille
{
  private:
    int cw, ch;
    int aw, ah;
    int x, y;
    char charset[789] = "...";
    std::vector<std::vector<int>> vector;
  
  public:
    Braille(int cw, int ch, int x, int y)
    {
      this->cw = cw;
      this->ch = ch;
      this->x = x;
      this->y = y;
      this->aw = 2*cw;
      this->ah = 4*ch;
      this->vector.resize(aw, std::vector<int>(ah));
    }
  
  ...
};

int main()
{
  Braille* b = new Braille(8, 4, 2, 4);
  b->enable(2, 1);
  b->enable(1, 4);
  b->update();
  
  return 0;
}

I'll also add that in my testing, the update() function seemingly didn't function correctly. What exactly are you trying to achieve here?

Link to comment
Share on other sites

Link to post
Share on other sites

Thankyou @mshaugh for the help, that has fixed some issues, and now i just have to try to get this madness to output unicode properly, otherwise this would all be a waste. 

Also if you want to see what i'm going for, here is a screenshot of me plotting a graph using the python version of what im currently trying to recreate in c++, if the screenshot does actually send.

image.png.d4489c1d53f0531711ed75e630aeeffb.png

Sadness is the one true emotion, and happiness, well, that's just a lie, sadness is all many of us feel, and is all we need to feel, because having it any other way, would just be wrong, why be happy when you can just be miserable like myself. 

Link to comment
Share on other sites

Link to post
Share on other sites

Yes, I can see. Could you explain to me what update() is supposed to do? Specifically, the values for id. If I understand correctly it should return values 0 <= update() < 789, but at the moment that simply isn't going to work, as you're trying to access indices that don't exist.

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, mshaugh said:

Yes, I can see. Could you explain to me what update() is supposed to do? Specifically, the values for id. If I understand correctly it should return values 0 <= update() < 789, but at the moment that simply isn't going to work, as you're trying to access indices that don't exist.

update draws the braille characters to the console, and there are 256 braille characters, but as they are Unicode, they are 3 bytes each, meaning they are 3 regular characters, so in the final version that i now have working, it outputs 3 that are in sequence instead of the one that you are seeing. 

As you can see, this is 3 dots being drawn successfully with it, thanks to your help

image.png.6d68a763377ce8f3ad6812e268b56b36.png 

 

Sadness is the one true emotion, and happiness, well, that's just a lie, sadness is all many of us feel, and is all we need to feel, because having it any other way, would just be wrong, why be happy when you can just be miserable like myself. 

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, Shoe_Eater said:

the main issue i'm having is Segmentation Faults.

Caused by the calculation of the "id" index into the charset. It yields none-sense values on certain occasions. (that's not to say that is the only problem).

Output of a little test modification that checks id and outputs the values when out of bounds:

id = 204527669, charset size 769
id = -1096492423, charset size 769
...etc

The code is too messy to investigate in great detail but at first glance it looks like you're declaring a new 'array' in the constructor and never initialize the member 'array':

//This declares a new 'array' that masks the member 'array'...
//member 'array' is never initialised.
int array[arrayheight][arraywidth]={0}

//Interestingly, this is actually declaring a variable length array, which should not... 
//...even compile under strict C++

 

 

6 hours ago, Shoe_Eater said:

the current messy version

That's probably the main cause where all other errors follow from. It's too messy - clean up first.

 

As for a few small tips:

  • Break up the logic behind a line like:
    id=(array[h*4][w*2])+(array[h*4+1][w*2]*2)+(array[h*4+2][w*2]*4)+(array[h*4+3][w*2]*8)+(array[h*4][w*2+1]*16)+(array[h*4+1][w*2+1]*32)+(array[h*4+2][w+1]*64)+(array[h*4+3][w+1]*128);

    into a more manageable system. A tangled mess like that is asking for problems.

  • Use std::vector or std::array in stead of raw arrays. Both have a 'at' member function that throws an exception when trying to go out of bounds. (and most compilers have a flag that reroutes access from the index operator '[]' trough 'at' for debug builds.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×