Computer Science Canada Printing array |
Author: | Tubs [ 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++
|
Author: | Tubs [ Mon Nov 14, 2005 11:56 am ] | ||
Post subject: | |||
I dont seem to be able to edit my posts for some reason, quick change:
instead of 1. |
Author: | wtd [ Mon Nov 14, 2005 1:04 pm ] | ||
Post subject: | |||
Arrays in C are indexed starting at zero. When you declare an array:
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? |
Author: | Tubs [ Mon Nov 14, 2005 4:59 pm ] |
Post 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). |
Author: | wtd [ Mon Nov 14, 2005 5:39 pm ] | ||||
Post 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.
Then if you want to mark 'a' guessed, for instance...
|
Author: | Tubs [ Mon Nov 14, 2005 5:59 pm ] |
Post 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 ![]() |
Author: | [Gandalf] [ Mon Nov 14, 2005 6:00 pm ] |
Post subject: | |
Mmmm... it is confusing as to whether this is C or C++ we are talking about. Isn't int main() C++ convention? |
Author: | Tubs [ Mon Nov 14, 2005 6:03 pm ] |
Post 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++. |
Author: | [Gandalf] [ Mon Nov 14, 2005 6:05 pm ] |
Post subject: | |
Exists, but is it convention? ![]() |
Author: | Tubs [ Mon Nov 14, 2005 6:20 pm ] |
Post subject: | |
It is there when I boot up my IDE so I don't care ![]() |
Author: | Tubs [ Mon Nov 14, 2005 6:23 pm ] |
Post subject: | |
I'm pretty sure the error is just some stupid syntax in the for statement since just printf'ing a single cell works. |
Author: | wtd [ Mon Nov 14, 2005 6:47 pm ] |
Post 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. |
Author: | md [ Mon Nov 14, 2005 6:48 pm ] |
Post subject: | |
Can you post the question so we can get a better idea of what it asks? |
Author: | Tubs [ Mon Nov 14, 2005 6:48 pm ] |
Post 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? |
Author: | Tubs [ Mon Nov 14, 2005 6:52 pm ] |
Post 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. |
Author: | wtd [ Mon Nov 14, 2005 6:54 pm ] | ||
Post subject: | |||
Well, if you need 26 '8' characters in a string.
|
Author: | Tubs [ Mon Nov 14, 2005 6:57 pm ] |
Post subject: | |
It isn't supposed to have anything to do with strings, since that is the next chapter in the book. |
Author: | wtd [ Mon Nov 14, 2005 6:59 pm ] |
Post subject: | |
What course are you taking where they're using C as an introductory language? |
Author: | Tubs [ Mon Nov 14, 2005 7:04 pm ] |
Post subject: | |
First year university programming. I have turing background from highschool though. |
Author: | wtd [ Mon Nov 14, 2005 7:08 pm ] |
Post subject: | |
Wow. That's an awful university. |
Author: | Tubs [ Mon Nov 14, 2005 7:13 pm ] |
Post subject: | |
Thanks. Thats not helping my problem though. |
Author: | wtd [ Mon Nov 14, 2005 7:14 pm ] |
Post subject: | |
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. |
Author: | Tubs [ Mon Nov 14, 2005 7:15 pm ] |
Post subject: | |
I posted the problem if you would look. |
Author: | wtd [ Mon Nov 14, 2005 7:26 pm ] |
Post subject: | |
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. |
Author: | Tubs [ Mon Nov 14, 2005 7:31 pm ] |
Post subject: | |
That wasn't that problem at all, mostly I was just confused on why my for statement was not working. |
Author: | wtd [ Mon Nov 14, 2005 7:35 pm ] |
Post subject: | |
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. |
Author: | Tubs [ Mon Nov 14, 2005 7:37 pm ] |
Post subject: | |
It is the first time I have written arrays in C and I overlooked it... let it go. |
Author: | wtd [ Mon Nov 14, 2005 7:39 pm ] |
Post subject: | |
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. |
Author: | Tubs [ Mon Nov 14, 2005 7:44 pm ] |
Post subject: | |
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. |
Author: | wtd [ Mon Nov 14, 2005 7:59 pm ] |
Post subject: | |
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? |
Author: | Andy [ Mon Nov 14, 2005 8:21 pm ] |
Post subject: | |
umm is it me or does he have i >= 26?? shouldnt it be i <= 26? cuz it'd never enter the for loop ![]() |
Author: | wtd [ Mon Nov 14, 2005 8:24 pm ] |
Post subject: | |
Nice catch. For some reason I saw "<=". |
Author: | Andy [ Mon Nov 14, 2005 8:25 pm ] |
Post subject: | |
if he changed that, his original code would've worked lol.. it would be bad code, but it would work haha |
Author: | md [ Mon Nov 14, 2005 8:32 pm ] |
Post subject: | |
From the text of hte problem it seems like a rather poorly thought out assignment... and from the way the OP took to wtd's comments methinks he should perhaps try being a little more open to help (and critisism). |
Author: | Tubs [ Tue Nov 15, 2005 1:07 am ] |
Post subject: | |
I am perfectly open to criticism but criticising my school is unnecessary. I know for a fact UW intro to programming is C++... |
Author: | md [ Tue Nov 15, 2005 1:24 am ] |
Post subject: | |
Tubs wrote: I am perfectly open to criticism but criticising my school is unnecessary. I know for a fact UW intro to programming is C++... well then you've got your facts wrong... being a UW student in CS, and knowing for a fact that java (or scheme, depending) are used for into, with C++ not even being taught until 3rd year (pretty sure... haven't seen ti yet in second year at least). |
Author: | Tubs [ Tue Nov 15, 2005 1:35 am ] |
Post subject: | |
My friend is in first year at UW and he doesn't stop complaining about C++ and he has no programming background. |
Author: | wtd [ Tue Nov 15, 2005 1:36 am ] |
Post subject: | |
Tubs wrote: I am perfectly open to criticism but criticising my school is unnecessary. I know for a fact UW intro to programming is C++...
What you're being taught is not C++. It's C. They are distinct languages. |
Author: | wtd [ Tue Nov 15, 2005 1:43 am ] |
Post subject: | |
Tubs wrote: My friend is in first year at UW and he doesn't stop complaining about C++ and he has no programming background.
Neither C nor C++ are good languages to teach to beginners. It'd be somewhat acceptable if there weren't better choices, but there are plenty. |
Author: | Andy [ Tue Nov 15, 2005 3:36 am ] |
Post subject: | |
umm what program is he in? if hes in system design, or mechatronics or mechanical, he'd be doing c++, if they're in electrical or computer he'd be doing c#, and if they're in math, its either scheme or java. o and cornflakes, you can take c++ starting 2A if you follow through their stupid chart on the website |
Author: | Tubs [ Tue Nov 15, 2005 11:33 am ] |
Post subject: | |
Mechatronics, the crazy bastard. |
Author: | Tubs [ Tue Nov 15, 2005 11:57 am ] | ||
Post subject: | |||
Anyway, in a different but related assignment I had to print a 25x25 array of X's that is read by a .txt file (not the assignment, but that is my task for right now). The thing is, every 25th character is some gibberish and I can't figure out why.
|
Author: | md [ Tue Nov 15, 2005 12:44 pm ] |
Post subject: | |
Problem 1 is that your array is too small... the number in the [] brackets is the number of elements in the array, not a bound. For a 25 character matrix it'd be [25][25]. |
Author: | Tubs [ Tue Nov 15, 2005 1:09 pm ] | ||
Post subject: | |||
Ok I changed the array size (again ![]()
|
Author: | Tubs [ Tue Nov 15, 2005 1:11 pm ] | ||
Post subject: | |||
Why can't I edit my posts? ![]() Here is the code for the above output:
|
Author: | wtd [ Tue Nov 15, 2005 3:03 pm ] |
Post subject: | |
Are you certain that your input file is properly formatted? Turn on visible whitespace in your text editor to check. I know Textpad has this feature. Not sure if other text editors on Windows do. The reason this might be a problem is that your while loop which reads in the file doesn't do any error checking. It assumes the file will be absolutely perfectly formatted. |
Author: | md [ Tue Nov 15, 2005 3:04 pm ] | ||||
Post subject: | |||||
Tubs wrote: Why can't I edit my posts?
![]() Here is the code for the above output:
** note how much more readable the cleaned up code is... There are a couple of problems I see, notably when x == 24 you should still have one character left in the array to read; only when x == 25 should you be at a new line. But the question begs to be asked: if you know what you're inputing then why are you using the while loop? Why not use a much simpler method since the data is guarunteed to be correct. For example
Yes... it does absolutely zero error checking, but from what I understand your input is guarunteed anyways. |
Author: | wtd [ Tue Nov 15, 2005 3:39 pm ] |
Post subject: | |
To post working code or not... that is the dilemna. It might help you to see working code and see what 'm doing that makes it work well, but then, you might just copy it and claim it as your own without attempting to gain understanding. |
Author: | Tubs [ Tue Nov 15, 2005 5:03 pm ] |
Post subject: | |
What would be the point of that, if I don't learn it I'm screwed in future courses aren't I ![]() |
Author: | Tubs [ Tue Nov 15, 2005 5:09 pm ] |
Post subject: | |
The reason it was printing jibberish is because i had <= instead of just <, it works now. Thanks a lot guys ![]() |
Author: | Hikaru79 [ Tue Nov 15, 2005 5:58 pm ] |
Post subject: | |
Tubs wrote: The reason it was printing jibberish is because i had <= instead of just <, it works now. Thanks a lot guys
![]() ...that's what wtd has been telling you since his first post ![]() |
Author: | Tubs [ Fri Nov 18, 2005 12:08 pm ] | ||
Post subject: | |||
I can't figure out why this code only prints up to the last line of the array! The text file is a 25x25 grid of characters.
|
Author: | Tubs [ Fri Nov 18, 2005 12:09 pm ] |
Post subject: | |
Sorry, ROWS and COLS are both defined as 25. |
Author: | md [ Fri Nov 18, 2005 4:12 pm ] |
Post subject: | |
First, if you have access to a debugger check to see if the array is being read properly. If not then that's probably the problem, if it is then I see no reason why it's not working... |