Thread: So far so good, now what?

  1. #1
    Unregistered
    Guest

    So far so good, now what?

    I've pieced this together. Now I cannot remember how to call the correct number into the printf function. I seem to remember doing this array different the previous time in the form of a table.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    	char *insult[7]{
    		"*",
    		"*",
    		"*",
    		"*",
    		"*",
    		"*",
    		"*",
    	},
    
    	char *noun[3]{
    		"*",
    		"*",
    		"*",
    	
    	},
    
    	int random_insult()
    	 {
    		  srand((unsigned)time(NULL));
    		  return rand()%6;
    	 }
     
     	int random_noun()
     	 {
     		  srand((unsigned)time(NULL));
     		  return rand()%2;
     	 }
    
    	int main()
    	{
    		int noun_number;
    		int insult_number;
    		noun_number = do_randomnoun;
    		printf(

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    If you want to print a random insult then do this:
    Code:
    printf("%s", insult[random_insult()]);
    OR for a random noun, do this:
    Code:
    printf("%s", insult[random_noun()]);

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    - You should call srand() only once in your program.
    - Using this method to get a random number is not good:
    >return rand()%6;
    as it produces numbers that aren't very random. Try a board search and find some of Prelude's advice on this matter to get a better result.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Unregistered
    Guest
    Well first i'd like to figure out what i'm doing wrong. Then i'll tinker with the tiny bits. This is what I have now. I tried the advice with the arrays. And with the printing of the randoms and it wouldn't compile. I did this once before about 6 months ago. I seem to recall having done it similar to this, but theres some funky error going on. I'm using DJGPP. And it tells me theres a syntax error before '{', token on lines 10 and 20. Anyways this is the code.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    
    char insult;
    char noun;
    
    
    
    const struct insult_type insult_table[]{
    	"",
    	"",
    	"",
    	"",
    	"",
    	"",
    	""
    	}
    
    const struct noun_type noun_table[]{
    	"",
    	"",
    	""
    	}
    
    int random_insult()
    {
    	  srand((unsigned)time(NULL));
    	  return rand()%6;
    	}
     
    int random_noun()
    {
    	  srand((unsigned)time(NULL));
    	  return rand()%2;
       }
    
    	
    void do_insult()
    {
          printf("%s", noun[]);
          printf("%s", insult[]);
          printf("Want another?\n\r");
       }
    	
    int main()
    {
          int cnt = 0;
          char answer;
    		
          for(cnt=0;cnt<20;cnt++)
          do_insult();
          scanf("%s", &answer);
    		if (answer = 'N') !! if (answer='n'))
    		{
    			exit(0);
    		}
    	return(0);
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    const char  *insult_table[] = { "i1", "i2", "i3", "i4", "i5", "i6", "i7" };
    
    const char  *noun_table[] = { "n1", "n2", "n3" };
    
    int random_insult(void)
    {
        return(rand() % 6);
    }
    
    int random_noun(void)
    {
        return(rand() % 2);
    }
    
    void do_insult(void)
    {
        printf("%s %s\n", noun_table[random_noun()], insult_table[random_insult()]);
        printf("Want another?\n");
    }
    
    int main(void)
    {
        int     cnt;
        int     answer;
    
        for (cnt = 0; cnt < 20; cnt++)
        {
            do_insult();
            answer = getchar();
            if (answer == 'N' || answer == 'n')
            {
                break;
            }
            while (getchar() != '\n');
        }
    
        return(0);
    }
    Here, review the changes I have made.... it should get you started.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 11:30 AM
  2. Good books for learning WIN32 API
    By Junior89 in forum Windows Programming
    Replies: 6
    Last Post: 01-05-2006, 05:38 PM
  3. Good resources for maths and electronics
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-22-2004, 04:23 PM
  4. what is good for gaphics in dos
    By datainjector in forum Game Programming
    Replies: 2
    Last Post: 07-15-2002, 03:48 PM
  5. i need links to good windows tuts...
    By Jackmar in forum Windows Programming
    Replies: 3
    Last Post: 05-18-2002, 11:16 PM