time

This is a discussion on time within the C++ Programming forums, part of the General Programming Boards category; how can i make a valor to a variable like: Code: int x; x = 0; ... ... ... ... ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    time

    how can i make a valor to a variable like:
    Code:
    int x;
    x = 0;
    ...
    ...
    ...
    ...
    if (raisestrenght = "yes"){
    x = x + 1;
    how can i make this raise stregnth last only for a couple of minutes?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You could do it like this:
    store the time when stength was raised
    Code:
    #include <time.h>
    ...
    
    time_t StrengthRaised=time(NULL);
    then if your program has a main loop you can check periodically to see if time has elapsed.
    Code:
    if ((StrengthRaised+120)>=time(NULL))
       x-=1;
    This will lower x in about 2 minutes.
    Last edited by Quantum1024; 11-19-2005 at 05:01 AM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    thank youuu

  4. #4
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    damn i could not make this work here is the code that im trying to make it last for a determined time:
    Code:
    dice = diceRoller(1,8);
    cout << "\n You feel stronger\n";
    cout << " Your strenght raised for "<<dice<<" points!\n\n";
    StatStr = StatStr + dice;
    PlyMagic = PlyMagic - 6;
    }
    Last edited by Scarvenger; 11-19-2005 at 09:12 AM. Reason: wrong language

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    here's an example
    Code:
    #include <stdio.h>
    #include <time.h>
    
    time_t LastIncrease=0;
    
    int IncreaseStrength(int s)
    {
    	s+=10;
    	LastIncrease=time(NULL);
    	printf("Strength Increased to %d\n", s);
    	return s;
    }
    
    int LowerStrength(int s)
    {
    	s-=10;
    	LastIncrease=0;
    	printf("Strength lowered to %d\n", s);
    	return s;
    
    }
    int main()
    {
    	int Strength=10;
    	Strength=IncreaseStrength(Strength);
    	for (;;)
    	{
    		//do other things here
    		//do time check
    		if ((LastIncrease+5/*seconds*/)<=time(NULL))
    		{
    			Strength=LowerStrength(Strength);
    			break; //exit example
    		}
    
    	};
    
    	return 0;
    }

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    That's a C example.

    You can also use clock(), which is very similar to time(), except it returns 0 the first time it is called.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    i surrender why isn that working? this is a function of a magic, that is the magic bless, the strenght is properly raised but i cant make it last for a determined time :/ i simply dont back to the normal status
    Code:
    void Bless()
    {
    clock_t lastIncrease = 0;
    lastIncrease = clock();
    system("cls");
    TopBorder();
    StatBorder();
    if (PlyMagic <6)
                {
            	cout << "/n  Nao ha magica suficiente!\n\n"<<endl;
    			}
    			else
    			{
    dice = diceRoller(1,8);
    cout << "\n  Voce se sente mais forte!\n";
    cout << " Sua forca aumentou por "<<dice<<" pontos!\n\n";
    StatStr = StatStr + dice;
    if ((lastIncrease + 50) <= clock()){
    StatStr = StatStr - dice;
    }
    PlyMagic = PlyMagic - 6;
    }
    return;
    }

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    It's pretty hard to play the game from the function and it's pretty hard to go back to main without the nessassary information. You're gonna have to return the dice roll, the time at which the spell was cast, the time it wears off and the magic loss.

    As you get better at programming, you'll find things like this work much better in classes.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    i already know a bit of classes, but how i could do that function as a class?

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    You don't do the function as a class, you do the function in a class. From looking at your level, right now, I'd say continue the way you're going and don't try classes, yet. It's best to get a good understanding of functions and other procedural programming before you try to get object-oriented. With the classes, though. What you would do is end up writing perhaps a class for spells, a class for players, a class for enemy creatures, a class for items. Things of that nature. You would probably go into inheritence with types of spells as well, fire, ice, lighting, etc. This way you can set base standard for all the spells like who they damage the most and what kind of player classes can use them.

    As I said, though, stick with just plain 'ol functions for now. You're on the right track with what you have, you just need to make the connection between your main program and your functions.
    Sent from my iPadŽ

  11. #11
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    ok ty

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 03:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 01:27 PM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21