Sorry for the downtime, welcome back!!

Creating KG-Chart for Mac

Forum and Craft Related Topics...
nintandrew
Rank 5 - Frog Mario
Rank 5 - Frog Mario
Posts: 209
Joined: Mon Jun 16, 2008 7:36 pm

Re: Creating KG-Chart for Mac

Post by nintandrew »

When you run it in the debugger console, does it come up with any errors?
I added a "better" haveslist file and try this code instead:
Spoiler

Code: Select all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


char code[454][7];
char name [454][26];
char haveslist [454][11];
char wrapped [454][15];
char input [25];
char onbobbin[10];
char notonbobbin[14];
char all[4];
char have[6];
char donthave[12];
int i;
int f;
int h;
int r;
int len;
int arrays (int f, int *hPtr);
void edit (int i);

int main (int argc, const char * argv[]) {
	int h;
	for (r=0; r=100000; r++) {
		fpurge(stdin);
		
	char onbobbin[] = "on bobbin";
	char notonbobbin[] = "not on bobbin";
	char all[] = "all";
	char have[] = "have";
	char donthave[] = "don't have";
	
		if (r!=0) {
		printf("\n-----------\n");
		}
	printf("\nEnter code, name of color, \"on bobbin\", \"not on bobbin\", \"have\", \"don't have\" or \"all\" (q=quit):\n ");
	fgets(input, 25, stdin);
	len = strlen(input);
	input[len - 1] = '\0';
		
		if (*input == 'q') {
			printf("Quitting.");
			return (0);
		}
		else if (*input == '\0'){
			f = 6;
		}
		
	else if (strcmp(input, onbobbin) == 0) {
		f=1;
	}
	
	else if (strcmp(input, notonbobbin) == 0) {
		f=2;
	}
	else if (strcmp(input, have) == 0) {
		f=3;
	}
	else if (strcmp(input, donthave) == 0) {
		f=4;
	}
	else if (strcmp(input, all) == 0) {
		f=5;
	}
	else {f=0;}
		
		arrays(f, &h);
				
		if (h!=1 && f==0) {
		edit(i);

	}
		else if (h == 1 && f==0) {
			printf("Invalid entry.\n");
		}
		
	}
	
	printf("-----------\n");
	
    return 0;
	
}

int arrays (int f, int *hPtr) {
	char compare5[] = "don't have";
		
FILE *namelist = fopen("../../namelist", "r+");
FILE *codelist = fopen("../../codelist", "r+");
FILE *wrappedlist = fopen("../../wrapped", "r+");
FILE *havelist = fopen("../../havelist", "r+");


fseek(namelist, 0, SEEK_SET);
fseek(codelist, 0, SEEK_SET);
fseek(wrappedlist, 0, SEEK_SET);
fseek(havelist, 0, SEEK_SET);

	

for (i=0; i<=453; i++) {
	
	fgets(wrapped[i], 15, wrappedlist);
	fgets(code[i], 7, codelist);
	fgets(name[i], 26, namelist);
	fgets(haveslist[i], 12, havelist);

	len = strlen(code[i]);
	code[i][len - 1] = '\0';
	len = strlen(name[i]);
	name[i][len - 1] = '\0';
	len = strlen(wrapped[i]);
	wrapped[i][len - 1] = '\0';
	len = strlen(haveslist[i]);
	haveslist[i][len - 1] = '\0';
	if (f==1 && (strncmp(input, wrapped[i],1) == 0) && i!=454) {
		printf("\n");
		if (i>=3) {
			printf("#");
		}
			printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
	}
	else if (f==1 && (strncmp(input, wrapped[i],1) == 0) && i==454) {
		printf("\n");
		if (i>=3) {
			printf("#");
		}
		printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
		fclose(wrappedlist);
		fclose(namelist);
		fclose(codelist);
		fclose(havelist);
		*hPtr=0;
		return(i,f);
	}
	else if (f==2 && (strcmp(input, wrapped[i]) == 0) && i!=454){
		printf("\n");
		if (i>=3) {
			printf("#");
		}
		printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
	}
	else if (f==2 && (strcmp(input, wrapped[i]) == 0) && i==454){
		printf("\n");
		if (i>=3) {
			printf("#");
		}
		printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
		fclose(wrappedlist);
		fclose(namelist);
		fclose(codelist);
		fclose(havelist);
		*hPtr=0;
		return(i, f);
	}
	
	else if (f==0 && (strcasecmp(input, code[i])==0) && (strncmp(haveslist[i], compare5, (size_t)1) != 0)) {
		printf("\n");
		if (i>=3) {
			printf("#");
		}
		printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
		fclose(wrappedlist);
		fclose(namelist);
		fclose(codelist);
		fclose(havelist);
		f=0;
		*hPtr=0;
		return(i, f);
	}
	else if (f==0 && (strcasecmp(input, code[i])==0) && (strncmp(haveslist[i], compare5, (size_t)1) == 0)) {
		printf("\n");
		if (i>=3) {
			printf("#");
		}
		printf("%s, %s\n-----------\n%s\n",code[i],name[i],haveslist[i]);
		fclose(wrappedlist);
		fclose(namelist);
		fclose(codelist);
		fclose(havelist);
		f=0;
		*hPtr=0;
		return(i, f);
	}
	else if (f==0 && (strcasecmp(input, name[i])==0) && (strncmp(haveslist[i], compare5, (size_t)1) != 0)) {
		printf("\n");
		if (i>=3) {
		printf("#");
	}
		printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
		fclose(wrappedlist);
		fclose(namelist);
		fclose(codelist);
		fclose(havelist);
		f=0;
		*hPtr=0;
		return(i, f);
	}
	else if (f==0 && (strcasecmp(input, name[i])==0) && (strncmp(haveslist[i], compare5, (size_t)1) == 0)) {
		printf("\n");
		if (i>=3) {
		printf("#");
		 }
		printf("%s, %s\n-----------\n%s\n",code[i],name[i],haveslist[i]);
		fclose(wrappedlist);
		fclose(namelist);
		fclose(codelist);
		fclose(havelist);
		f=0;
		*hPtr=0;
		return(i, f);
		}	
			 
	else if(f==5 && i!=454 && strncmp(haveslist[i], compare5, (size_t)1)!=0){
		printf("\n");
		if (i>=3) {
			printf("#");
		}
		
		printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
	}
	else if(f==5 && i!=454 && (strncmp(haveslist[i], compare5, (size_t)1)==0)){
		printf("\n");
		if (i>=3) {
			printf("#");
		}
		
		printf("%s, %s\n-----------\n%s\n",code[i],name[i],haveslist[i]);
	}
	
	
	else if (f==3 && (strncmp(input, haveslist[i], (size_t)1)==0) && i!= 454) {
		printf("\n");
		if (i>=3) {
			printf("#");
		}
		printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
		}
	else if (f==3 && (strncmp(input, haveslist[i], (size_t)1)==0) && i== 454) {
		printf("\n");
		if (i>=3) {
			printf("#");
		}
		printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
		fclose(wrappedlist);
		fclose(namelist);
		fclose(codelist);
		fclose(havelist);
		*hPtr=0;
		return(i, f);
	}
	else if (f==4 && (strcmp(input, haveslist[i])==0) && i != 454) {
		printf("\n");
		if (i>=3) {
			printf("#");
		}
		printf("%s, %s\n-----------\n%s\n",code[i],name[i],haveslist[i]);
	}
	else if (f==4 && (strcmp(input, haveslist[i])==0) && i == 454) {
		printf("\n");
		if (i>=3) {
			printf("#");
		}
		printf("%s, %s\n-----------\n%s\n",code[i],name[i],haveslist[i]);
		fclose(wrappedlist);
		fclose(namelist);
		fclose(codelist);
		fclose(havelist);
		*hPtr=0;
		return(i, f);
	}
	else if (f==5 && i==454 && (strncmp(haveslist[i], compare5, (size_t)1)!=0)){
		printf("\n");
		if (i>=3) {
			printf("#");
		}
		
		printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
		fclose(wrappedlist);
		fclose(namelist);
		fclose(codelist);
		fclose(havelist);
		*hPtr=0;
		return(i, f);
	}
	else if (f==5 && i==454 && (strncmp(haveslist[i], compare5, (size_t)1)==0)){
		printf("\n");
		if (i>=3) {
			printf("#");
		}
		
		printf("%s, %s\n-----------\n%s\n",code[i],name[i],haveslist[i]);
		fclose(wrappedlist);
		fclose(namelist);
		fclose(codelist);
		fclose(havelist);
		*hPtr=0;
		return(i, f);
	}
	else if ((i==453) && ((strcasecmp(input, code[i])!=0)||(strcmp(input, name[i])!=0))){
		fclose(wrappedlist);
		fclose(namelist);
		fclose(codelist);
		fclose(havelist);
		*hPtr=1;
		return(i, f);
	}
}

	return (0);
}
	
			

void edit (int i){
	char ans[5];
	char ans2[5];
	char yes[] = "yes";
	char no[] = "no";
	char compare1[] = "on bobbin\b\b\b\b";
	char compare2[] = "not on bobbin";
	char compare3[] = "have\b\b\b\b\b\b";
	char compare4[] = "don't have";
	char wrapedit[] = "wrapped";
	char haveedit[] = "have";
	char editing[9];
		
	FILE *wrappedlist = fopen("../../wrapped", "r+");
	FILE *havelist = fopen("../../havelist", "r+");
	
	fseek(wrappedlist, i*14, SEEK_SET);
	fseek(havelist, i*11, SEEK_SET);
	
	fpurge(stdin);
	
	printf("Edit? (\"yes\"/\"no\")\n");
	fgets(ans, 5, stdin);
	len = strlen(ans);
	ans[len - 1] = 0;	
	
	if ((strcmp(ans, yes) == 0) && (strncmp(haveslist[i], compare4, (size_t)1) == 0)) {
		fpurge(stdin);
		
		printf("Switch \"don't have\" to \"have\"? (\"yes\"/\"no\")\n");
		fgets(ans2, 5, stdin);
		len = strlen(ans2);
		ans2[len -1] = 0;
		
		if (strcmp(ans2, yes)==0) {
			printf("Switched \"don't have\" to \"have\".");
			fputs(compare3, havelist);
			fclose(wrappedlist);
			fclose(havelist);
			return;
		}
		else if(strcmp(ans2, no)==0){
			
			printf("Not switching \"don't have\" to \"have\".");
			fclose(wrappedlist);
			fclose(havelist);
			return;
		}

		printf("Invalid entry1.");
			fclose(wrappedlist);
			fclose(havelist);
		return;
	
	}
	
	else if ((strcmp(ans, yes)==0) && (strncmp(haveslist[i], compare3, (size_t)1) == 0)) {
		
		printf("Edit \"have\" or \"wrapped\"?\n");
		fpurge(stdin);
		fgets(editing, 9, stdin);
		len = strlen(editing);
		editing[len - 1] = 0;
		
		if (strcmp(editing, wrapedit)==0) {
		
		if (strncmp(wrapped[i], compare1, (size_t)1)==0) {
			
			printf("Switch \"on bobbin\" to \"not on bobbin\"? (\"yes\"/\"no\")\n");
			fgets(ans2, 5, stdin);
			len = strlen(ans2);
			ans2[len - 1] = 0;
			
			if (strcmp(ans2, yes)==0) {
				printf("Switched \"on bobbin\" to \"not on bobbin\".");
				fputs(compare2, wrappedlist);
				fclose(wrappedlist);
				fclose(havelist);
				return;
			}
			else if(strcmp(ans2, no)==0){
				
				printf("Not switching \"on bobbin\" to \"not on bobbin\".");
				fclose(wrappedlist);
				fclose(havelist);
				return;
			}
			printf("Invalid entry2.");
			fclose(wrappedlist);
			fclose(havelist);
			return;
		}
			else if(strncmp(wrapped[i], compare2, (size_t)1)==0){
				
				
				printf("Switch \"not on bobbin\" to \"on bobbin\"? (\"yes\"/\"no\")\n");
				fgets(ans2, 5, stdin);
				len = strlen(ans2);
				ans2[len - 1] = 0;
				if (strcmp(ans2, yes)==0) {
					printf("Switched \"not on bobbin\" to \"on bobbin\".");
					fwrite(compare1, (size_t)strlen(compare1), 1, wrappedlist);
					fclose(wrappedlist);
					fclose(havelist);
					return;
				}
				else if(strcmp(ans2, no)==0){
					printf("Not switching \"not on bobbin\" to \"on bobbin\".");
					fclose(wrappedlist);
					fclose(havelist);
					return;
		}
				
				printf("Invalid entry.");
				fclose(wrappedlist);
				fclose(havelist);
				return;
		}
			
			printf("You don't want to edit wrapped apparently.");
			fclose(wrappedlist);
			fclose(havelist);
			return;
		}
		else if(strcmp(editing, haveedit)==0){
		
			if (strncmp(haveslist[i], compare3, 1) == 0){
				
				printf("Switch \"have\" to \"don't have\"? (\"yes\"/\"no\")\n");
				fgets(ans2, 5, stdin);
				len = strlen(ans2);
				ans2[len - 1] = 0;
				if (strcmp(ans2, yes)==0) {
					printf("Switched \"have\" to \"don't have\".");
					fputs(compare2, wrappedlist);
					fputs(compare4, havelist);
					fclose(wrappedlist);
					fclose(havelist);
					return;
				}
				else if(strcmp(ans2, no)==0){
					printf("Not switching \"have\" to \"don't have\".");
					fclose(wrappedlist);
					fclose(havelist);
					return;
				}
				printf("Invalid entry.");
				fclose(wrappedlist);
				fclose(havelist);
				return;
			}
			printf("Invalid entry.");
			fclose(wrappedlist);
			fclose(havelist);
			return;
		}
			printf("Invalid Entry.");
		
		fclose(wrappedlist);
		fclose(havelist);
		
		return;
	}
	else if(strcmp(ans, no)==0){
		printf("Not editing.");
		fclose(wrappedlist);
		fclose(havelist);
		return;
	}
	printf("Invalid entry.");
	fclose(havelist);
	fclose(wrappedlist);
	return;
}
Attachments
havelist.txt
(4.88 KiB) Downloaded 250 times
Eyup

User avatar
FouFou
Rank 3 - Star Mario
Rank 3 - Star Mario
Posts: 51
Joined: Sat May 22, 2010 6:39 pm

Re: Creating KG-Chart for Mac

Post by FouFou »

Ok, I'll check it out when I have a chance (heading out the door atm), but I did notice this:


FILE *namelist = fopen("../../namelist", "r+");
FILE *codelist = fopen("../../codelist", "r+");
FILE *wrappedlist = fopen("../../wrapped", "r+");
FILE *havelist = fopen("../../havelist", "r+");


that would imply that the namelist,codelist, etc are all in a different folder than the main spritesitch.c file? That could be causing a bus error when the program tried to access these files. Right now I have it set up so all files, including the original c file are all in the same folder.

nintandrew
Rank 5 - Frog Mario
Rank 5 - Frog Mario
Posts: 209
Joined: Mon Jun 16, 2008 7:36 pm

Re: Creating KG-Chart for Mac

Post by nintandrew »

In my documents folder, I have a file that xCode made called FlossOwned. It looks like this:
Spoiler
Image
If you go into xCode, start a new XCode project and on the next window click "Choose" at the bottom right without changing any settings and title it FlossOwned (or anything you want I guess). Then, in the new window that pops up, there should be a list near the top and middle and "main.c" should be one of them. Click it and this code should be seen:
Spoiler

Code: Select all

#include <stdio.h>

int main (int argc, const char * argv[]) {
    // insert code here...
    printf("Hello, World!\n");
    return 0;
}
Delete all of this and paste in my code. Hit "command+shift+r" to open the debugger console. Hit "command+enter" to run it, there may be errors because the files are not there yet. Click the red stop sign at the top of the debugger console or hit "command+shift+enter" (if the program is still running, you will know by the stop sign being clickable) and close the debugger window. Now, open up the project folder, it is in documents by default when you are making the name for the new project. Open the file in documents and it should look like my picture above but without the files. Put my files in this folder and remove the .txt extensions by right-clicking a document (you may have to open the file in "Finder" to be able to do this), select "get info". A new window should pop up and about half way down, it should say "Name & Extension", delete the .txt and close this window. It will ask if you want to change the file, change the file. Do this file extension change for each file. When finished, go back to xCode and run the program in the debugger window as stated above, it should work. I don't know how to run it anywhere else, and it works, so that's all I need.

Sorry if that's too in depth, I don't know how experienced with computers you are, but if you can run xCode and know what parts of the code might mean, you're doing better than most of the people in my family. Any more errors, please tell me.
Eyup

User avatar
FouFou
Rank 3 - Star Mario
Rank 3 - Star Mario
Posts: 51
Joined: Sat May 22, 2010 6:39 pm

Re: Creating KG-Chart for Mac

Post by FouFou »

Ok, I went with using Xcode rather than running it in command line, and set up everything the way you set it up. I'm getting “EXC_BAD_ACCESS” in the debugger when I try to run the program, although I'm not sure what line is causing the error. Pretty much whatever I enter, be it DC number, color, "on bobbin", random letters, etc ends up with a bus error, besides quitting, which works correctly.

User avatar
FouFou
Rank 3 - Star Mario
Rank 3 - Star Mario
Posts: 51
Joined: Sat May 22, 2010 6:39 pm

Re: Creating KG-Chart for Mac

Post by FouFou »

Ok, I did some manually debugging, looks like the line that is throwing the initial bus error is:

len = strlen(input);

So right after you get the initial input from the user and are attempting to figuring out string length. input does equal what I typed in, but strlen() on input is causing the bus error. When I try to do a printf on len to try to figure out what it is, I'm getting a warning saying "passing argument 1 of 'printf' makes pointer from integer without a cast" - which I don't get if I try to do a printf of input - which works fine.

Hope that helps. :-/

nintandrew
Rank 5 - Frog Mario
Rank 5 - Frog Mario
Posts: 209
Joined: Mon Jun 16, 2008 7:36 pm

Re: Creating KG-Chart for Mac

Post by nintandrew »

Well, that EXC_BAD_ACCESS error usually means that something is called from memory in a wrong way. You do have the code from my post on the top of this page, right? Also, "len" is an integer, so to print it out, you need "printf("%i", len)" while "printf("%s", input)" works because input is an array and an array name without its brackets is a pointer to the first element of that array. (I don't really understand pointers yet, if the program said something about a pointer I would end up on the internet for half an hour and then figure it out). Okay, I just tried it and got the same error. When you run the program in xCode for the first time, even with the error, it makes a folder with the name of the program in your documents folder. Put the 4 documents in this folder and make sure you have the haveslist file from the top of this page. Delete the .txt extensions for these files in the program folder. Run the program again, it should work. Thanks for the patience.
Eyup

User avatar
FouFou
Rank 3 - Star Mario
Rank 3 - Star Mario
Posts: 51
Joined: Sat May 22, 2010 6:39 pm

Re: Creating KG-Chart for Mac

Post by FouFou »

First off, what type of Mac are you on? If you're on a MacPro or a PowerPC then you have a different CPU architecture. Different CPU architectures treat initialized variables in different ways. In PowerPC you have initialized variables automatically initializing to 0, while on Intel (newer Macs) this is initialized to some random large negative number (in my case it was -1881141193).

From what I could tell, going through your code 'h' is never initialized before calling arrays(f, &h). Yes, you declare the variable, but good C programing practice is every time you declare a variable such as an int, give it some value (so h = 1); I'm still unsure as to what h is supposed to do, since in the main loop in function main, besides being declared at the beginning, and that used as a pointer that is still undefined gets passed into arrays().

Also you can just get rid of they whole strlen() business (which can on occasion cause bus errors), which it looks like you're using to get rid of the '/n' character in the string. Instead, just have the string arrays look something like this:

char onbobbin[] = "on bobbin\n";
char notonbobbin[] = "not on bobbin\n";
char all[] = "all\n";
char have[] = "have\n";
char donthave[] = "don't have\n";

By doing that, I've got the main function working on my macbook with an intel processor, although I haven't sat down yet and tried to get arrays() working yet.


To your previous post, yes I was using your code at the top of the page that was originally a .txt file, however that information wasn't called until arrays() and I was attempting to at least get that far in the code first. All four documents are in the xcode project folder, and xcode has been made aware of their existence. I'm suspecting why it isn't working on my machine has to do with the fact that C requires manual memory management, unlike Java which is the same on each machine because it's handled inside a virtual machine while C depends on the the operating system and CPU because C works on your computer's machine language level. (er, hope that made sense...)

nintandrew
Rank 5 - Frog Mario
Rank 5 - Frog Mario
Posts: 209
Joined: Mon Jun 16, 2008 7:36 pm

Re: Creating KG-Chart for Mac

Post by nintandrew »

o_0
I had no idea I would run into someone else that understood this on Sprite Stitch, thank you. I was trying to remove the "\n" but I guess I could have added that in the array initializations. I have an Intel MacBook Pro that I got earlier this year, new. Also, thanks for the tips on initializing h and about strlen. "h" is actually used in arrays so if input doesn't match anything, "Invalid entry." is displayed. I probably could have done something different, but this is my first program.
Thanks for all the tips, sorry it doesn't work like it should, but I made a new project and copied the code from here and downloaded the files and it worked for me, though different machines can behave differently.
Eyup

User avatar
FouFou
Rank 3 - Star Mario
Rank 3 - Star Mario
Posts: 51
Joined: Sat May 22, 2010 6:39 pm

Re: Creating KG-Chart for Mac

Post by FouFou »

Sometimes code is funny - it works one place, and it would make sense for it to work elsewhere, and it does. (Or, my favorite case, you know the code shouldn't work at all, and yet it for some reason works perfectly). And it's great for a first program - at least you're learning programing in C. My first language with Java, which doesn't have the programer deal with memory management at all. When I started using C, you can imagine my confusion when I started having to deal with "Segmentation Faults" and "Bus Errors" - problems you would never see in Java. By starting with C, you're actually learning good programing practices (even if dealing with Strings in Java is sooooooo much easier). The project idea for having KG-Chart for Mac sounds like a great idea, and I wish you the best of luck. I can continue playing around with the code you gave me if you would like, to see if I can get it running on my computer. :D

nintandrew
Rank 5 - Frog Mario
Rank 5 - Frog Mario
Posts: 209
Joined: Mon Jun 16, 2008 7:36 pm

Re: Creating KG-Chart for Mac

Post by nintandrew »

Thanks, if you get it working, let me know. I can't believe it can work for me and not for you. That's nuts. Sorry.
Eyup

Post Reply