Thread: Batleship Program, Can't Get it correct

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    3

    Batleship Program, Can't Get it correct

    I just received my homework which was battleship program. Started it a few days ago but my code seems having some logical error and i can't really find it. Can anybody fix it for me? Whenever the second time i entered, the program will just pop out: no such coordinate and i can't fix it. Help me

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <ctype.h>
    
    int main()
    {
    
        int i, j;
        auto char row = 'A';
        int seas;
        int seaIndex;
        char sea[50];
     
    
    
        for(i = 0; i < 50; i++)
        {
            sea[i] = '~';
    
        }
    
    
    
        for(j=0; j<10; j++){
        printf("    1      2      3      4      5\n\n");
        for(i = 0; i < 50; i++)
                            {
                                if(i%5 == 0)
    
                                    printf("%c", row++);
                                    printf("   %c   ", sea[i]);
    
    
                                if(i%5 == 4)
                                {
                                    printf("\n");
                                }
    
                            }
    
                                printf("\n\nCoordinate Sir: ");
                                scanf("%c %d", &row, &seas);
                                fflush(stdin);
    
                                row = toupper(row) - 'A';
    
                                if(row < 0 || row > 9 || seas < 1 || seas > 5)
                                {
                                    printf("\nNO SUCH COORDINATE!");
                                    continue;
                                }
    
    
    
                                    seaIndex = (row * 5) + (seas - 1);
    
                                   if(sea[seaIndex] == 'X')
                                   {
                                        printf("YOU ALREADY SHOOT HERE");
                                        continue;
                                   }
    
    
                                    sea[seaIndex] = 'X';
    
    
    
    }
    }

  2. #2
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    Someone's going to complain about your use of fflush(stdin). That apart, I think you should set "row" to 'A' each time you start a new cycle.

    Code:
    for( j=0; j<10; ++j ) {
        row = 'A';
    
        printf("    1      2      3      4      5\n\n");
        /* etc... */
    To save one line of code, you could write the same thing this way:

    Code:
    for( row='A', j=0; j<10; row='A', ++j ) {
        printf("    1      2      3      4      5\n\n");
        /* etc. */
    Moreover, you could improve indenting for the sake of readability. The output of your program would be more readable too, should you "clear" the console every time you enter a new cycle and make a better use of '\n'. Try it!

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Also, "row = toupper(row) - 'A'" does not necessary convert 'a', 'b', .... into digits 0,1,....

    It relies on the uppercase letters being sequential values. Not all character sets on all systems support that (although, admittedly, character sets that do are in pretty common use, there are real-world systems where that is not the case).

    Given that you're mapping to values 0-9, it might be better to read digits and subtract '0'. Unlike letters, characters that represent decimal digits are guaranteed contiguous in all current character sets.

    The auto keyword on line 11 is redundant .... all variables defined within a function (i.e. not defined outside a function) have the auto attribute by default.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    Quote Originally Posted by aldo_baldo View Post
    Someone's going to complain about your use of fflush(stdin). That apart, I think you should set "row" to 'A' each time you start a new cycle.

    Code:
    for( j=0; j<10; ++j ) {
        row = 'A';
    
        printf("    1      2      3      4      5\n\n");
        /* etc... */
    To save one line of code, you could write the same thing this way:

    Code:
    for( row='A', j=0; j<10; row='A', ++j ) {
        printf("    1      2      3      4      5\n\n");
        /* etc. */
    Moreover, you could improve indenting for the sake of readability. The output of your program would be more readable too, should you "clear" the console every time you enter a new cycle and make a better use of '\n'. Try it!

    nope...still no hope...everytime i entered the second time, it will just show me...

    no such coordinate...i think there's something wrong in my if decision? but i can't figure it out

    Batleship Program, Can't Get it correct-mjkwhra-jpg

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    The problem lies herein the basic issues with improperly using scanf() and it's ilk.

    Change you scanf() call to:
    Code:
                                scanf(" %c %d", &row, &seas); /* Note the additional leading space */
    Now watch as your code magically works correctly now

    The issues is that a '\n' (as entered when you hit ENTER after entering your coordinates) is a valid character to fit inside %c. Because your scanf call never consumes the trailing \n after each call to it, only the first one successfully reads a letter. The rest of the calls just get munged up by the \ns and other characters in the buffer (numbers are valid characters too..). Read the FAQ about consuming \n properly when using scanf() but the better alternative is to simply use the fgets() + sscanf() combo and check for ERANGE while you're at it (when reading in numeric values)

  6. #6
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    Quote Originally Posted by nonpuz View Post
    The problem lies herein the basic issues with improperly using scanf() and it's ilk.

    Change you scanf() call to:
    Code:
                                scanf(" %c %d", &row, &seas); /* Note the additional leading space */
    Now watch as your code magically works correctly now

    The issues is that a '\n' (as entered when you hit ENTER after entering your coordinates) is a valid character to fit inside %c. Because your scanf call never consumes the trailing \n after each call to it, only the first one successfully reads a letter. The rest of the calls just get munged up by the \ns and other characters in the buffer (numbers are valid characters too..). Read the FAQ about consuming \n properly when using scanf() but the better alternative is to simply use the fgets() + sscanf() combo and check for ERANGE while you're at it (when reading in numeric values)
    Oh man. Thank you. You fixed my code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program seems correct, but it's miscalculating??
    By cheer4BAS in forum C++ Programming
    Replies: 3
    Last Post: 01-15-2014, 12:08 PM
  2. please help me correct this program
    By loogan211 in forum C Programming
    Replies: 5
    Last Post: 02-09-2013, 11:53 PM
  3. correct this program anyone please....
    By jothimani1991 in forum C Programming
    Replies: 3
    Last Post: 01-08-2013, 06:49 PM
  4. another little program: is this correct?
    By fsx in forum C Programming
    Replies: 7
    Last Post: 05-19-2009, 07:50 AM
  5. Is this program correct ?
    By broli86 in forum C Programming
    Replies: 9
    Last Post: 08-02-2008, 06:01 PM