Hello, I have a problem to print the whole string in argv[4]. here's my code

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define XMAX 65
#define YMAX 32
char screen[XMAX][YMAX];
void printScreen(void);
void fill(int, int, char[]);
int main(int argc, char *argv[])
{ FILE *in;
int x, y,z;
char a;
if (argc!= 5)
{ fprintf(stderr,"Usage:\n\t%s <65x32 text file> <x> <y> <letter>\n",argv[0]);
exit(1);
}
in = fopen(argv[1],"r");
if (in == NULL)
{ perror("opening input file");
exit(1);
}
for (y=0; y<YMAX; y++)
{ for (x=0; x<XMAX; x++)
screen[x][y] = fgetc(in);
}
if (fclose(in))
{ perror("closing input file");
exit(1);
}
printScreen();
x = atoi(argv[2]);
y = atoi(argv[3]);
a = argv[4][0];

fill(x,y,a);
printScreen();
}
void printScreen(void)
{ int x,y;

for (y=0; y<YMAX; y++)
for (x=0; x<XMAX; x++)
if(screen[x][y]!= EOF)
putchar(screen[x][y]);
else
puts("\n");

}
void fill(int x, int y, char a)
{
if ((screen[x][y] != ' '))
return;

screen[x][y] = a;
fill(x,y+1,a);
fill(x,y-1,a);
fill(x+1,y,a);
fill(x-1,y,a);
}

In my code, i only able to print a character. How shoulld i change my code so that i can print the whole string in argv[4]. If i use argv[4][0], then it will only print the first element of the string in argv[4]

Salem commented: Your idea of indentation sucks. -1

variable a should be declared as char*

char* a;

variable a should be declared as char*

char* a;

If u don't mind could you please explain more. I'm not good in pointer. Which char a should i change? Is it in function prototype or i should change all of them? OK. Thanks

argv is a two-diminsional array of strings. char c is only one character, not a pointer to a character array. you need to declare it as char* a in the 3d line following the declaration of main().

argv is a two-diminsional array of strings. char c is only one character, not a pointer to a character array. you need to declare it as char* a in the 3d line following the declaration of main().

int main(int argc, char *argv[])
{ FILE *in;
int x, y;
char *a;
if (argc!= 5)
{ fprintf(stderr,"Usage:\n\t%s <65x32 text file> <x> <y> <letter>\n",argv[0]);
exit(1);
}
in = fopen(argv[1],"r");
if (in == NULL)
{ perror("opening input file");
exit(1);
}
/* Read the 65x32 text file */
for (y=0; y<YMAX; y++)
{ for (x=0; x<XMAX; x++)
screen[x][y] = fgetc(in);
/* To bypass the extra newline character */
}
if (fclose(in))
{ perror("closing input file");
exit(1);
}
printScreen();
x = atoi(argv[2]);
y = atoi(argv[3]);
a = argv[4];
 
fill(x,y,*a);
printScreen();
}

I've changed my code like the above but it still print first character of argv[4]. May I know why?

remove the star from the fill function (3d line from the bottom). you should really use [ code=c ] tags so your posted program will have line numbers and you don't have to manually add those color tags.

also, post the function printscreen(), I have no idea what it is.

Also, to add to what AD mentions, format your code so we can read it.

remove the star from the fill function (3d line from the bottom). you should really use [ code=c ] tags so your posted program will have line numbers and you don't have to manually add those color tags.

also, post the function printscreen(), I have no idea what it is.

If i removed the star, the program will give me an error.

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

#define XMAX 65
#define YMAX 32

char screen[XMAX][YMAX];

void printScreen(void);

void fill(int, int, char);

 
int main(int argc, char *argv[])
{
    FILE *in;
    int x, y;
    char *a;
 
   if (argc!= 5)
   {
    fprintf(stderr,"Usage:\n\t%s <65x32 text file> <x> <y> <letter>\n",argv[0]);
    exit(1);
   }
 
   in = fopen(argv[1],"r");
   if (in == NULL)
  { 
      perror("opening input file");
      exit(1);
  }

 
   for (y=0; y<YMAX; y++){
      for (x=0; x<XMAX; x++)
        screen[x][y] = fgetc(in);
  }

 
   if (fclose(in))
  { 
       perror("closing input file"); 
       exit(1);
  }
 
   printScreen();
   x = atoi(argv[2]);
   y = atoi(argv[3]);
   a = argv[4];
 

   fill(x,y,*a);

   printScreen();
}

 
void printScreen(void)
{
     int x,y;
 
    for (y=0; y<YMAX; y++)
     for (x=0; x<XMAX; x++)
       if(screen[x][y]!= EOF)
          putchar(screen[x][y]);
      else
          puts("\n");
 
}

 
void fill(int x, int y, char a)
{
   if ((screen[x][y] != ' '))
     return;
 

   screen[x][y] = a;
   fill(x,y+1,a);
   fill(x,y-1,a);
   fill(x+1,y,a);
   fill(x-1,y,a);
}

you have changed that fill function -- your original code took a character array as the second argument, now the latest code only takes a single character. With this code you need to put that star back in.

Hooo boy....

int main(int argc, char *argv[])
{
...
   fill(x,y,*a);    // remove the *, you will pass in the array

   printScreen();
}

...

void fill(int x, int y, char a)  // Add the *, so the function knows it's a pointer
{
   if ((screen[x][y] != ' '))
     return;
 

   screen[x][y] = a;  // now that it's a pointer, you figure out how to 
   fill(x,y+1,a);     //    do what you want here, especially since you
   fill(x,y-1,a);     //    have no comments explaining what this is
   fill(x+1,y,a);     //    supposed to accomplish.
   fill(x-1,y,a);
}

Actually the output of the program will be like this

[IMG]http://img172.imageshack.us/img172/9789/outputbangangmh2.th.jpg[/IMG]

If we choose I as argv[4] then I will fill up the inner part of the output. What i want to make here is instead of character, I want string to fill up the output like 'WOW'. That's the problem I encountered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.