Thread: int minus the amount of rand()

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    2

    int minus the amount of rand()

    So this is the begging of a text-adventure game so all the text is just story line ( and a lot have been removed but no important part ).

    But the problem is that i want "liv" ( life ) should be permanently changed after the attack.
    printf("Du har %d i liv\n", liv-rand() % 4);
    What i want to happen is that you get hit 5 times ( looped 5 times ) and hurt you a random amount between 0 -4 ( rand() % 4) ;

    liv ( life ) is hundred from the start and then
    you lose 3 health
    you lose 2 health
    you lose 1 health
    you lose 2 health
    you lose 2 health

    You now have 90 liv ( life )

    The code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    {
    	char namn[20];
    	float svarhet;
    	int liv, pengar;
    	int forsta, vakt;
    	int i;
    	int menu;	 
    
    	printf("Välkomen till puzzlet\n");
    	printf("Vänligen skriv in din pesudonym\n"); //stava
    	scanf( "%s", namn);
    	printf("du valdet namnet %s\n\n", namn);
    
    	printf("välj svårighetsgrad 1 eller 2\n");
    	printf("1=svår\n");
    	printf("2=lätt\n");
    	scanf("%d", &menu );
    	switch( menu ) {
    		case 1: liv = 100; pengar = 1000; printf("du valde den svåra"); break;
    		case 2: liv = 200; pengar = 2000; printf("Du valde det lätta spåret, mes"); break;
    		default: printf("Invalid option selected\n");
    		}
    
    	printf("Låt oss börja leken\n");
    	printf("Info\n");
    	printf("#################\n");
    	printf("# Liv %d       #\n", liv );
    	printf("# Pengar %d   #\n", pengar );
    	printf("#################\n\n");
    
    	printf(" Liv %d Pengar %d                    \n", liv, pengar);   
    
    	int max = 5;
    	int d = 0;
    	int t;
    
    	printf("skriv 1\n"); //stava
    	scanf( "%d", &t);
    	if (t == 1 ) 
    	
    	srand ( time(NULL) );
        	for(d = 0; d < max;d++){
      		srand ( time(NULL) );
    
    
     		printf("Du har %d i liv\n", liv-rand() % 4);
            printf("%d\n",d); // vilken loop vi är på
    	
    	}
    
    	 		printf("\n\n\nDu har %d i liv\n", liv);
    	
    }

    Hope someone got what i mean do Englis is far away from my native language.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then remember: srand shall be called once and once only.
    Then you use rand() to get a random number.
    Last edited by Elysia; 08-09-2010 at 07:45 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
        	for(d = 0; d < max;d++){
            liv -= rand() % 4;                           
            printf("Du har %d i liv\n",liv);          // now liv after hit
            printf("%d\n",d); // vilken loop vi är på
    	
    	}

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    2
    Thank you all, fixed it right up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM