Thread: how to pass this code in main?

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    28

    how to pass this code in main?

    Ive got a problem with this code,you see it works when it is not passed ,but when i try to pass it in main the supposed to be tree design doesnt work anymore..how do i pass this in main?(I did this previouslyin main-- int nNum=draw() but it only went up to the Enter your name thing then it all went blank spaces..wherein it was supposed to draw a tree...any suggestions pls?


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

    void center(int); // center() func prototype

    int draw();
    {
    int i, j, count = 0, times = 19;
    char x_mas = '^', *post = "||", star = '*';
    // Set x_mas var default to '^'
    char name[20];

    puts("Enter your name: ");
    gets(name);

    /* puts("Enter a symbol to represent pine spikes: ");*/
    x_mas = '$';

    for(i=0;i<20;i++)
    {
    // Block to handle 0 (star on tree)
    if(i==0 && times == 19)
    {
    center(times);
    printf("%c", star);
    }

    --times; // Keep decrementing times to make it
    center(times); // work with the index num i

    if(i == 1)
    {
    center((++times + ++times)); // times remains 18.
    printf("%c%c\n", x_mas, x_mas); // 36 spaces for index num = 1
    continue;
    }
    else if(i > 1)
    {
    for(j=0;j<i;j++)
    {
    printf("%c%c", x_mas, x_mas);
    }
    if(j == i)
    putchar('\n');
    }
    }

    times = 19;

    while(count != 2)
    {
    count++;
    center(times - 1); // Space until reach 18th x_mas char
    printf("%s\n", post); // so calculate to get 18 (times - 1)
    }

    printf("%s,WELCOME TO WHO WANTS TO BE A MILLIONAIRE GAME!\n", name);

    /* system("PAUSE");*/
    return 0;
    }

    void center(int times)
    {
    int i;
    char space = ' ';

    for(i=0;i<times;i++)
    printf("%c", space);

    printf("%c%c", space, space);

    }
    Last edited by kreyes; 03-07-2002 at 02:27 AM.
    only the fittest survive!!!

  2. #2
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    this looks better:

    Code:
    #include <stdio.h> 
    #include <string.h> 
    #include <stdlib.h> 
    
    void center(int); // center() func prototype 
    
    int draw();  /* whats this semicolon doing here? */
    { 
      int i, j, count = 0, times = 19; 
      char x_mas = '^', *post = "||", star = '*'; 
      // Set x_mas var default to '^' 
      char name[20]; 
    
      puts("Enter your name: "); 
      gets(name); 
    
      /* puts("Enter a symbol to represent pine spikes: ");*/ 
      x_mas = '$'; 
    
      for(i=0;i<20;i++) 
      { 
        // Block to handle 0 (star on tree) 
        if(i==0 && times == 19) 
        { 
          center(times); 
          printf("%c", star); 
        } 
    
        --times; // Keep decrementing times to make it 
        center(times); // work with the index num i 
    
        if(i == 1) 
        { 
          center((++times + ++times)); // times remains 18. 
          printf("%c%c\n", x_mas, x_mas); // 36 spaces for index num = 1 
          continue; 
        } 
        else if(i > 1) 
        { 
          for(j=0;j<i;j++) 
        { 
          printf("%c%c", x_mas, x_mas); 
        } 
        if(j == i) 
          putchar('\n'); 
        } 
      } 
    
      times = 19; 
    
      while(count != 2) 
      { 
        count++; 
        center(times - 1); // Space until reach 18th x_mas char 
        printf("%s\n", post); // so calculate to get 18 (times - 1) 
      } 
    
      printf("%s,WELCOME TO WHO WANTS TO BE A MILLIONAIRE GAME!\n", name); 
    
      /* system("PAUSE");*/ 
      return 0; /* this function will always return a 0, is this what you want? */
    } 
    
    void center(int times) 
    { 
      int i; 
      char space = ' '; 
    
      for(i=0;i<times;i++) 
      printf("%c", space); 
    
      printf("%c%c", space, space); 
    }
    Last edited by dharh; 03-07-2002 at 02:54 AM.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    28
    the semicolon thing ws just a typographical error ....i tried omittin return 0 but it still wont work..do u still have any ideas how could i pass it in main coz i want it to be incorporated in my over all program....?

    also i tried adding the '&' sign in printf e.g printf("%c",&star);
    it didnt genrate any more spaces but infinite no of single char garbage value.......
    Last edited by kreyes; 03-07-2002 at 03:12 AM.
    only the fittest survive!!!

  4. #4
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    This works
    Code:
    #include <stdio.h> 
    #include <string.h> 
    #include <stdlib.h> 
    
    void center(int); // center() func prototype 
    int draw(void);
    
    int main() {
      int nNum;
    
      nNum = draw();
    
    
      return 0;
    }
    
    int draw()
    { 
      int i, j, count = 0, times = 19; 
      char x_mas = '^', *post = "||", star = '*'; 
      // Set x_mas var default to '^' 
      char name[20]; 
    
      puts("Enter your name: "); 
      gets(name); 
    
      /* puts("Enter a symbol to represent pine spikes: ");*/ 
      x_mas = '$'; 
    
      for(i=0;i<20;i++) 
      { 
        // Block to handle 0 (star on tree) 
        if(i==0 && times == 19) 
        { 
          center(times); 
          printf("%c", star); 
        } 
    
        --times; // Keep decrementing times to make it 
        center(times); // work with the index num i 
    
        if(i == 1) 
        { 
          center((++times + ++times)); // times remains 18. 
          printf("%c%c\n", x_mas, x_mas); // 36 spaces for index num = 1 
          continue; 
        } 
        else if(i > 1) 
        { 
          for(j=0;j<i;j++) 
        { 
          printf("%c%c", x_mas, x_mas); 
        } 
        if(j == i) 
          putchar('\n'); 
        } 
      } 
    
      times = 19; 
    
      while(count != 2) 
      { 
        count++; 
        center(times - 1); // Space until reach 18th x_mas char 
        printf("%s\n", post); // so calculate to get 18 (times - 1) 
      } 
    
      printf("%s,WELCOME TO WHO WANTS TO BE A MILLIONAIRE GAME!\n", name); 
    
      /* system("PAUSE");*/ 
      return 0; 
    } 
    
    void center(int times) 
    { 
      int i; 
      char space = ' '; 
    
      for(i=0;i<times;i++) 
      printf("%c", space); 
    
      printf("%c%c", space, space); 
    }
    Last edited by dharh; 03-07-2002 at 11:09 AM.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    28

    Thumbs up

    It worked!!! thanks i know the problem i forgot to use the = instead of ==.
    thanks again!!!
    only the fittest survive!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 09:46 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM