
-----------------------------------
Tubs
Mon Nov 14, 2005 11:52 am

Printing array
-----------------------------------
This code looks ok to me but it just doesnt print anything? I'm probably just overlooking some stupid syntax error, can anyone help me out?

Dev c++


#include 

int main(int argc, char *argv[])
{

int length = 5, i;
char word[length]; guessed[26];

for (i = 1; i >= 26; ++i)

    {
    guessed[i]= 1;
    }

for (i = 1; i >= 26; ++i)

    {
    printf ("%c", guessed[i]);
    }


system ("pause");

  return 0;
}


-----------------------------------
Tubs
Mon Nov 14, 2005 11:56 am


-----------------------------------
I dont seem to be able to edit my posts for some reason, quick change:


guessed = '*';


instead of 1.

-----------------------------------
wtd
Mon Nov 14, 2005 1:04 pm


-----------------------------------
Arrays in C are indexed starting at zero.  When you declare an array:

char guessed[26];

You're creating n array with space for 26 elements, starting at zero and ending with index 25.  Your loop goes from index 1 to index 26.

Characters in C are ASCII characters.  What is 1 in ASCII?  Nothing that'll print to anything meaningful on the screen.  I notice you change this to '*'.  I have no idea what you're trying to acocmplish, so I can't say if this is good or not.

What are you trying to accomplish?

-----------------------------------
Tubs
Mon Nov 14, 2005 4:59 pm


-----------------------------------
Making a hangman game, where the 'guesses' array holds the user's guessed letters. It begins with all values as '*' (so says the question).

-----------------------------------
wtd
Mon Nov 14, 2005 5:39 pm


-----------------------------------
So guesses just needs to hold true or false values?

In that case, you want to use a boolean value, that can only be true or false.

#include 

int main()
{
   BOOL guesses

Then if you want to mark 'a' guessed, for instance...

guesses

-----------------------------------
Tubs
Mon Nov 14, 2005 5:59 pm


-----------------------------------
I wish.. it says specifically in the question to have '*' in the place of the letters. My text says to use something like:


char guesses[26] = {'*', '*'... etc }

but that can't be the most efficient method of doing this (besides, I have to make the game of life next which has 600 odd cells in the array). Just an example of simple declaration of a char array and printf'ing it would be awesome :D[/code]

-----------------------------------
[Gandalf]
Mon Nov 14, 2005 6:00 pm


-----------------------------------
Mmmm...  it is confusing as to whether this is C or C++ we are talking about.  Isn't int main() C++ convention?

-----------------------------------
Tubs
Mon Nov 14, 2005 6:03 pm


-----------------------------------
"]Mmmm...  it is confusing as to whether this is C or C++ we are talking about.  Isn't int main() C++ convention?

It is all in C, int main() exists in both C and C++.

-----------------------------------
[Gandalf]
Mon Nov 14, 2005 6:05 pm


-----------------------------------
Exists, but is it convention? ;)

-----------------------------------
Tubs
Mon Nov 14, 2005 6:20 pm


-----------------------------------
It is there when I boot up my IDE so I don't care :) as long as it runs.

-----------------------------------
Tubs
Mon Nov 14, 2005 6:23 pm


-----------------------------------
I'm pretty sure the error is just some stupid syntax in the for statement since just printf'ing a single cell works.

-----------------------------------
wtd
Mon Nov 14, 2005 6:47 pm


-----------------------------------
If you need to declare a char array which functions as a string, then you need to allow space for the null character, to terminate the string.

Char array strings are one reason C is evil as a teaching language.

-----------------------------------
md
Mon Nov 14, 2005 6:48 pm


-----------------------------------
Can you post the question so we can get a better idea of what it asks?

-----------------------------------
Tubs
Mon Nov 14, 2005 6:48 pm


-----------------------------------
It definately has something to do with inserting variables inside the array instead of the location of the cell. How would you print the whole array?

-----------------------------------
Tubs
Mon Nov 14, 2005 6:52 pm


-----------------------------------
Question:

Write an interactive program that plays a game of hangman. Store the word to be guessed in successive elements of an array of individual characters called word. The player must guess the letters belonging to word. The program should terminate when either all the letters have been guessed correctly (the player wins) or a specified number of incorrect guesses have been made (the computer wins). Use another array, guessed, to keep track of the solution so far. Initialize all elements of guessed to the '*' symbol. Each time a letter in word is guessed, replace the corresponding '*' in guessed with that letter.

-----------------------------------
wtd
Mon Nov 14, 2005 6:54 pm


-----------------------------------
Well, if you need 26 '8' characters in a string.

#include 
#include 

int main()
{
   char guesses

-----------------------------------
Tubs
Mon Nov 14, 2005 6:57 pm


-----------------------------------
It isn't supposed to have anything to do with strings, since that is the next chapter in the book.

-----------------------------------
wtd
Mon Nov 14, 2005 6:59 pm


-----------------------------------
What course are you taking where they're using C as an introductory language?

-----------------------------------
Tubs
Mon Nov 14, 2005 7:04 pm


-----------------------------------
First year university programming. I have turing background from highschool though.

-----------------------------------
wtd
Mon Nov 14, 2005 7:08 pm


-----------------------------------
Wow.  That's an awful university.

-----------------------------------
Tubs
Mon Nov 14, 2005 7:13 pm


-----------------------------------
Thanks. Thats not helping my problem though.

-----------------------------------
wtd
Mon Nov 14, 2005 7:14 pm


-----------------------------------
Well, without seeing the problem we have no idea what you're trying to accomplish, so we can't help you very well.  The problems could be in any one of several different areas.

-----------------------------------
Tubs
Mon Nov 14, 2005 7:15 pm


-----------------------------------
I posted the problem if you would look.

-----------------------------------
wtd
Mon Nov 14, 2005 7:26 pm


-----------------------------------
Ok, if you didn't know that arrays in C are indexed starting at zero, then I seriously suggest you go to your professor and ask for help.  Not knowing that indicates to me that you're unprepared to write that program, and trying to just jump in and make wild stabs at it is just going to confuse you further.

-----------------------------------
Tubs
Mon Nov 14, 2005 7:31 pm


-----------------------------------
That wasn't that problem at all, mostly I was just confused on why my for statement was not working.

-----------------------------------
wtd
Mon Nov 14, 2005 7:35 pm


-----------------------------------
That actually was a major problem.  If you have an array with 26 elements, the last index is 25.  You were trying to write to index 26.  That is a big problem, and it's a fundamental lack of knowledge about how arrays work in C kind of problem.

-----------------------------------
Tubs
Mon Nov 14, 2005 7:37 pm


-----------------------------------
It is the first time I have written arrays in C and I overlooked it... let it go.

-----------------------------------
wtd
Mon Nov 14, 2005 7:39 pm


-----------------------------------
What I'm saying is that it's something you should have been taught before being asked to write a program like that.  If you weren't, then there are likely other problems waiting to come back and bite you, and you should talk about it with your professor.

-----------------------------------
Tubs
Mon Nov 14, 2005 7:44 pm


-----------------------------------
Its not something I didn't know, it was simply overlooked like typing 'print' instead of 'printf'. If you are just going to criticize my school and knowledge of C instead of help, don't bother posting.

-----------------------------------
wtd
Mon Nov 14, 2005 7:59 pm


-----------------------------------
I'm not criticizing you for the 1..26 thing.  I am saying that it's such a basic thing that it suggests other problems.  I've seen lots of other people with the same issue, and in every case there have been a lot of other basic misconceptions that weren't caught early on and because of that had a tendency to snowball.

Kind of like building a rocket, and you get the whole thing constructed and put into firing position and the electrical system fully debugged before anyone bothers to point out that it should be pointing up.  ;)

As for this particular problem, you're storing a word to be guessed as a series of characters in an array.  You're then creating a second array with 26 elements.  Presumably it's not a coincidence that there are also 26 letters in the alphabet.  :)

How does having this array fit into solving the problem of creating a simple hangman game?

Do you know how to check to see if the word has been guessed, which would end the game?  

Do you know how to check to see if every possible letter has been guessed, which would also end the game?

-----------------------------------
Andy
Mon Nov 14, 2005 8:21 pm


-----------------------------------
umm is it me or does he have i >= 26?? shouldnt it be i 