954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

battleship error

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define MAX_ROWS 10
#define MAX_COLS 10
static char map[10][10];
void sin1(char map[][MAX_COLS]); 
void sin2(void);
void sin3(char map[][MAX_COLS]);
int main(void)
{  
   system("chcp 1253 >nul ");
   FILE *fileptr;
  
   char myColchar,apantisi,map[MAX_ROWS][MAX_COLS];
   int metritis1=0,apotelesma,myColnum=1,myrow=1,k,a,l,c,row, col;
   char r,fileRow[MAX_COLS + 2]; // account for newline and NULL
   
   printf("dose tin diataksi me ton arithmo pou epithymeis(1/2): \n");
   scanf("%d",&k);//edo tha iparxei sinartisi pou tha pairnei to k os parametro kai tha exei void epistrofi...
   while (k!=1 && k!=2){
         printf("ekanes lathos dose ksana tin diataksi me ton arithmo pou epithymeis(1/2): \n");
         scanf("%d",&k);}
   if (k==1){
        fileptr = fopen("1.txt", "r");}
   else if (k==2){
        fileptr=fopen("2.txt","r");}     
   else {
        printf("de dosate sosto\n");}
   if (fileptr == NULL)
      {
           perror("File Open Error");
           return -1;
      }
   else if (fileptr!=NULL){
   printf(" A B C D E F G H I J\n");   
   printf("--------------------- \n");   
   for (row = 0; row < MAX_ROWS; row++)
      {  
         fgets(fileRow, sizeof(fileRow), fileptr);
         
         printf("|");
         for (col = 0; col < MAX_COLS; col++){
              
              map[row][col] = fileRow[col];
              if (map[row][col]=='.'){
                                   map[row][col]=' ';}
              printf("%c",map[row][col]);
              printf("|");}
              printf("%d",row+1);
         
              printf("\n");
              printf("--------------------- \n");     
      }}
      
   printf("\nse periptosi pou ekteleite voli patiste <<N>> allios <<O>>: \n");
   printf("Ektelesi Volis (N/O): \n");
   scanf("%s",&apantisi);
   if (apantisi=='N'){
                     sin2();
                                             
                           
                                                                 

                     

                    
                    

                     
                     }
   else if (apantisi=='O'){
                     sin3(map);       
                     sin1(map);                                                    
                    
                          }
   
   
   fclose(fileptr);
   
   system("pause");
// now you have the map loaded,

      // so do the rest of your program
   return 0;
}
//*****************************************************************************************************************************
void sin1(char map[][MAX_COLS])
{    
     int row,col;
     for (row = 0; row < MAX_ROWS; row++)
      {  
          printf("|");
          for (col = 0; col < MAX_COLS; col++){
              printf("%c",map[row][col]);
              printf("|");}
          printf("%d",row+1);
          printf("\n");
              printf("--------------------- \n");     
      }
}

// *************************************************************************************************************************
void sin2(void)
{
                     int metritis1=0,l,c,apotelesma;                
                     
                     srand ( time(NULL) ); 
                     l = rand() % 10;
                     c = rand() % 10;
                     c=65+c;
                     l=l+1;
                     printf("Katastasi: Ektelesi Volis\n");
                     printf("sintetagmenes ekserxomenis volis: %c%d\n",c,l);
                     printf("Apotelesma ektelestheisas volis(Patiste <<1>> gia eystoxi kai <<2>> gia astoxi)\n");
                     printf("Voli %c %d: \n",c,l);
                     scanf("%d",&apotelesma);
                     while (apotelesma!=1 && apotelesma!=2){
                           printf("Apotelesma ektelestheisas volis(Patiste <<1>> gia eystoxi kai <<2>> gia astoxi)\n");
                           printf("Voli %c %d: \n",c,l);
                           scanf("%d",&apotelesma);}
                     if (apotelesma==1){
                                        metritis1++;
                                        }
                     else if (apotelesma==2){
                          }
}                         

//****************************************************************************************************************************

void sin3(char map[][MAX_COLS])
{
                    char myColchar;
                    int myColnum,myrow,col,row;
                    
                     printf("Katastasi: Lipsi Volis\n");
                     printf("Sintetagmenes eiserxomenis Volis: \n");
                     scanf("%s %d", &myColchar,&myrow);
                     myColnum = (int)myColchar;
                     if(myColnum >= 97){
                          myColnum = myColnum - 97;}
                     else if((myColnum >=65) && (myColnum <=90)){
                          myColnum = myColnum - 65;}
                       col = myColnum;
                       row = myrow - 1; // Need to compensate because array starts at 0.      
                     if (map[row][col]==' '){
                          map[row][col]='X';
                          printf("Apotelesma eiserxomenis volis: Anepityxis\n"); }               
                     else if (map[row][col]=='Ð'){
                          map[row][col]='ð';}                          
                     else if (map[row][col]=='Í'){ 
                          map[row][col]='í'; } 
                     else if (map[row][col]=='Ê'){     
                          map[row][col]='ê';}
                     else if (map[row][col]=='Ö'){  
                          map[row][col]='ö';}
                     else if (map[row][col]=='Õ'){     
                          map[row][col]='õ';}  
                     if (map[row][col]!='X'){
                          printf("Apotelesma eiserxomenis volis: Epityxis\n");}        
}


the compiler doesn't show any errors but if the user enters 1 and then O then the function sin3 has something that causes error window that says that the memory is not able to read....can u tell me what is wrong?i mean i tried it too many times but i cant see what is wrong...all i want is tha the sin3 function will determine whether or not there is a ship in the map[row][col] and then change the map[row][col] with another symbol...but also i want when the sin3 ends that the changes will be applied to the map array to use them later in the program....can u tranform my code?

mariosbikos
Light Poster
40 posts since Apr 2010
Reputation Points: 17
Solved Threads: 0
 

what i just need is that whatever happens in the function sin3 and transforms array to change the current array and print it with sin1(map) in main program.....can my sin3 function return the map?

mariosbikos
Light Poster
40 posts since Apr 2010
Reputation Points: 17
Solved Threads: 0
 

shouldn't it be entered as "O" and then "1" since the column is the letter, and in the scanf, your column letter is being entered first.

i see that there can easily be confusion with this because your array is structured such that the row is indicated first, not the column. in the traditional game, the coordinates are called out like "A 1" or "B 2" with the column letter in the first position.

but in any event, even if it is being entered correctly, "O" is the 15th letter of the alphabet (column index = 14), and you dont have that big of an array

or am i missing something?

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

i am kinda lost too can u change my code which is up and make it correct cause i am confused....

mariosbikos
Light Poster
40 posts since Apr 2010
Reputation Points: 17
Solved Threads: 0
 
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define MAX_ROWS 10
#define MAX_COLS 10
void sin1(char map[][MAX_COLS]); 
void sin2(void);
int main(void)
{  
   system("chcp 1253 >nul ");
   FILE *fileptr;
  
   char myColchar,apantisi,map[MAX_ROWS][MAX_COLS];
   int metritis1=0,apotelesma,myColnum=1,myrow=1,k,a,l,c,row, col;
   char r,fileRow[MAX_COLS + 2]; // account for newline and NULL
   
   printf("dose tin diataksi me ton arithmo pou epithymeis(1/2/3): \n");
   scanf("%d",&k);//edo tha iparxei sinartisi pou tha pairnei to k os parametro kai tha exei void epistrofi...
   while (k!=1 && k!=2 && k!=3){
         printf("ekanes lathos dose ksana tin diataksi me ton arithmo pou epithymeis(1/2): \n");
         scanf("%d",&k);}
   if (k==1){
        fileptr = fopen("1.txt", "r");}
   else if (k==2){
        fileptr=fopen("2.txt","r");}     
   else if (k==3){
        fileptr=fopen("3.txt","r");}
   if (fileptr == NULL)
      {
           perror("File Open Error");
           return -1;
      }
   else if (fileptr!=NULL){
   printf(" A B C D E F G H I J\n");   
   printf("--------------------- \n");   
   for (row = 0; row < MAX_ROWS; row++)
      {  
         fgets(fileRow, sizeof(fileRow), fileptr);
         
         printf("|");
         for (col = 0; col < MAX_COLS; col++){
              
              map[row][col] = fileRow[col];
              if (map[row][col]=='.'){
                                   map[row][col]=' ';}
              printf("%c",map[row][col]);
              printf("|");}
              printf("%d",row+1);
         
              printf("\n");
              printf("--------------------- \n");     
      }}
      
   printf("\nse periptosi pou ekteleite voli patiste <<N>> allios <<O>>: \n");
   printf("Ektelesi Volis (N/O): \n");
   scanf("%s",&apantisi);
   if (apantisi=='N'){
                     sin2();
                                             
                           
                                                                 

                     

                    
                    

                     
                     }
   else if (apantisi=='O'){
                     
                           
                      printf("Katastasi: Lipsi Volis\n");
                      printf("Sintetagmenes eiserxomenis Volis: \n");
                      scanf("%s %d", &myColchar,&myrow);
                      
                      myColnum = (int)myColchar;
                      if(myColnum >= 97){
                          myColnum = myColnum - 97;}
                      else if((myColnum >=65) && (myColnum <=90)){
                          myColnum = myColnum - 65;}
                       col = myColnum;
                       row = myrow - 1; // Need to compensate because array starts at 0.      
                      if (map[row][col]==' '){
                          map[row][col]='X';
                          printf("Apotelesma eiserxomenis volis: Anepityxis\n"); }               
                      else if (map[row][col]=='Ð'){
                          map[row][col]='ð';}                          
                      else if (map[row][col]=='Í'){ 
                          map[row][col]='í'; } 
                      else if (map[row][col]=='Ê'){     
                          map[row][col]='ê';}
                      else if (map[row][col]=='Ö'){  
                          map[row][col]='ö';}
                      else if (map[row][col]=='Õ'){     
                          map[row][col]='õ';}  
                      if (map[row][col]!='X'){
                          printf("Apotelesma eiserxomenis volis: Epityxis\n");}                     
                      sin1(map);                                                    
                    
                          }
   
   
   fclose(fileptr);
   
   system("pause");
// now you have the map loaded,

      // so do the rest of your program
   return 0;
}

void sin1(char map[][MAX_COLS])
{    
     int row,col;
     for (row = 0; row < MAX_ROWS; row++)
      {  
          printf("|");
          for (col = 0; col < MAX_COLS; col++){
              printf("%c",map[row][col]);
              printf("|");}
          printf("%d",row+1);
          printf("\n");
              printf("--------------------- \n");     
      }
}


void sin2(void)
{
                     int metritis1=0,l,c,apotelesma;                
                     
                     srand ( time(NULL) ); 
                     l = rand() % 10;
                     c = rand() % 10;
                     c=65+c;
                     l=l+1;
                     printf("Katastasi: Ektelesi Volis\n");
                     printf("sintetagmenes ekserxomenis volis: %c%d\n",c,l);
                     printf("Apotelesma ektelestheisas volis(Patiste <<1>> gia eystoxi kai <<2>> gia astoxi)\n");
                     printf("Voli %c %d: \n",c,l);
                     scanf("%d",&apotelesma);
                     while (apotelesma!=1 && apotelesma!=2){
                           printf("Apotelesma ektelestheisas volis(Patiste <<1>> gia eystoxi kai <<2>> gia astoxi)\n");
                           printf("Voli %c %d: \n",c,l);
                           scanf("%d",&apotelesma);}
                     if (apotelesma==1){
                                        metritis1++;
                                        }
                     else if (apotelesma==2){
                          }
}

i want in the upper code to put the else if(apantisi='O') loop contents to a function ...can anyone help?and give the how?(pliz change my code)

mariosbikos
Light Poster
40 posts since Apr 2010
Reputation Points: 17
Solved Threads: 0
 
i am kinda lost too can u change my code which is up and make it correct cause i am confused....

that's not how it works here

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

i know but i really cant make it up in my mind....so even an example would help i mean in my mind my code is right but windows say no.....

mariosbikos
Light Poster
40 posts since Apr 2010
Reputation Points: 17
Solved Threads: 0
 
i know but i really cant make it up in my mind....so even an example would help i mean in my mind my code is right but windows say no.....

If I were looking for help on an English programming forum, I'd be sure to translate my program into English, to help them understand it.

Have you considered helping us to help you?

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 
in my mind my code is right but windows say no.....


You want to check your scanf() usage. Most of them seem to be right but some are dangerously wrong.
For example;

// A single char
char apantisi;
...
// '%s' expects an array of char (i.e. a string), not a single char 
scanf("%s",&apantisi);

Instead that should be

// A single char
char apantisi;
// Use '%c' for a single char 
scanf("%c", &apantisi);

Fixing all these scanf() errors (i.e. scanf()ing a single char) should get rid of your program's crashes.

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

actually i think you are wrong.the error is not there if i change it instead it cant even scan at allo for a character.so the real error is in the sin3 function but i cant get it exactly....guys shall i translate my code to english?would that be easier to help me...?

mariosbikos
Light Poster
40 posts since Apr 2010
Reputation Points: 17
Solved Threads: 0
 
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define MAX_ROWS 10
#define MAX_COLS 10
static char map[10][10];
void sin1(char map[][MAX_COLS]); 
void sin2(void);
void sin3(char map[][MAX_COLS]);
int main(void)
{  
   system("chcp 1253 >nul ");
   FILE *fileptr;
  
   char myColchar,apantisi,map[MAX_ROWS][MAX_COLS];
   int proti,metritis1=0,apotelesma,myColnum=1,myrow=1,k,a,l,c,row, col;
   char r,fileRow[MAX_COLS + 2]; // account for newline and NULL
   
   printf("give the ship structure you want(1/2/3): \n");
   scanf("%d",&k);//edo tha iparxei sinartisi pou tha pairnei to k os parametro kai tha exei void epistrofi...
   while (k!=1 && k!=2 && k!=3){
         printf("you made a mistake please give me again(1/2): \n");
         scanf("%d",&k);}
   if (k==1){
        fileptr = fopen("1.txt", "r");}
   else if (k==2){
        fileptr=fopen("2.txt","r");}     
   else if (k==3){
        fileptr=fopen("3.txt","r");}
   if (fileptr == NULL)
      {
           perror("File Open Error");
           return -1;
      }
   else if (fileptr!=NULL){
   printf(" A B C D E F G H I J\n");   
   printf("--------------------- \n");   
   for (row = 0; row < MAX_ROWS; row++)
      {  
         fgets(fileRow, sizeof(fileRow), fileptr);
         
         printf("|");
         for (col = 0; col < MAX_COLS; col++){
              
              map[row][col] = fileRow[col];
              if (map[row][col]=='.'){
                                   map[row][col]=' ';}
              printf("%c",map[row][col]);
              printf("|");}
              printf("%d",row+1);
         
              printf("\n");
              printf("--------------------- \n");     
      }}
      
   printf("\nif you are the one who fires press N or else O \n");
   printf("shot fire? (N/O): \n");
   scanf("%s",&apantisi);
   if (apantisi=='N'){
                     proti=2; 
                     sin2();
                                              
                           
                                                                 

                     

                    
                    

                     
                     }
   else if (apantisi=='O'){
                     proti=3;
                     sin3(map);
                                                                         
                    
                          }
   
   
         
   fclose(fileptr);
   
   system("pause");
// now you have the map loaded,

      // so do the rest of your program
   return 0;
}
//*****************************************************************************************************************************
void sin1(char map[][MAX_COLS])
{    
     int row,col;
     for (row = 0; row < MAX_ROWS; row++)
      {  
          printf("|");
          for (col = 0; col < MAX_COLS; col++){
              printf("%c",map[row][col]);
              printf("|");}
          printf("%d",row+1);
          printf("\n");
              printf("--------------------- \n");     
      }
}

// *************************************************************************************************************************
void sin2(void)
{
                     int metritis1=0,l,c,apotelesma;                
                     
                     srand ( time(NULL) ); 
                     l = rand() % 10;                     
                     c = rand() % 10;
                     
                     c=65+c;
                     l=l+1;
                     printf("Condition:Open Fire\n");
                     printf("coordinates of firing shot: %c%d\n",c,l);
                     printf("result of shot(Press <<1>> if you found target or <<2>> if you didn't)\n");
                     printf("Shot %c %d: \n",c,l);
                     scanf("%d",&apotelesma);
                     while (apotelesma!=1 && apotelesma!=2){
                           printf("result of shot(Press <<1>> if you found target or <<2>> if you didn't)\n");
                           printf("Shot %c %d: \n",c,l);
                           scanf("%d",&apotelesma);}
                     if (apotelesma==1){
                                        metritis1++;
                                        }
                     else if (apotelesma==2){
                          }
}                         

//****************************************************************************************************************************

void sin3(char map[][MAX_COLS])
{
                     char myColchar;
                     int myColnum=1,myrow,col,row;
                    
                     printf("Condition :Incoming shots\n");
                     printf("Coordinates of incoming shot: \n");
                     scanf("%s %d", &myColchar,&myrow);
                     myColnum = (int)myColchar;
                     if(myColnum >= 97){
                          myColnum = myColnum - 97;}
                     else if((myColnum >=65) && (myColnum <=90)){
                          myColnum = myColnum - 65;}
                       col = myColnum;
                       row = myrow - 1; // Need to compensate because array starts at 0.      
                     if (map[row][col]==' '){
                          map[row][col]='X';
                          printf("Result of incoming: Unsuccessful\n"); }               
                     else if (map[row][col]=='Ð'){
                          map[row][col]='ð';}                          
                     else if (map[row][col]=='Í'){ 
                          map[row][col]='í'; } 
                     else if (map[row][col]=='Ê'){     
                          map[row][col]='ê';}
                     else if (map[row][col]=='Ö'){  
                          map[row][col]='ö';}
                     else if (map[row][col]=='Õ'){     
                          map[row][col]='õ';}  
                     if (map[row][col]!='X'){
                          printf("Result of incoming shot:Success\n");}        
}


here is in english i just need to change the sin3 function cause in there something is wrong and the memory of system suffers.....but i dont get it if i use the sin3 function contents and put the in the if statement in the main program it works well...instead when i want to create the sin3 it does not....

mariosbikos
Light Poster
40 posts since Apr 2010
Reputation Points: 17
Solved Threads: 0
 

in the sin3 function can i return the new array map contents to the main program?if yes how?

mariosbikos
Light Poster
40 posts since Apr 2010
Reputation Points: 17
Solved Threads: 0
 
here is in english i just need to change the sin3 function cause in there something is wrong and the memory of system suffers


Couldit be ..

scanf("%s %d", &myColchar,&myrow);

What do you think?
;)

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

i think that this is ok with the scanf...it read the char exactly....i have donw this before...the problem is why the sin3 function doesn;t work....

mariosbikos
Light Poster
40 posts since Apr 2010
Reputation Points: 17
Solved Threads: 0
 
i think that this is ok with the scanf...it read the char exactly....i have donw this before...the problem is why the sin3 function doesn;t work....

Alright, now please understand that what you are doing regarding scanf()/a char , is just downright wrong (you are corrupting the program's memory, believe it or not). If you don't fix those issues, simply expect most anything in terms of your program's behaviour/outcome.

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

Could it be ..

scanf("%s %d", &myColchar,&myrow);

What do you think? ;)

Mariosbikos -- slow down, please! You're ignoring perfectly good advice like the one above, from mitrmkar. myColchar is a char, and a char can never be a string, since it can't have enough room for an end of string char: '\0'.

So fix your scanf()'s, and leave the other stuff alone for now. Using scanf() for char's, is trickier than it looks, so I'm going to teach you how to do that, correctly.

When you enter anything with scanf(), you hit enter key at the end, so the keyboard buffer (KB), has a newline on the end of it.

For number scanf() (%d), that's no problem, because scanf() will pass right over any newline it runs into from the KB.

For char's, it's a WAY different story. The scanf() see's a '\n' (newline char), and say's "OK, I've got my char, leaving now", and it looks like your scanf() line of code was skipped entirely.

So, the thing to do is to pull the newline char's, after every scanf(), (when you have a char scanf() somewhere in the program), with a getchar(). Each getchar() will remove ONE char, and if you use it like this, it will do the job:

char ch;
int n;

scanf("%d", &n); //we get an int, but left a newline in the KB
getchar();     //we pull the newline char off the KB
scanf("%c", &ch); //now we can get our char variable, correctly


Without the getchar(), the second scanf(), will get "skipped" and ch will have nothing but a newline char in it's value.

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

Wow you were right....this was really the problem....one more question if i want to get first the char and then the number insted....how could that happen?i mean i cant have

getchar(); //we pull the newline char off the KB
scanf("%c", &ch);
scanf("%d", &n);
can i?

mariosbikos
Light Poster
40 posts since Apr 2010
Reputation Points: 17
Solved Threads: 0
 
char myColchar;
                     int myColnum,myrow,col,row;
                    
                     printf("Katastasi: Lipsi Volis\n");
                     printf("Sintetagmenes eiserxomenis Volis: \n");
                     scanf("%d", &myrow);
                     getchar();
                     scanf("%c", &myColchar);
                     
                     myColnum = (int)myColchar;
                     printf("myColnum is %d\n",myColnum);
                     if(myColnum >= 97){
                          myColnum = myColnum - 97;}
                     else if((myColnum >=65) && (myColnum <=90)){
                          myColnum = myColnum - 65;}
                     col = myColnum;
                     row = myrow - 2;


and there is another error with %c you guys say....in the upper part of the code if i put myColchar as A or B or anything then the myColnum gets the price 10 insted of 65 for A and 66 for B etc.....what can i do for this?before the change with scanf it worked but now it does not....why?

mariosbikos
Light Poster
40 posts since Apr 2010
Reputation Points: 17
Solved Threads: 0
 

never mind i found wheri was wrong thnx guys u really helped me....!!!!
yet there is still the problem how can the user enters firstly the char and then the number with the same result.....any thoughts?

mariosbikos
Light Poster
40 posts since Apr 2010
Reputation Points: 17
Solved Threads: 0
 
never mind i found wheri was wrong thnx guys u really helped me....!!!! yet there is still the problem how can the user enters firstly the char and then the number with the same result.....any thoughts?

If you have one getchar() after each scanf(), then you know that there is no newline still in the KB (unless the player's cat walked across the keyboard ;) )

So getting the char first, will be no trouble:

scanf("%c", &ch);

and if you're next scanf() is for a number, then youdon't need any

getchar(); //not needed at all
scanf("%d", &number);

because scanf() will pull off the newline char, and keep right on going, to find the number behind it, in the KB.

It's a bit confusing, but you get used to it - but it shows one big reason why scanf() is not a good choice for a real robust user input function.

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: