![]() |
|
#1
|
|||
|
|||
help in battleshiphi
I need help in battleship game. I wrote the prog for battleship but its not working properly here is my code: CPP / C++ / C Code:
the output should be like, suppose if coordinate is: 5T. The X should be like. X should be place in between 5 and T. ABCDEFGHIJKLMNOPQRSTUVWXYZ 1|~~~~~~~~~~~~~~~~~~~~~~~~~~| 2|~~~~~~~~~~~~~~~~~~~~~~~~~~| 3|~~~~~~~~~~~~~~~~~~~~~~~~~~| 4|~~~~~~~~~~~~~~~~~~~~~~~~~~| 5|~~~~~~~~~~~~~X~~~~~~~~~~~~| 6|~~~~~~~~~~~~~~~~~~~~~~~~~~| 7|~~~~~~~~~~~~~~~~~~~~~~~~~~| 8|~~~~~~~~~~~~~~~~~~~~~~~~~~| 9|~~~~~~~~~~~~~~~~~~~~~~~~~~| A|~~~~~~~~~~~~~~~~~~~~~~~~~~| B|~~~~~~~~~~~~~~~~~~~~~~~~~~| C|~~~~~~~~~~~~~~~~~~~~~~~~~~| D|~~~~~~~~~~~~~~~~~~~~~~~~~~| E|~~~~~~~~~~~~~~~~~~~~~~~~~~| F|~~~~~~~~~~~~~~~~~~~~~~~~~~| but i am getting wrong out put. which is: ABCDEFGHIJKLMNOPQRSTUVWXYZ 1|~~~~X~~~~~~~~~~~~~~~~~~~~~| 2|~~~~~~~~~~~~~~~~~~~~~~~~~~| 3|~~~~~~~~~~~~~~~~~~~~~~~~~~| 4|~~~~~~~~~~~~~~~~~~~~~~~~~~| 5|~~~~~~~~~~~~~~~~~~~~~~~~~~| 6|~~~~~~~~~~~~~~~~~~~~~~~~~~| 7|~~~~~~~~~~~~~~~~~~~~~~~~~~| 8|~~~~~~~~~~~~~~~~~~~~~~~~~~| 9|~~~~~~~~~~~~~~~~~~~~~~~~~~| A|~~~~~~~~~~~~~~~~~~~~~~~~~~| B|~~~~~~~~~~~~~~~~~~~~~~~~~~| C|~~~~~~~~~~~~~~~~~~~~~~~~~~| D|~~~~~~~~~~~~~~~~~~~~~~~~~~| E|~~~~~~~~~~~~~~~~~~~~~~~~~~| F|~~~~~~~~~~~~~~~~~~~~~~~~~~| please help me with this, don't know what to do thanx |
|
#2
|
|||
|
|||
Re: help in battleshipQuote:
Regards, Dave |
|
#3
|
|||
|
|||
Re: help in battleship2 input one int and one char. for example Coordinate 4T
i know i wrote one int cause i don't know how to do it, yeh the array will replace with X. thanks |
|
#4
|
|||
|
|||
Re: help in battleshipQuote:
You might think of reading two chars. You can do it with scanf("%c%c") or two getchar() calls. Either way, you have to know a little more about buffered input. Namely, whether you use scanf("%c") or getchar(), here's the story: When the user starts typing, nothing gets to the C program until the user presses 'Enter' at the end of his/her line. At that time all of the input is in a system buffer, from which a program gets its input data. Suppose the user presses the '5' key and the 'T' key and 'Enter'. Then there are three chars in the system buffer (the '5', the 'T' and a newline char, '\n'). Two calls to getchar (or a call to scanf with "%c%c" format specifier) will get the char '5' into program memory, the char 'T' into program memory, and it will leave the newline char in the system buffer. Then the next time a user program does its getchar() or scanf("%c.."), it will read the newline char into the user's program memory. That's not what you want (obviously). Also, if the user enters more than two chars, the rest of the line will still be in the system buffer and will screw up future program inputs. So, here's a way to read exactly two chars from the user and clear out the system buffer so that the next time through the loop it will be waiting for fresh user input. CPP / C++ / C Code:
While we are at it, let's also convert user row 0-9,A-F to integer values 0-15 to use as the row index to your array. Also user column A-Z will be converted to integer values 0-25 to be used as the column index to your array. This makes a couple of assumptions about character encoding that will serve for most computers (and all PC implementations that I know about). If the user enters either or both chars that are not a valid row or column designator for the game, I just set the integer row or column to -1 so that the program can know that it was invalid (and make sure that the program doesn't try to store something outside the bounds of the array). CPP / C++ / C Code:
Regards, Dave |
|
#5
|
|||
|
|||
Re: help in battleshipHi, Dave
thank u very much. I like the way u teach me. You should be my professor thanks but the prog doesn't compiling getting error: undeclared identifier crow undeclared identifier ccol undeclared identifier row undeclared identifier col i did declared that in main here is my code: CPP / C++ / C Code:
|
|
#6
|
|||
|
|||
Re: help in battleshipQuote:
The variables should be declared inside the function where they are used. Each function starts its own "address space", and variables declared in other functions are not visible. Regards, Dave |
|
#7
|
|||
|
|||
Re: help in battleshipQuote:
hi Dave, same thing, i am getting same errors as before, don't know why.. now i am getting more error like: char encountered int encountered thanks |
|
#8
|
|||
|
|||
Re: help in battleshipQuote:
2. Tell us what compiler and operating system you are using. 3. Post the exact compiler messages. Paste them into your post. Don't edit or delete or paraphrase any part of the messages. Regards, Dave |
|
#9
|
|||
|
|||
Re: help in battleshipQuote:
hi Dave. I am using c compiler in phobos. here is my code: CPP / C++ / C Code:
here is my error: "333.c", line 50.5: 1506-275 (S) Unexpected text 'char' encountered. "333.c", line 50.10: 1506-045 (S) Undeclared identifier crow. "333.c", line 51.5: 1506-275 (S) Unexpected text 'int' encountered. "333.c", line 51.9: 1506-045 (S) Undeclared identifier row. "333.c", line 53.26: 1506-045 (S) Undeclared identifier ccol. "333.c", line 74.9: 1506-045 (S) Undeclared identifier col. thanks Dave |
|
#10
|
|||
|
|||
Re: help in battleshipQuote:
Put all of your variable declarations before any executable code in each function block: CPP / C++ / C Code:
Regards, Dave |
Recent GIDBlog
Observations of Iraq by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [C] Battleship for NT platforms | Alhazred | C Programming Language | 4 | 20-Jul-2004 04:12 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The