Thought I would jump in. First a little background on me.
I've been in the Games Industry for 6 years. I've been a Programmer at Sony Computer Entertainment Europe Cambridge, I have 7 games to my name. Basically I am a hardcore c/++ programmer. I have just started looking at iOS dev and hence Mac dev and obj-c.
I was having the same problems FouFou was having with your code, below I have made some fixes which I hope should get it working for FouFou as well. Main point is the txt files now need a .txt extension which is how you download them from here, but its easy enough to change that back.
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] = "on bobbin"; //Inits the global one
char notonbobbin[14] = "not on bobbin";
char all[4] = "all";
char have[6] = "have";
char donthave[12] = "don't have";
enum commands
{
NoInput = 0
,OnBobbin
,NotOnBobbin
,Have
,DontHave
,All
,Blank
};
int i;
//int f; //this is a global which you you then pass into a function, which then has a local version that shadows this var. Makes it very confusing.
int h;
int r;
int len;
int arrays (enum commands f, int *hPtr);
void edit (int i);
int main (int argc, const char * argv[])
{
int h;
//for (r=0; r=100000; r++) //you have made a for loop with a stupidly large count to endless loop?
while(1) //this will loop for ever, until you return or break out of it
{
fpurge(stdin);
//these definition are showdowing the global ones above, so any code in this func gets a valid result,
//any code out of this func references onbobbin it will get random data.
/*
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);
for( int j = 0 ; j < len ; ++j )
{
input[j] = tolower(input[j]); //make sure all chars are lowercase for the following compares, so Have == have
}
enum commands f = NoInput;
if (*input == 'q')
{
printf("Quitting.");
return (0);
}
else if (*input == '\0')
{
f = Blank;
}
else if (strcmp(input, &onbobbin[0]) == 0) //this func take char*,char* onbobbin is char, &get the adress of, [0] gets the first element in memory
{
f = OnBobbin;
}
else if (strcmp(input, ¬onbobbin[0]) == 0)
{
f = NotOnBobbin;
}
else if (strcmp(input, &have[0]) == 0)
{
f = Have;
}
else if (strcmp(input, &donthave[0]) == 0)
{
f = DontHave;
}
else if (strcmp(input, &all[0]) == 0)
{
f = All;
}
else
{
f = NoInput;
}
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 (enum commands f, int *hPtr)
{
char compare5[] = "don't have";
FILE *namelist = fopen("../../namelist.txt", "r+");
FILE *codelist = fopen("../../codelist.txt", "r+");
FILE *wrappedlist = fopen("../../wrapped.txt", "r+");
FILE *havelist = fopen("../../havelist.txt", "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';
*/
//you are repeating checks here which makes the if statements more complicated and the code harder to read, while taking 2 as long.
if( f == OnBobbin ) //if f ==1
{
//if( strncmp(&input[0], &wrapped[i],1 ) this is comparing a single char?
if( input[0] == wrapped[i][0] )
{
printf("\n");
if (i >= 3 )
{
printf("#");
}
printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
if( i == 454 )
{
fclose(wrappedlist);
fclose(namelist);
fclose(codelist);
fclose(havelist);
*hPtr=0;
return(i,f); //this returns f, as you can only return one thing in c
}
}
}
else if ( f == NotOnBobbin )
{
if( input[0] == wrapped[i][0] )
{
printf("\n");
if (i >= 3 )
{
printf("#");
}
printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
if( i == 454 )
{
fclose(wrappedlist);
fclose(namelist);
fclose(codelist);
fclose(havelist);
*hPtr=0;
return(i,f); //this returns f, as you can only return one thing in c
}
}
}
else if( f == NoInput )
{
if( ( strcasecmp( &input[0], &code[i][0] ) == 0 ) || ( strcasecmp( &input[0], &name[i][0]) == 0 ) )
{
printf("\n");
if ( i >= 3 )
{
printf("#");
}
if( haveslist[i][0] == compare5[0] )
{
printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
}
else
{
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 == All )
{
printf("\n");
if (i>=3)
{
printf("#");
}
if( haveslist[i][0] == compare5[0] )
{
printf("%s, %s\n-----------\n%s\n",code[i],name[i],haveslist[i]);
}
else
{
printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
}
if( i == 454 )
{
fclose(wrappedlist);
fclose(namelist);
fclose(codelist);
fclose(havelist);
*hPtr=0;
return(i, f);
}
}
else if ( f == Have )
{
if( input[0] == haveslist[i][0] )
{
printf("\n");
if (i>=3)
{
printf("#");
}
printf("%s, %s\n-----------\n%s, %s\n",code[i],name[i],haveslist[i],wrapped[i]);
if( i == 454 )
{
fclose(wrappedlist);
fclose(namelist);
fclose(codelist);
fclose(havelist);
*hPtr=0;
return(i, f);
}
}
}
else if ( f == DontHave )
{
if ( strcmp( &input[0], &haveslist[i][0] ) == 0 )
{
printf("\n");
if (i>=3)
{
printf("#");
}
printf("%s, %s\n-----------\n%s\n",code[i],name[i],haveslist[i]);
if( i == 454 )
{
fclose(wrappedlist);
fclose(namelist);
fclose(codelist);
fclose(havelist);
*hPtr=0;
return(i, f);
}
}
}
else if ((i==453) && ((strcasecmp(&input[0], &code[i][0])!=0)||(strcmp(&input[0], &name[i][0])!=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.txt", "r+");
FILE *havelist = fopen("../../havelist.txt", "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;
}
Tested on Intel Mac OS X 10.6.3