unused variable causes lots of problems

This is a discussion on unused variable causes lots of problems within the C Programming forums, part of the General Programming Boards category; im writing a battleship game.. and i have 2 problems first, here is one of the functions: Code: #include <stdio.h>#include ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    15

    unused variable causes lots of problems

    im writing a battleship game.. and i have 2 problems
    first,
    here is one of the functions:

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    void battle(char p1_base[10][10],char p1_enemy[10][10],char p2_base[10][10],char p2_enemy[10][10]);
    int chkzero(int qty[5]);
    int setShip(char p_base[10][10],char p_enemy[10][10],int player);
    int mode_select(char p1_base[10][10], char p1_enemy[10][10], char p2_base[10][10],char p2_enemy[10][10]);
    void pressEnter();
    void setBoard(char p_base[10][10], char p_enemy[10][10]);
    void rowcol(char *row,int *col);
    void printBoard1(char p_base[10][10], char p_enemy[10][10]);
    void message(char msg[50]);
    int chkboard(char board[10][10]);
    void reset_qty(int qty[5]);
    void sunk(char p_base[10][10], int done[5]);
    int main(){
        char p1_base[10][10],p1_enemy[10][10],p2_base[10][10],p2_enemy[10][10];
        int exit=0;
    
    
        printf("--Battleship C!--\n");
        printf("What would you like to do?\n\n");
        int choice;
        printf(" \n_____________\n");
        printf("|--Main Menu--|\n");
        printf("|             |\n");
        printf("|[1]Game Start|\n");            //print main menu
        printf("|[2]Logout    |\n");
        printf(" ~~~~~~~~~~~~~\n\n");
    
    
        do{
            printf("Enter Choice: ");
            scanf("%d",&choice);
            getchar();
            if (choice==1){
                exit=mode_select(p1_base,p1_enemy,p2_base,p2_enemy);
            }
            else if (choice==2){
                return 0;
            }
            else printf("Choice invalid!");
        }while(choice!=2 && choice!=1);
        if(exit==0){main();}
        return 0;
    }
    void rowcol(char *row,int *col){
        do{
            printf("Enter row (A-J): ");
            scanf("%s",row);
            if(*row<'A' || *row>'J'){
            printf("\nA-J only. Capital letters.\n");
            }
        }while(*row<'A' || *row>'J');
        do{
            printf("Enter col (1-10): ");
            scanf("%*[^0-9]%d",col);
            getchar();
            if(*col<1 || *col>10){
            printf("\n1-10 only.\n");
            }
        }while(*col<1 || *col>10);
    }
    void sunk(char p_base[10][10], int done[5]){
        int i,j,h;
        char ship[5][20]={"Aircraft Carrier","Battleship","Cruiser","Destroyer","Submarine"};
        int count[5]={0,0,0,0,0};
    
    
        for(i=0;i<10;i++){
            for(j=0;j<10;j++){
                for(h=0;h<5;h++){
                    if(p_base[i][j] == ship[h][0]){
                        count[h]++;
                    }
                }
            }
        }
        for(i=0;i<5;i++){
            printf("%d    %c   %d    %d\n",count[i],ship[i][0],done[i],i);
        }
        for(i=0;i<3;i++){
            if (count[i]==0){
                if(done[i]==0){
                    printf("\nThe %s has been sunk!\n",ship[i]);
                    done[i]=1;
                }
            }
        }
        if(count[4]==1 && done[4]==0){
            printf("\na Submarine has sunk!\n");
            done[4]=1;
        }
        if(count[4]==0 && done[5]==1){
            printf("\n Another Submarine has sunk!\n");
            done[5]=1;
        }
    }
    void battle(char p1_base[10][10],char p1_enemy[10][10],char p2_base[10][10],char p2_enemy[10][10]){
        int col,player=1,round,valid=1,hit=3;
        int done1[5]={0,0,0,0,0}; //valid, 1; invalid; 0
        char row, *msghit[30]={"Nice! Guess again!","Excellent! Try again!","Great! Go for another one!","Awesome! One more for you!","A hit! One more!","Keep it going! More!","Amazing! Another!","Keep it up! Go!"};
        char *msgmiss[30]={"Miss!","Awwwwwww! You missed!","Try again!","Ooops! miss","Oh men!","Don't give up!","You hit nothing!"};
        int done2[5]={0,0,0,0,0};
    
    
        printf("\n\n.\n\n.\n\n.\n\n.\n\n.\n\n.\n\n.\n\n.                        IT'S BATTLE TIME!!\n\n.\n\n");
        printf("\n--LEGEND--\n%c -> hit\n%c -> miss\n\n",(char)206,'X');
        printBoard1(p2_enemy,p1_enemy);
        printf("\n\n.\n\n.\n\n.\n\n.\n\n.\n\n.\n\n.\n\n");
        for(round=0;chkboard(p1_enemy)==0 && chkboard(p2_enemy)==0;){
            if (valid==1){
                if(hit==1){
                    printBoard1(p2_enemy,p1_enemy);
                    sunk(p1_base,done1);
                    sunk(p2_base,done2);
                    message(msghit[rand()%8]);
    
    
                }
                else if (hit==0){
                printBoard1(p2_enemy,p1_enemy);
                message(msgmiss[rand()%7]);
                }
                if (player==1 && hit==0){
                    printf("\n\n-- Round %d --\n\n",round+1);
                }
            printf("  - Player %d -\n",player);
            }
    
    
                rowcol(&row,&col);
                switch(player){
                    case 1: if(p2_base[row-'A'][col-1]=='~'){
                            p1_enemy[row-'A'][col-1]='X';
                            p2_base[row-'A'][col-1]='X';
                            valid=1;
                            hit=0;
                            }
                        else if(p1_enemy[row-'A'][col-1]!='~'){
                            printf("\nYou have already hit that spot. Enter a different target.\n");
                            if(player==1) player=2;
                            else if(player ==2) player =1;
                            valid=0;
                            hit=0;
                            break;
                        }
                        else{
                            p1_enemy[row-'A'][col-1]=(char)206;
                            p2_base[row-'A'][col-1]=(char)206;
                            valid=1;
                            hit=1;
                        }
                    break;
                    case 2: if(p1_base[row-'A'][col-1]=='~'){
                            p2_enemy[row-'A'][col-1]='X';
                            p1_base[row-'A'][col-1]='X';
                            valid=1;
                            hit=0;
                        }
                        else if(p2_enemy[row-'A'][col-1]!='~'){
                            printf("\nCannot print there. Enter a different target.\n");
                            if(player==1) player=2;
                            else if(player ==2) player =1;
                            valid=0;
                            hit=0;
                            break;
                        }
                        else{
                            p2_enemy[row-'A'][col-1]=(char)206;
                            p1_base[row-'A'][col-1]=(char)206;
                            valid=1;
                            hit=1;
                        }
                        if(hit==0){
                            round++;
                        }
                    break;
                }
                if(player==1 && hit==0) {player=2;}
                else if(player==2 && hit==0) {player =1;}
        }
        if(player==1) {player=2;}
        else if(player ==2) {player =1;}
        printBoard1(p2_enemy,p1_enemy);
        printf("\n\n\n You sunk all of his ships! Player %d wins!\n\n\n",player);
        pressEnter();
        printBoard1(p1_base,p2_base);
        pressEnter();
    }
    int chkzero(int qty[5]){
        int i;
        for (i=0;i<5;i++){
            if (qty[i]==0) continue;
            else return 0;
        }
        return 1;
    }
    int setShip(char p_base[10][10],char p_enemy[10][10],int player){
        int i,qty[5]={1,1,1,2,2},choice,hist[7],count=0,count2=0;
        char row,row_temp,  direction[50], ship;
        char Ship[5][20]={"Aircraft Carrier","Battleship","Cruiser","Destroyer","Submarine"};
        int col,col_temp ,size, valid=0;
    
    
            reset_qty(qty);
            do{
                printf("\n\n    #       ship         size\n");
                printf("\n[1] x%d %s   5\n\n",qty[0],Ship[0]);
                printf("[2] x%d %s         4\n\n",qty[1],Ship[1]);
                printf("[3] x%d %s            3\n\n",qty[2],Ship[2]);
                printf("[4] x%d %s          2\n\n",qty[3],Ship[3]);
                printf("[5] x%d %s          1\n",qty[4],Ship[4]);
                printf("_________________________________\n");
                printf("[6] Undo\n");
                printf("[7] Reset\n");
                printf("[8] Quit\n");
                do{
                    do{
                        printf("Enter choice: ");
                        scanf("%d",&choice);
                        getchar();
                        if(choice<9 && choice>0 && qty[choice-1]==0){
                                printf("No more %s. Please choose another.\n",Ship[choice-1]);
                                continue;
                        }
                    }while(qty[choice-1]==0 && choice<9 && choice>0);
                    switch (choice){
                        case 1:
                            printf("\n-- %s --\n\n",Ship[0]);
                            ship='A';
                        break;
                        case 2:
                            printf("\n-- %s --\n\n",Ship[1]);
                            ship='B';
                        break;
                        case 3:
                            printf("\n-- %s --\n\n",Ship[2]);
                            ship='C';
                            break;
                        case 4:
                            printf("\n-- %s --\n\n",Ship[3]);
                            ship='D';
                            break;
                        case 5:
                            printf("\n-- %s --\n\n",Ship[4]);
                            ship='S';
                            break;
                        case 6:
                            printf("\n-- Undo --\n\n");
                            if(count==0){
                                if (player==2){
                                    printBoard1(p_enemy,p_base);
                                }
                                else{
                                printBoard1(p_base,p_enemy);
                                }
                                printf("\nNothing to undo.\n");
                                break;
                            }
                            count2++;
                            if(count2>1){
                                if (player==2){
                                    printBoard1(p_enemy,p_base);
                                }
                                else{
                                printBoard1(p_base,p_enemy);
                                }
                                printf("\nSorry. You can undo only once.\n");
    
    
                                break;
                            }
                            for(i=0;p_base[row-'A'][col-1]==ship;i++){
                                    p_base[row-'A'][col-1]='~';
                                    if(strcmp(direction,"up")==0){
                                        row++;
                                    }
                                    else if(strcmp(direction,"down")==0){
                                        row--;
                                    }
                                    else if(strcmp(direction,"left")==0){
                                        col++;
                                    }
                                    else if(strcmp(direction,"right")==0){
                                        col--;
                                    }
                                 }
                                switch (ship){
    
    
                                    case 'A':
                                        qty[0]++;
                                    break;
                                    case 'B':
                                        qty[1]++;
                                    break;
                                    case 'C':
                                        qty[2]++;
                                    break;
                                    case 'D':
                                        qty[3]++;
                                    break;
                                    default:qty[4]++;
                                    break;
                                }
                                if (player==2){
                                    printBoard1(p_enemy,p_base);
                                }
                                else{
                                printBoard1(p_base,p_enemy);
                                }
                                count--;
                            break;
                        case 7:
                            printf("\n-- Reset --\n\n");
                            setBoard(p_base,p_enemy);
                            reset_qty(qty);
                            count=0;
                            break;
                        case 8:
                            printf("\n\nReturning to Mode to Select...\n\n\n");
                            return 1;
                            break;
                        default: printf("Choice invalid!\n");
                        break;
    
    
                    }
                }while(choice>7 || choice <1);
                if (choice!=6 &&choice!=7){
                    count2=0;
                    printf("\nPlace Head: \n");
                    rowcol(&row,&col);
                    size=6-choice;
                if (choice!=5){
                    do{
                        printf("\nFrom head to (up/down/left/right): ");
                        scanf("%s", direction);
                        getchar();
                        valid=1;
                        if(strcmp(direction,"up")==0){
                            row_temp=row;
                            for(i=0;i<size;i++){
                               if( p_base[row-'A'][col-1] != '~' || row-'A'<0){
                                   if (player==2){
                                    printBoard1(p_enemy,p_base);
                                }
                                else{
                                printBoard1(p_base,p_enemy);
                                }
                                   printf("Cannot print there.\n");
                                 break;
                               }
                            row--;
                            }
                            row=row_temp;
                            if(i==size){
                                for(i=0;i<size;i++){
                                    p_base[row-'A'][col-1]= ship;
                                    row--;
                                }
                                    row++;
                                    count++;
                                    qty[choice-1]--;
                                    if (player==2){
                                    printBoard1(p_enemy,p_base);
                                    }
                                    else{
                                    printBoard1(p_base,p_enemy);
                                    }
                            }
    
    
                        }
                        else if(strcmp(direction,"down")==0){
                            row_temp=row;
                            for(i=0;i<size;i++){
                               if( p_base[row-'A'][col-1] != '~' || row-'A'>9){
                                   if (player==2){
                                    printBoard1(p_enemy,p_base);
                                }
                                else{
                                printBoard1(p_base,p_enemy);
                                }
                                   printf("Cannot print there.\n");
                                 break;
                               }
                            row++;
                            }
                            row=row_temp;
                            if(i==size){
                                for(i=0;i<size;i++){
                                    p_base[row-'A'][col-1]= ship;
                                    row++;
                                }
                                    row--;
                                    count++;
                                    qty[choice-1]--;
                                    if (player==2){
                                    printBoard1(p_enemy,p_base);
                                    }
                                    else{
                                    printBoard1(p_base,p_enemy);
                                    }
                            }
                        }
                        else if(strcmp(direction,"left")==0){
                            col_temp=col;
                            for(i=0;i<size;i++){
                               if( p_base[row-'A'][col-1] != '~' || col-1<0){
                                   if (player==2){
                                    printBoard1(p_enemy,p_base);
                                    }
                                    else{
                                    printBoard1(p_base,p_enemy);
                                    }
                                   printf("Cannot print there.\n");
                                 break;
                               }
                            col--;
                            }
                            col=col_temp;
                            if(i==size){
                                for(i=0;i<size;i++){
                                    p_base[row-'A'][col-1]= ship;
                                    col--;
                                }
                                    col++;
                                    count++;
                                    qty[choice-1]--;
                                    if (player==2){
                                    printBoard1(p_enemy,p_base);
                                    }
                                    else{
                                    printBoard1(p_base,p_enemy);
                                    }
                            }
                        }
                        else if(strcmp(direction,"right")==0){
                            col_temp=col;
                            for(i=0;i<size;i++){
                               if( p_base[row-'A'][col-1] != '~' || col-1>9){
                                   if (player==2){
                                    printBoard1(p_enemy,p_base);
                                    }
                                    else{
                                    printBoard1(p_base,p_enemy);
                                    }
                                   printf("\nCannot print there.\n");
                                 break;
                               }
                            col++;
                            }
                            col=col_temp;
                            if(i==size){
                                for(i=0;i<size;i++){
                                    p_base[row-'A'][col-1]= ship;
                                    col++;
                                }
                                    col--;
                                    count++;
                                    qty[choice-1]--;
                                    if (player==2){
                                    printBoard1(p_enemy,p_base);
                                    }
                                    else{
                                    printBoard1(p_base,p_enemy);
                                    }
                            }
                        }
                        else {
                                printf("invalid input!");
                                valid=-1;
                        }
    
    
                    }while(valid!=1);
                }
                else {
                   if( p_base[row-'A'][col-1] != '~'){
                       if (player==2){
                        printBoard1(p_enemy,p_base);
                        }
                        else{
                        printBoard1(p_base,p_enemy);
                        }
                       printf("\nCannot print there.\n");
                   }
                    else{
                        p_base[row-'A'][col-1]=ship;
                        count++;
                        qty[choice-1]--;
                        if (player==2){
                        printBoard1(p_enemy,p_base);
                        }
                        else{
                        printBoard1(p_base,p_enemy);
                        }
                    }
                }
            }
                if (choice==7){
                        if (player==2){
                            printBoard1(p_enemy,p_base);
                            }
                            else{
                            printBoard1(p_base,p_enemy);
                            }            choice=0;
                }
            }while(chkzero(qty)==0);
            return 0;
    }
    int mode_select(char p1_base[10][10], char p1_enemy[10][10], char p2_base[10][10],char p2_enemy[10][10]){
        int player,choice;
        do{
            printf("\n--Mode Select--\n");
            printf("\n[1]Single Player\n");
            printf("[2]Multiplayer\n");
            printf("[3]Go back\n");
            printf("\nEnter Choice: ");
            scanf("%d", &choice);
            getchar();
               switch(choice){
                    case 1:
                        return 0;
                        break;
                    case 2:
                        player=1;
                        printf("\n\n.\n.\n.\n.\n.\n--Player %d's Turn -- \n.\n.\n.\n.\n.\n",player);
                        setBoard(p1_base,p1_enemy);
                        printf("\n\n                        -Set your Ships- \n\n\n");
                        printBoard1(p1_base,p1_enemy);
                        if(setShip(p1_base,p1_enemy,player)==1){
                            continue;
                        }
                        printf("\n\nPlayer 1's ships have been deployed... ");
                        pressEnter();
                            player=2;
                            printf("\n.\n.\n.\n --Player %d's Turn -- \n.\n.\n.\n\n",player);
                            setBoard(p2_enemy,p2_base);
                            printf("\n\n                        -Set your Ships- \n\n\n");
                            printBoard1(p2_enemy,p2_base);
                            if(setShip(p2_base,p2_enemy,player)==1){
                            continue;
                            }
                            printf("\n\nAll ships have been deployed...\n");
                            pressEnter();
                            battle(p1_base,p1_enemy,p2_base,p2_enemy);
    
    
                        return 0;
                        break;
                    case 3:
                        return 0;
                        break;
                    default:printf("Choice invalid!");
                        break;
               }
            }while(choice!=2 && choice!=1);
        return 0;
    }
    
    
    
    
    void pressEnter(){
        char c;
        printf("\npress enter to continue... ");
        c=getchar();
        while (c != '\n' && c != EOF){
        c=getchar();
        }
    }
    void setBoard(char p_base[10][10], char p_enemy[10][10]){
        int row,col;
        int x=128;
        for (row=0;row<10;row++){
            for(col=0;col<10;col++){
                p_base[row][col]='~';
                p_enemy[row][col]='~';
                x++;
            }
        }
    }
    void printBoard1(char p_base[10][10], char p_enemy[10][10]){
        int row,col;
        printf("             Player 1                            Player 2\n\n");
    for(row=0;row<10;row++){
            printf("%c",(char)row+65);
            for(col=0;col<10;col++){
                printf("%3c",p_base[row][col]);
            }
            printf("  |||");
            for(col=0;col<10;col++){
                printf("%3c",p_enemy[row][col]);
            }
            printf("  %c",(char)row+65);
            printf("\n\n");
        }
        printf(" ");
        for(col=1;col<=10;col++){
            printf("%3d",col);
        }
        printf("     ");
        for(col=1;col<=10;col++){
            printf("%3d",col);
        }
        putchar('\n');
    }
    
    
    void message(char msg[50]){
        printf("\n\n.\n\n.\n\n.\n\n.\n%s\n.\n\n.\n\n.\n\n.\n\n.\n\n",msg);
    }
    
    
    
    
    int chkboard(char board[10][10]){
        int countx=0,i,j;
    
    
        for(i=0;i<10;i++){
            for(j=0;j<10;j++){
                if(board[i][j]=='*'){
                    countx++;
                }
            }
        }
        if (countx==18) return 1;
        else return 0;
    }
    
    
    
    
    void reset_qty(int qty[5]){
        int i;
        char reset[5]={1,1,1,2,2};
            for(i=0;i<5;i++){
                qty[i]=reset[i];
            }
    }
    Firstly, there is an unused variable hist[7] in the setShip(); and if
    i remove it causes a lot of problems, example, the choice variable
    in that function always resets the variable choice to 0 whenever i call the rowcol(), and even i fix that it will give another problem somewhere..

    Second, The sunk() function works, except for one variable...
    on the second call, done[0] doesnt change when it meets
    the conditions. but on the first call, it does..
    done1 has the same values of done 2...
    even if i change the order of the call ( if i call done2 first),
    same thing happens on the second call.. *sigh*
    i cant seem to find the problem..

    i hope somebody could help me.. I been coding since morning
    and it gave me a lot of stress..

    any kind of help is okay..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Your setShip function is way too long. Break it up into parts. It is rare for a function to be justifiably longer than 100 lines, and keeping it below 50 lines would be more normal (especially with your slightly more line-saving brace placement style).

    Now, your problem is probably because of say, an array out of bounds access somewhere. After breaking your function into smaller functions, step through the code with a debugger to find out where the problem manifests. The error probably lies in the code that is executed before that point.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what to do with unused array elements
    By droseman in forum C Programming
    Replies: 4
    Last Post: 11-05-2009, 05:30 AM
  2. How to dispose of unused characters
    By C++Newb in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2009, 09:38 PM
  3. Problems with Summing lots of doubles
    By firetheGlazer in forum C Programming
    Replies: 7
    Last Post: 07-15-2008, 04:44 AM
  4. Where do Unused function returns go?
    By indigo0086 in forum C++ Programming
    Replies: 4
    Last Post: 08-08-2006, 10:54 AM
  5. Replies: 2
    Last Post: 01-15-2002, 05:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21