Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Printing array
Index -> Programming, C++ -> C++ Help
Goto page 1, 2, 3, 4  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tubs




PostPosted: Mon Nov 14, 2005 11:52 am   Post subject: 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++

code:

#include <stdio.h>

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;
}
Sponsor
Sponsor
Sponsor
sponsor
Tubs




PostPosted: Mon Nov 14, 2005 11:56 am   Post subject: (No subject)

I dont seem to be able to edit my posts for some reason, quick change:

code:

guessed = '*';


instead of 1.
wtd




PostPosted: Mon Nov 14, 2005 1:04 pm   Post subject: (No subject)

Arrays in C are indexed starting at zero. When you declare an array:

code:
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




PostPosted: Mon Nov 14, 2005 4:59 pm   Post subject: (No subject)

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




PostPosted: Mon Nov 14, 2005 5:39 pm   Post subject: (No subject)

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.

c:
#include <stdbool.h>

int main()
{
   BOOL guesses[26];

   for (int guess_index = 0; guess_index < 26; guess_index++)
   {
      guesses[guess_index] = false;
   }

   /* ... */

   return 0;
}


Then if you want to mark 'a' guessed, for instance...

c:
guesses[letter_input - 'a'] = true;
Tubs




PostPosted: Mon Nov 14, 2005 5:59 pm   Post subject: (No subject)

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 Very Happy[/code]
[Gandalf]




PostPosted: Mon Nov 14, 2005 6:00 pm   Post subject: (No subject)

Mmmm... it is confusing as to whether this is C or C++ we are talking about. Isn't int main() C++ convention?
Tubs




PostPosted: Mon Nov 14, 2005 6:03 pm   Post subject: (No subject)

[Gandalf] wrote:
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++.
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Mon Nov 14, 2005 6:05 pm   Post subject: (No subject)

Exists, but is it convention? Wink
Tubs




PostPosted: Mon Nov 14, 2005 6:20 pm   Post subject: (No subject)

It is there when I boot up my IDE so I don't care Smile as long as it runs.
Tubs




PostPosted: Mon Nov 14, 2005 6:23 pm   Post subject: (No subject)

I'm pretty sure the error is just some stupid syntax in the for statement since just printf'ing a single cell works.
wtd




PostPosted: Mon Nov 14, 2005 6:47 pm   Post subject: (No subject)

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




PostPosted: Mon Nov 14, 2005 6:48 pm   Post subject: (No subject)

Can you post the question so we can get a better idea of what it asks?
Tubs




PostPosted: Mon Nov 14, 2005 6:48 pm   Post subject: (No subject)

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




PostPosted: Mon Nov 14, 2005 6:52 pm   Post subject: (No subject)

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.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 4  [ 54 Posts ]
Goto page 1, 2, 3, 4  Next
Jump to:   


Style:  
Search: