Array of pointers
Author |
Message |
wtd
|
Posted: Thu Nov 17, 2005 1:07 pm Post subject: (No subject) |
|
|
Now, we try to compile this, and what happens?
code: | $ gcc life.c
life.c:64: error: conflicting types for 'generate'
life.c:5: error: previous declaration of 'generate' was here
life.c:64: error: conflicting types for 'generate'
life.c:5: error: previous declaration of 'generate' was here |
Ok, what do we have on line 64?
Well, around line 64 we have...
c: | void generate(char *grid[25], int y_dim) |
And we have an error about conflicting types. The next error is related to that.
What's on line 5?
c: | void generate(char (*grid)[25], int y_dim); |
Oh hey look... they're not the same. Let's make them the same.
c: | #include <stdio.h>
#define ROWS 25
#define COLS 25
void generate (char (*grid )[25], int y_dim );
int main (int argc, char *argv [])
{
int x, y, k, j, rep;
char cells [ROWS ][COLS ], i;
FILE *in; //Pointers to open and close the file
in = fopen ("data.txt", "r"); // open a file for reading
for (y = 0; y < 25; ++y )
{
for (x = 0; x < 25; ++x )
{
fscanf (in, "%c", &cells [x ][y ]);
}
}
fclose (in );
for (k = 0; k < COLS; ++k )
{
printf("%c", cells [j ][k ]);
}
printf("Enter 1 to generate again> ");
scanf ("%d", &rep );
if (rep == 1)
{
rep = 0;
do
{
generate (cells, COLS - 1);
for (j = 0; j < ROWS; ++j )
{
for (k = 0; k < COLS; ++k )
{
printf("%c", cells [j ][k ]);
}
printf("\n");
}
printf("Enter 1 to generate again> ");
scanf ("%d", &rep );
} while (rep = 1);
}
system ("pause");
return 0;
}
void generate (char (*grid )[25], int y_dim )
{
int x_i, y_i, friends = 0;
for (y_i = 1; y_i < y_dim; y_i++ )
{
for (x_i = 1; x_i < 24; x_i++ )
{
if (grid [y_i - 1][x_i - 1] == 'O')
friends++;
if (grid [y_i - 1][x_i ] == 'O')
friends++;
if (grid [y_i - 1][x_i + 1] == 'O')
friends++;
if (grid [y_i ][x_i - 1] == 'O')
friends++;
if (grid [y_i ][x_i + 1] == 'O')
friends++;
if (grid [y_i + 1][x_i - 1] == 'O')
friends++;
if (grid [y_i + 1][x_i ] == 'O')
friends++;
if (grid [y_i + 1][x_i + 1] == 'O')
friends++;
/// Conditions
if (grid [y_i ][x_i ] == 'O')
// no survival code due to no changes
if (friends >= 4 || friends < 2) // death
grid [y_i ][x_i ] = 'X';
if (grid [y_i ][x_i ] == 'X')
if (friends = 3) // birth
grid [y_i ][x_i ] == 'O';
}
}
}
|
Now let's compile again!
Oh look... it compiled fine. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
wtd
|
Posted: Thu Nov 17, 2005 1:09 pm Post subject: (No subject) |
|
|
Tubs wrote: I dont know how to do super cool stuff like that 
You don't know how to use the space key to indent your code?
As for the rest...
code: | [syntax="c"]...[/syntax] |
|
|
|
|
|
 |
Tubs

|
Posted: Thu Nov 17, 2005 1:22 pm Post subject: (No subject) |
|
|
My IDE said it compiled correctly... what a POS. |
|
|
|
|
 |
wtd
|
Posted: Thu Nov 17, 2005 1:31 pm Post subject: (No subject) |
|
|
Hmmmm.... maybe there's a reason wtd advises against newbies using IDEs...
Nawww.... couldn't be. |
|
|
|
|
 |
Tubs

|
Posted: Thu Nov 17, 2005 1:32 pm Post subject: (No subject) |
|
|
Even with those changes it has a windows error  |
|
|
|
|
 |
wtd
|
Posted: Thu Nov 17, 2005 3:54 pm Post subject: (No subject) |
|
|
That would be a logic error, that's much more difficult to identify.
See, with C, any such error tends to make the program either stop dead in its tracks, or produce faulty output... and often both. |
|
|
|
|
 |
Tubs

|
Posted: Thu Nov 17, 2005 4:23 pm Post subject: (No subject) |
|
|
Catching errors in your own code is hard, kind of like trying to fix grammatical mistakes in an essay you wrote. Of the three programs I have to write, not one work yet  |
|
|
|
|
 |
Tubs

|
Posted: Thu Nov 17, 2005 4:48 pm Post subject: (No subject) |
|
|
ok. This code runs correctly up to the point where the letter being guessed is inputted, at which point it goes all to hell (compile it and see ). I have been looking at this all afternoon and I can't figure out what the heck is going wrong, so I guess it might be one of C's funny little quirks.
code: |
#include <stdio.h>
int guessing (char target[5], char prev[25], char *cg);
void display (char guessed[25], char solved[5]);
int main(int argc, char *argv[])
{
int guesses, status, i, j;
char guessed[26], word[6] = {'d', 'o', 'n', 'k', 'e', 'y'} , solved[6];
char current_guess;
printf ("HANGMAN:\n");
for (i = 0; i < 26; ++i)
{
guessed[i]= '*';
}
for (i = 0; i < 6; ++i)
{
solved[i] = '-';
}
printf ("How many guesses would you like to have> ");
scanf ("%d", &guesses);
printf ("\n");
for (i = 0; i <= guesses; ++i)
{
display(guessed, solved);
status = guessing(word, guessed, ¤t_guess);
for (j = 0; j < 6; j++)
{
if (status == i)
{
printf ("You got the letter %c!\n", word[i]);
solved[i] = word[i];
}
}
if (status == 6)
{
printf ("That letter is incorrect, try again!\n");
}
else if (status == 7)
{
printf ("That letter has already been guessed, try again!\n");
}
}
printf ("\n");
printf ("");
system ("pause");
return 0;
}
int guessing (char target[5], char prev[25], char *cg)
{
char letter;
int j, i, char_code;
printf ("Guess a letter:> ");
scanf ("% c", letter);
char_code = (int)letter;
for (i = 0; i < 26; i++)
{
if (char_code == prev[i])
return 7;
}
if (letter == 'd')
{
return 0;
}
else if (letter == 'o')
{
return 1;
}
else if (letter == 'n')
{
return 2;
}
else if (letter == 'k')
{
return 3;
}
else if (letter = 'e')
{
return 4;
}
else if (letter == 'y')
{
return 5;
}
else
return 6;
}
|
Sorry for the hundred line program thing again, I would be more specific if I knew what I was looking for  |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
wtd
|
Posted: Thu Nov 17, 2005 5:06 pm Post subject: (No subject) |
|
|
I appreciate that you're frustrated, but please clean up your code with proper indentation. I'm getting tired of doing it myself. |
|
|
|
|
 |
Tubs

|
Posted: Thu Nov 17, 2005 7:09 pm Post subject: (No subject) |
|
|
What would you call proper? I was never told TO indent let alone how to do it. |
|
|
|
|
 |
wtd
|
Posted: Thu Nov 17, 2005 7:16 pm Post subject: (No subject) |
|
|
Take a loot at the code I've posted. |
|
|
|
|
 |
Tubs

|
Posted: Thu Nov 17, 2005 7:28 pm Post subject: (No subject) |
|
|
Hows this?
c: |
#include <stdio.h>
int guessing (char target [5], char prev [25], char *cg );
void display (char guessed [25], char solved [5]);
int main (int argc, char *argv [])
{
int guesses, status, i, j;
char guessed [26], word [6] = {'d', 'o', 'n', 'k', 'e', 'y'} , solved [6];
char current_guess;
printf ("HANGMAN:\n");
for (i = 0; i < 26; ++i )
{
guessed [i ]= '*';
}
for (i = 0; i < 6; ++i )
{
solved [i ] = '-';
}
printf ("How many guesses would you like to have> ");
scanf ("%d", &guesses );
printf ("\n");
for (i = 0; i <= guesses; ++i )
{
display (guessed, solved );
status = guessing (word, guessed, ¤t_guess );
for (j = 0; j < 6; j++ )
{
if (status == i )
{
printf ("You got the letter %c!\n", word [i ]);
solved [i ] = word [i ];
}
}
if (status == 6)
{
printf ("That letter is incorrect, try again!\n");
}
else if (status == 7)
{
printf ("That letter has already been guessed, try again!\n");
}
}
printf ("\n");
printf ("");
system ("pause");
return 0;
}
int guessing (char target [5], char prev [25], char *cg )
{
char letter;
int j, i, char_code;
printf ("Guess a letter:> ");
scanf ("% c", letter );
char_code = (int)letter;
for (i = 0; i < 26; i++ )
{
if (char_code == prev [i ])
return 7;
}
if (letter == 'd')
{
return 0;
}
else if (letter == 'o')
{
return 1;
}
else if (letter == 'n')
{
return 2;
}
else if (letter == 'k')
{
return 3;
}
else if (letter = 'e')
{
return 4;
}
else if (letter == 'y')
{
return 5;
}
else
return 6;
}
void display (char guessed [25], char solved [5])
{
int i;
printf ("You have used the letters: ");
for (i = 0; i <= 25; ++i )
{
printf ("%c", guessed [i ]);
}
printf ("\n");
printf ("The word so far is: ");
for (i = 0; i <= 5; ++i )
{
printf ("%c", solved [5]);
}
printf ("\n");
}
|
|
|
|
|
|
 |
wtd
|
Posted: Thu Nov 17, 2005 7:35 pm Post subject: (No subject) |
|
|
Why don't you indent code in functions? |
|
|
|
|
 |
[Gandalf]

|
Posted: Thu Nov 17, 2005 7:44 pm Post subject: (No subject) |
|
|
Get rid of all those unneccessary spaces between if statements. If anything, try to be consistent in how you intent throughout the program (not what you seem to be doing). |
|
|
|
|
 |
Tubs

|
Posted: Thu Nov 17, 2005 7:55 pm Post subject: (No subject) |
|
|
wtd wrote: Why don't you indent code in functions?
I had them as seperate units in the project and copied them seperately.
Ok, second try.
c: |
#include <stdio.h>
int guessing (char target [5], char prev [25], char *cg );
void display (char guessed [25], char solved [5]);
int main (int argc, char *argv [])
{
int guesses, status, i, j;
char guessed [26], word [6] = {'d', 'o', 'n', 'k', 'e', 'y'} , solved [6];
char current_guess;
printf ("HANGMAN:\n");
for (i = 0; i < 26; ++i )
{
guessed [i ]= '*';
}
for (i = 0; i < 6; ++i )
{
solved [i ] = '-';
}
printf ("How many guesses would you like to have> ");
scanf ("%d", &guesses );
printf ("\n");
for (i = 0; i <= guesses; ++i )
{
display (guessed, solved );
status = guessing (word, guessed, ¤t_guess );
for (j = 0; j < 6; j++ )
{
if (status == i )
{
printf ("You got the letter %c!\n", word [i ]);
solved [i ] = word [i ];
}
}
if (status == 6)
{
printf ("That letter is incorrect, try again!\n");
}
else if (status == 7)
{
printf ("That letter has already been guessed, try again!\n");
}
}
printf ("\n");
printf ("");
system ("pause");
return 0;
}
int guessing (char target [5], char prev [25], char *cg )
{
char letter;
int j, i, char_code;
printf ("Guess a letter:> ");
scanf ("% c", letter );
char_code = (int)letter;
for (i = 0; i < 26; i++ )
{
if (char_code == prev [i ])
return 7;
}
if (letter == 'd')
return 0;
else if (letter == 'o')
return 1;
else if (letter == 'n')
return 2;
else if (letter == 'k')
return 3;
else if (letter = 'e')
return 4;
else if (letter == 'y')
return 5;
else
return 6;
}
void display (char guessed [25], char solved [5])
{
int i;
printf ("You have used the letters: ");
for (i = 0; i <= 25; ++i )
{
printf ("%c", guessed [i ]);
}
printf ("\n");
printf ("The word so far is: ");
for (i = 0; i <= 5; ++i )
{
printf ("%c", solved [5]);
}
printf ("\n");
}
|
|
|
|
|
|
 |
|
|