Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2885
  • Last Modified:

battleship C again

(view http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21090791.html)
I have this program, and it does not put it in the correct row. please compile/run using gcc compiler(i dont know if that makes a difference). i have a feeling the program resides in getcoordinates().
the output should be:
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? 1 (input)
Enter coordinates: H7 (input)
Enter value to be written at (7, 6): 3 (input)
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? 2 (input)
0 0 0 0 0 0 x 0 0 0  (but instead, the 3 is not showing there. it is showing on the part where I put the x)
0 0 0 0 0 0 0 0 0 0   also, this statement: printf("\nmyRow = %d, myCol = %d\n", myRow, myCol);
0 0 0 0 0 0 0 0 0 0   shows as myRow = 0 myCol = 8 (help!)
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 3 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

void clearBoard(int rows, int columns, int * boardPtr);
void showBoard(int rows, int columns, int * boardPtr);
int getCoords(int maxRow, int maxCol, int *row, int *col);
void paintSpace(int rows, int cols, int * boardPtr);

void clearBoard(int rows, int columns, int * boardPtr)
{
int i;

for(i=0; i < rows * columns; i++)
   {
      *(boardPtr+i) = 0; //initialize value to 0
   }
} // End of clearBoard function.


void showBoard(int rows, int columns, int * boardPtr)
{
int i;
for(i=0; i < rows * columns; i++)
   {
   // Print newline character is row is finished.
   // If i modulus the columns is 0, then you've reached
   // the end of a row and need to print a newline.
   if(i % columns == 0)
      {
      printf("\n");
      }
   
   if ((*(boardPtr+i) >= 0) && (*(boardPtr+i) <= 9))
      {
      printf("%d ", *(boardPtr+i));
      }
   else
      {
      printf("x");
      }
   }
}

void eatWhitespace (void)
{
  char ch;
  while(isspace(ch = getchar()))
    {
      /* Do nothing. */
    }
  ungetc(ch, stdin);
}

void eatToNewline (void)
{
  while (getchar() != '\n')
    {
      /* Do nothing. */
    }
}


int getCoords(int maxRow, int maxCol, int *row, int *col)
{
char myRowchar;
 
int myCol=0;
int myRownum=0;
myRownum = (int)myRowchar;
 
eatWhitespace();

if (2 == scanf("%c %d", &myRowchar, &myCol))
{
 
  if(myRownum >= 97) /*if the row is greater than or equal to a*/  
   {
    myRownum = myRownum - 97;
     
   }
  else if((myRownum >=65) && (myRownum <=90))
    /*if row is greater than A AND less than 90*/
   {
    myRownum = myRownum - 65;
   
   }  

  if (myRownum > maxRow)
   {
    *row = 0;
    *col = 0;
    return 0;
   }
  else if (myCol > maxCol)
   {
    *row = 0;
    *col = 0;
    return 0;
   }

  else
   {
    *row = myRownum;
    *col = myCol - 1; // Need to compensate because array starts at 0.
   }
 
}
 else
   {
     eatToNewline();
   }

 return 1;
}

void paintSpace(int rows, int cols, int * boardPtr)
{
int myRow = 0, myCol = 0, value = 0;

printf("Enter coordinates :");

 
  while ( 0 == getCoords(rows,cols,&myRow,&myCol))
{
   printf("Bad coordinates!\nEnter coordinates: ");
}
  printf("\nmyRow = %d, myCol = %d\n", myRow, myCol);

printf("Enter a value: ");
scanf("%d", &value);

*(boardPtr + (myRow*cols) + myCol)  = value;


}










int main()
{
int choice;
int rows = 10;
int columns = 10;
int boardPtr[rows][columns];

while(1)
{
   printf("\n*************************************************\n");
   printf("0 - Clear the board.\n");
   printf("1 - Paint a space.\n");
   printf("2 - Show board.\n");
   printf("3 - Play game.\n");
   printf("4 - Quit.\n");
   printf("*************************************************\n");
   printf("Your choice? _");
   scanf("%d", &choice);
   
   switch(choice)
      {
      case 0:
         clearBoard(rows, columns, (int *)boardPtr);
         break;

      case 1:
       fflush(stdin);
 paintSpace(rows, columns, (int *)boardPtr);
         break;

      case 2:
         showBoard(rows, columns, (int *)boardPtr);
         break;

      case 3:
         printf("The game isn't ready yet.\n");
         break;

      case 4:
         exit(0);

      default:
         printf("Your input was invalid.\n");
         break;
      } //End of Switch statement
   } //End of while loop
 return 0;
} //End of Program
0
simmah
Asked:
simmah
  • 5
  • 4
  • 3
2 Solutions
 
avizitCommented:
in int getCoords(int maxRow, int maxCol, int *row, int *col)

---
you declare  myRowchar,

and then initialize myRownum by casting myRowchar to an int.

the scanf reads the value of myRowchar and myCol

but after that you check for values of "myRownum "  which is initialised only with the junk value thatwas present when you declare
myRowchar,.

/abhijit/
0
 
simmahAuthor Commented:
what do you suggest I do?
0
 
avizitCommented:
That would depend on what your intention is

the I pointed out is that

in funtion getCoords

you declare myRowchar --char myRowchar;

now after this line the value of "myRowchar" is not defined. It can have any junk value.
now what you do next is
myRownum = (int)myRowchar;

i.e you transfer the junk value of myRowchar to myRownum after casting it to an int.

and then later you check
if(myRownum >= 97

so basically you are comparing a junk value againts 97 , the result of which is not defnite.

-----
now what you should do , depends very much on what is your aim. I have no idea bout that. Only thing i know is you are using uninitailised value .which is wrong.









0
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
simmahAuthor Commented:
I made it an (int) because myrowchar first starts off as a character type. then I want to convert it into an int and do the calculations through it. the rows are supposed to be from A-J(1-10). therefore, 97 is decimal value for 'a' and 65 is decimal value for 'A'. if you go to the link mentioned on the initial posting, it'll show you the requirements and also the output and what it should be.
0
 
simmahAuthor Commented:
It is almost 11pm. I will check back again 8am(central US), if you need anything else. Thank you and I hope we get somewhere with this.
0
 
ankuratvbCommented:
Hi,
>myRownum = (int)myRowchar;
Put this statement after the scanf in getcoords()
It should be:

int getCoords(int maxRow, int maxCol, int *row, int *col)
{
char myRowchar;

int myCol=0;
int myRownum=0;

eatWhitespace();

if (2 == scanf("%c %d", &myRowchar, &myCol))
{
 myRownum = (int)myRowchar;
 
 if(myRownum >= 97) /*if the row is greater than or equal to a*/  
  {
   myRownum = myRownum - 97;
     
0
 
ankuratvbCommented:
when scanf() is not successful,it means bad coordinates,so make the user enter again

if (2 == scanf("%c %d", &myRowchar, &myCol))
{
 .
 .
 .
//Your code here
}
else
  {
    eatToNewline();
    return 0;
  }

Also,Right now,it accepts 0 and -ve values for myCol,put in a check so that if mycol<=0 then print bad coords and enter again.
0
 
avizitCommented:
That is fine

what is wrong is this

char myRowchar;

after this line the value of myRowchar is undefined as you havent initialized it . So it contains a junk value.

and now you use this junk value to populate myRownum  -> myRownum = (int)myRowchar;

and then you use myRownum for comparisons which actually contains junk value .....

also read up http://cplus.about.com/library/weekly/aa101902k.htm

0
 
avizitCommented:
ankuratvb is fast ( as usual :)
my response is to simmah
just mentioned lest it looks like i am responding to ankur :)

0
 
ankuratvbCommented:
Hi avizit,

if (2 == scanf("%c %d", &myRowchar, &myCol))
{
myRownum = (int)myRowchar;

This solves the problem,as myrowchar is scanf'd and if it is successful,then only,myrownum is set and the calculations done.
0
 
avizitCommented:
It does solves :) sorry I dint mean to say your solution was wrong . Your response slipped in between ..i was responding to the author.
I typed my response and was googling for reference and guess then you responded.



0
 
ankuratvbCommented:
NP :~))
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now