Hi, I'm new to the community and to C. i have no programing background what so ever but i have been reading books on it. I am trying to use a for loop to print out the folowing
*******
(space)*******
(space)(space)*******
(space)(space)(space) *******

I wrote the program

#include <stdio.h>
int main(void)
{
char stars[]= "*******", spaces[5]=" ";
int i;
for ( i=0; i < 4; i++){
printf("%s%s\n", spaces, stars);
spaces= ' ';
spaces[i+i]=' ';

return 0;
}
}

when i try to compile it i get alot of errors and i have no idea how to fixed them. Can someone please tell me where i might have made a mistake. For some reason i cant use any of the message editor functions on this thread. the format is screwed up. i had to include (space) to show where i wanted the space to be

I wish I could get amnesia and relearn C. It was so much fun the first time, and I miss the days when things were so new and exciting!

This is your code with some comments.

#include <stdio.h>

int main(void)
{
   char stars[]= "*******", 
        spaces[5]=" "; // You are only putting a space in space[0],
                       // the rest of the array is undefined.
   int i;
   for ( i=0; i < 4; i++) {
       printf("%s%s\n", spaces, stars); // This will print "(space)*******"
       spaces[i]= ' ';  // This puts a space on top of the existing space
       spaces[i+i]=' '; // This puts a space on the space you just put a space on

       return 0; // And now we leave the program! YAY!

       
   } // The code never makes it here, so the for loop never goes past i=0
}

So it never does a loop, it puts a space on the first iteration even though you said you didn't want it, and your technique is a bit unclear. Its a great first try though! I love it!

I modified the code above to work correctly. Usually in life, the simpler you make something, the more effective it is. So I removed a few things and now it should work like you wanted.

#include <stdio.h>

int main(void)
{
   char stars[]= "*******";

   int i;
   for ( i=0; i < 4; i++) {
       printf("%s\n", stars); // Print the current state of "stars"
       stars[i]= ' ';         // Replace a single star with a space
   } 

   return 0; // I'm sure you would agree that this makes more sense here
}

thank you so much for the response. yeah after a while i realized some of my mistakes and put his together

#include <stdio.h>
  int  main(void)
{ 
  char stars[]= "*******", spaces[5]="";
  int i;
  for ( i=0; i < 4; i++){
    printf("%s%s\n", spaces, stars);
    spaces[i]= ' ';
    spaces[i+1]= '\0';
  
 
}return 0;
}

i have a question though why wasnt the spaces array

Ah, I see what you were trying to do now.

I didn't put the spaces array because I didn't think you needed it, but now I see that you just wanted to push the stars forward, not eat up the stars with spaces, so your code works better for you. I was just initially confused as to your logic.

Great work!

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.