d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > I Have To Be Missing Something > Anyone?
Add Reply New Topic New Poll
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Oct 25 2012 10:14pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 25 2012 10:36pm
can you please post the source code in code tags here instead of this hard to read screenshot
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Oct 26 2012 03:41am
Code
#include <stdio.h>

int main() {

char wordA[6];
char wordB[6];
char guessed[7];


int i;
int j;
int k;
int h;

int counter;
int check;

wordA[0] = 'h';
wordA[1] = 'a';
wordA[2] = 'n';
wordA[3] = 'g';
wordA[4] = 'm';
wordA[5] = 'a';
wordA[6] = 'n';

check = 0;
counter = 0;

//printf("check 1: %c\n", wordA[0]);

for (i = 0; i < 7; i++) {

guessed[i] = '*';

}

//printf("check 2: %c\n", wordA[0]);

for (h = 0; h <= 7; h++) {

wordB[h] = '*';
   printf("check 3: %c\n", wordA[h]);
   printf("check 4: %c\n", wordB[h]);


}

//Some how this above for loop is reading into the word1 array but only into the first position
printf("check 5: %c\n", wordA[0]);

return 0;

}


Sorry about that

There are some unused variables and whatnot but really I just need to know to why word1[] array is being changed by the for loop with only word2[] array in it...or appears to be at least. Also note this is not the EXACT same code I added some printf checks inside the loop but the output is still the same and so ir everything else.

This post was edited by Nom_Nomz on Oct 26 2012 03:44am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 26 2012 03:59am
well lets see

wordA = "hangman"

guessed = "*********" or something like that

wordB = "*********"


this is what your program does right now. I don't see what you are talking about.

You need to post the ENTIRE code man.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 26 2012 04:07am
Quote (Nom_Nomz @ Oct 26 2012 05:41am)
#include <stdio.h>

int main() {

char wordA[6];
char wordB[6];

char guessed[7];


int i;
int j;
int k;
int h;

int counter;
int check;

wordA[0] = 'h';
wordA[1] = 'a';
wordA[2] = 'n';
wordA[3] = 'g';
wordA[4] = 'm';
wordA[5] = 'a';
wordA[6] = 'n';

check = 0;
counter = 0;

//printf("check 1: %c\n", wordA[0]);

for (i = 0; i < 7; i++) {

guessed[i] = '*';

}

//printf("check 2: %c\n", wordA[0]);

for (h = 0; h <= 7; h++) {

wordB[h] = '*';
    printf("check 3: %c\n", wordA[h]);
    printf("check 4: %c\n", wordB[h]);


}

//Some how this above for loop is reading into the word1 array but only into the first position
printf("check 5: %c\n", wordA[0]);

return 0;

}


if you declare it size n, you can only access 0 to n -1. eg: if you declare it as size 6, you can only access 0 to 5. since you're accessing slots 6 and 7, you're going out of bounds and overwriting your own memory. this isn't java, so you gotta watch yourself.

This post was edited by carteblanche on Oct 26 2012 04:09am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 26 2012 04:12am
godlike

I thought the problem was something else entirely, didn't pay attn to his string decs
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 26 2012 11:56am
Quote (Eep @ Oct 26 2012 06:12am)
godlike

I thought the problem was something else entirely, didn't pay attn to his string decs


not god like; just common sense. its the first thing i picked up on as well.

@op: declare your arrays to wordlength + 1 then use strlen to find the length of the string or use a while loop checking for '\0'.

ex: hangman would be an array of 8;

0 = h
1 = a
2 = n
3 = g
4 = m
5 = a
6 = n
7 = '\0' //C automanticall puts this here.

so your declaration would be

Code
words[8];


varrients of this declaration include

Code
words[] = "hangman"; //automatically sets the array elements for you compile time.


and for your for loops i suggest switch them to

Code
#include <strings.h>
for(h = 0; h < strlen(words); h++)


or personally my favorite

Code
char *q = words;
while(*++q != '\0')


id just stick with the for loop and since i assume you are going to want to loop through this project and use a wordlist eventually id declare the words[n] size to be around 26 simply because there is no 26 letter words in a dictionary.

edit:: hint when you do go to change words out to another during your program if you copy a shorter word then your last word you will have fragments of the word at the end. i suggest using a call to memcpy. ex:

Code
memcpy(words, '\0', sizeof(words));


the windows api funciton zeromemory() works as well but youd have to include windows.h which is allot of overhead for such a simple program.

This post was edited by AbDuCt on Oct 26 2012 12:08pm
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Oct 26 2012 02:01pm
All right I see what you guys are saying. So it would appear that when the for loop setting the '*' runs out of room in the array word2[] it prints the last one in the first position of array word1[]. which is weird but that HAS to be what is happening...right?

By the way I really appreciate the help guys.

This post was edited by Nom_Nomz on Oct 26 2012 02:02pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 26 2012 06:57pm
Quote (Nom_Nomz @ Oct 26 2012 04:01pm)
All right I see what you guys are saying.  So it would appear that when the for loop setting the '*' runs out of room in the array word2[] it prints the last one in the first position of array word1[].  which is weird but that HAS to be what is happening...right?

By the way I really appreciate the help guys.


that totally depends on how the OS maps your arrays memory. if they are mapped next to eachother yea that could be whats happening. basically you are running over the memory limits of one array and falling into anothers.
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Oct 26 2012 08:23pm
Ok. Thanks so much guys everything is working now. See you around!
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll