Thread: Please Help!

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    77

    Please Help!

    I wrote this program with user defined fuctions to roll a visual dice. I am a newbie so please tell me what am I doing wrong.

    Thanks

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    char getans();
    int randn(int n);
    void printBLine(int n);
    
    int main()
     {
       
    
       ans = getans();
       srand(time(0));
    
    
    int randn(int n)
    
       int r = 0 ;
       char ans = 0 ;
       int i = 0 ;
       int numOfBLine;
    
       while(ans== 'y')
        {
          r = rand()% 6 + 1 ;
    
          
          switch (n)
          {
            case 1 : printf("\n * \n"); 
                          break;
            case 2 : printf(" * \n");
                          printf(" * \n");   
                           break;
            case 3 : printf("*   \n");
                          printf(" *  \n");
                          printf("  * \n");  
                          break;
            case 4 :  printf(" * * \n\n");
                          printf(" * * \n"); 
                          break;
            case 5 : printf(" * * \n");
                          printf("  *  \n");
                          printf(" * * \n"); 
                         break;
            case 6 : for(i=0 ; i<3 ; i++)
                             printf(" * * \n");
                         break;
          }  /*endSwitch */
         
          printBLine();
           ans = getans();
        }
    
        /* write blank lines */
        printBLine();
         return n;
    }
    
    char getans()
     {
       int ans = -1 ;
       printf("Throw y/n ?");
       while (ans == -1)
        {
          ans = getchar( );
          ans = tolower(ans);
        }
       fflush(stdin); 
       return ans;
    }
    
    
    
    /* write blank lines */
    int printBLine(int n)
    
    	{
          int i=0;
          for(i=0 ; i< numOfBL ; i++) 
                printf("\n");
    	return n;
    	
    	}

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well what's going wrong? It makes it a lot easier to find the source of the source of the problem if we know what we're looking for.

    edit: I notice a few problems almost immediately. You're using variables out of scope. Make them global if you need to use them in multiple functions (or pass pointers to functions). You've also got problems with your curly braces. Make sure you've got a matching closing brace for every opening brace, and in the right place.
    Last edited by sean; 02-06-2005 at 08:36 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    sorry, I declared "ans" in the main function, and I closed the braces for main function.

    It still gives me error.

    says:

    r, ans, and i not initailized.

    What does this means?

    edit: And says syntax error before while

    Thanks

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If you declare all your variables in main, you won't be able to use them in any other function. Declare the variables that need to be global in the same place you put your function prototypes before main.

    In your two while loops, you first use ans as a char, and then later as a numerical value. That's going to be a problem, but in the meanwhile, ans is still being used out of scope - that'll be fixed if you follow the above paragraph.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    ok here is the code again that I fixed a little, but I am getting a error again. I declared the variables locally.

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    char getans();
    int randn(int n);
    void printBLine(int n);
    
    int main()
     {
       int dice;
       char blank;
       int ans;
       
       ans = getans();
       blank=printBLine();
       dice=randn();
       srand(time(0));
       
     }
    
    int randn(int n)
    
     {
       int r = 0 ;
       char ans = 0 ;
       int i = 0 ;
       
    
       while(ans== 'y')
        {
          r = rand()% 6 + 1 ;
    
          
          switch (n)
          {
            case 1 : printf("\n * \n"); 
                          break;
            case 2 : printf(" * \n");
                          printf(" * \n");   
                           break;
            case 3 : printf("*   \n");
                          printf(" *  \n");
                          printf("  * \n");  
                          break;
            case 4 :  printf(" * * \n\n");
                          printf(" * * \n"); 
                          break;
            case 5 : printf(" * * \n");
                          printf("  *  \n");
                          printf(" * * \n"); 
                         break;
            case 6 : for(i=0 ; i<3 ; i++)
                             printf(" * * \n");
                         break;
          }  /*endSwitch */
         
          printBLine();
           ans = getans();
        }
    
        /* write blank lines */
        printBLine();
         return n;
    }
    
    
    char getans()
    
     {
       int ans = -1 ;
       printf("Throw y/n ?");
       while (ans == -1)
     {
          ans = getchar( );
          ans = tolower(ans);
     }
       fflush(stdin); 
       return ans;
    }
    
    
    
    /* write blank lines */
    int printBLine(int n)
    
    	{
          int i=0;
          for(i=0 ; i< numOfBL ; i++) 
                printf("\n");
    	return n;
    	
    	}
    Now is says conflict with printBLine() fucntion

    thanks

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I declared the variables locally.
    Well you set ans equal to the return value of getans, but that value is never used. Probably because you're not paying any attention to the scope of your variables and what they're used for.

    As far as I can tell, the problems in your printBLine() function is that you use numOfBL, which is not defined. Do you mean to use n instead?

  7. #7
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    As sean says you need to declare numOfBL before you can use it - and declare it locally in the function.

    More glaring are you function prototypes. You have a mismatch for them. for ex:
    Code:
    void printBLine(int n);
    .....
    .....
    ....
    int printBLine(int n)
    
        {
          int i=0;
          for(i=0 ; i< numOfBL ; i++) 
                printf("\n");
        return n;
        
        }
    notice anything. Your prototype and its definition needs to match. So if you define PrintBline to return int and except an argumentyour prototype should say as such, otherwise you just confuse the compiler. The same thing for the randn function correct those first and compile again.

    Code:
     fflush(stdin);
    don't do this, as this can cause undefined behaviour and is not good coding style see the FAQ for more info.

    That is all I could see on a cursory glance.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    thanks guys....I tried that too....for some reason it still doesn't work....well anyways I am going to bed now....I give up for today ....I will work on it tommrow now.....Thanks for all the help

Popular pages Recent additions subscribe to a feed