Anytime Input?

Pages: 1234
haha, I'll be honest, i look at that code and think, WTH does alot fo it mean. All i get from it is
1
2
3
4
5
6
7
8
9
10
void timed_text(entered string,time between each char appears)
{
	int length = content.length();//not sure
	for(int i = 0; i<length+1;i++)// i dont like for commands as i'm not good with them, and dont know why people use i as a variable all the time
	{
		char a = content[i]; // something about which char is being typed
		std::cout << a; // display that char
		wait(time); // wait specific duration between each char
	}
} 

I'm also unsure how to apply it later
something like this i'm guessing

timed_text(This is where you enter the string?, .04);
and it'll display?
Last edited on
closed account (4Gb4jE8b)
I shall explain all, so expect a long bit of text.

First use code blocks, much nicer ["code"] and ["/code"] (without quotes) make everything awesome :P

int length = content.length()

content is the name of the string passed into the function, .length() appended to a string returns an integer value representing how long the string is.

for loops are very useful, as are while loops. Learn them, master them, thank me later. here's a good place to read up on them http://www.cplusplus.com/doc/tutorial/control/

I use the variable i as an iterator, as in for each time the loop is run, i is increased by one.

content[i] represents the specific character being set into "a" the [i] makes it dependent on the iteration of the loop. Your next two lines are correct.

you would apply this by calling the function like so

timed_text("this is my string", .1)

if you are going to directly put in text, it has to be in quotes. Otherwise you have to put in a variable string name such as
1
2
string mystr = "something";
timed_text(mystr, .1)

you can also do math for the second argument call, such as the following

1
2
3
string mystr = "something"; //declares a variable called string set equal to "something"
double mydub = .1; //declares a double variable equal to .1
timed_text(mystr, mydub+.1)


as long as the second argument is convertible to a form of double, it can be used as the second argument.
Last edited on
I read through the http://www.cplusplus.com/doc/tutorial/control/ page and we've been taught all fo these so far. with the exception of goto, but probably for the same reason everyone else hates it. But reading the explanation is much nicer then how our teacher explained it. What i got from it is a while is more meant to loop as long as the condition is met (no matter what it is), but the for command is more intended to be used as a quick way to do something X amount of times to save on compile time and save us the time of coding. Which is nice but for is just a while with the counter built in to me.

and with the code you submitted headlessgargoyle, it works, but after it all runs, I get an error
closed account (4Gb4jE8b)
yep :) that's because i'm evil. MUAHAHAHA, no when i gave it to you i messed up
this "for(int i = 0; i<length+1;i++)" should be "for(int i = 0; i<length;i++)"

I'm having fun with this, I created a function to type something, then erase x amount of characters from it, then write in the rest of the string (if there is any left)

much more complex.... Now to add even more to it! haha, i must say thanks for getting me into this, i really like messing with it.
i was playing around with the colored text you talked about. guess i wanted to see it for myself. anyway here is some code. you can choose any color listed in enum COLOR. so if you want pink you writesomething like coloredtext("my message", pink); full example below. i hope it doesnt confuse you more.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

#include <iostream>
#include <string>
#include <Windows.h>


enum COLOR {red = 12, blue = 9, aqua = 11, green= 10, pink = 13, yellow = 14, white = 7};

class coloredtext
{
public:

	coloredtext(std::string text, COLOR color)
	{

		console = GetStdHandle(STD_OUTPUT_HANDLE);	
		SetConsoleTextAttribute(console, color);
		std::cout << text  << std::endl;
	}

protected:
	HANDLE console;
};


int main(int argc, char* argv[])

{
	coloredtext("test and stuff",yellow);
	coloredtext("whoopie doo da",red);
	coloredtext("yikes its changing", green);

	std::cin.sync();
	std::cin.get(); // wait
	
	return 0;
}
headlessgargoyle.... you just swipped code for that... and changed a few things. You should make it at least a little more your own before you claim it...

http://www.cplusplus.com/reference/clibrary/ctime/clock/

By the way, I wasn't explaining how it links cpp files, rather object files that are built from the cpp files. The compiler builds objects that can be used later, but you can't see what is in them, so you use the header file to tell you the structure of the functions. The compiler then builds your cpp file too. Then it all gets sent to the linker which resolves the functions in the object files.
@craniumonempty, actually the code headlessgargoyle used was from my code, I used it from that link. but anyways, i dont claim it as my own either. But how do we link .cpp files to eachother then? Atleast i'm pretty sure large programs are made of multiple codes linked together, and i'm not sure if it'd be easier to do then to have a 100 page long 10000 lined .cpp file , And did anyone find out if it's possible through anything to make my menu? or access an inventory? I dont mind learning an GUI or API or whichever i'd need. I do have 4 or 5 months before this project is due :D. And in class i'm sure we'll learn more too.
broke it down so there was no class. the only new thing for you now just should be the enumerator. the rest is in a simple function. set to any color listed in enum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <Windows.h>


enum COLOR {red = 12, blue = 9, aqua = 11, green= 10, pink = 13, yellow = 14, white = 7};

void coloredtext(std::string text, COLOR color)
{
		HANDLE console;
		console = GetStdHandle(STD_OUTPUT_HANDLE);	
		SetConsoleTextAttribute(console, color);
		std::cout << text  << std::endl;
}


int main(int argc, char* argv[])

{
	coloredtext("test and stuff",yellow);        // print yellow text
	coloredtext("whoopie doo da",red);         // print red text
	coloredtext("yikes its changing", green); // print green text

	std::cin.get(); // wait
	
	return 0;
}

Last edited on
@acorn, actually the color codes we learned are a little different, i'll post them as soon as i find them.
yea there is a better way to do it. you can actually change both the foreground and the background. this just happened to be the fastest way for me atm. i kinda like being able to say red or green instead of numbers like 255,255,0 for a rgb.
Last edited on
closed account (4Gb4jE8b)
@cranium, i never meant to imply that i wrote wait(), i did however write time_typed(). My apologies for coming off as anything different.

@cstorm, the short answer on linking functions written in cpp files is write a prototype in a header, define the function in a cpp that includes the header, and in any other cpp you want to use that function, include the original header. I was just asking about that earlier :P for a slightly more indepth and longer answer http://www.cplusplus.com/forum/beginner/34484/
Last edited on
thanks headlessgargoyle, and cranium, and acorn for the help. I've learned alot from one thread haha. which is good. I do have another question really quick though. Lets say in my game, it is a rpg style action console ect., I have set weapons the user get during the game, and am using a switch, how to I apply a random chance to ass extra damage to your attack, when all these are predefined vars.
here are the vars
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int playerHp = 100;
	int playerHpMax = 100;
	int playerDamage = 0;
	int playerExtrDamage = 0;
		int playerWeapon = 0;
			// 0= fist(BD = 6 | critHit% = .3 , .2 , .4), 1= rock(BD = 7 | critHit% = .2 , .25 , .3), 2= hammer(BD = 8 | critHit% = .15 , .2 , .5), 3= knife(BD = 9 | critHit% = .2 , .3 , .6), 4= M9(BD = 15 | CritHit% .3 , .6[+20% income damage 1 turn]),
		string playerWeaponName = "Fists";
		int playerWeaponBD = 6;
		double playerCritHit[5] ={ 0.3, 0.2, .4 , 0.0, 0.0};
		int playerWeaponAmmo = 0;
			// 0= unlimited
		int playerArmorID = 1;
			// 0 = none, 1= radiation suit(armor=2), 2 = Thick Coated radiation suit(armor=4), 3=
		string playerArmorName = "Radiation Suit";
		int playerArmor = 2;

and here is the switch code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
switch(choice)
					{
					case 1:
						//if()
						//playerExtrDamage = playerWeaponBD;
						playerDamage = playerWeaponBD + playerExtrDamage;
						cout << " You bash " << enemyName << " with the rock and deal " << playerDamage << " damage" << endl<< endl;
						enemyHp = enemyHp - playerDamage;
						cout << enemyName << " runs and scratches you, dealing " << enemyDamage << " damage to you, but your armor deflected "<< playerArmor << endl;
						playerHp = playerHp - enemyDamage + playerArmor;
						if (playerHp <= 0)
							{
								cout << " You have died" << endl;
								wait(3);
								exit (1);
							}//end if
						system("pause");
						system("cls");
						break;


I want to know how to apply the bonus damage if the random chance is met, but am not quite sure how the code needs to be written
Last edited on
doh, just need sleep again...

you don't really need to link the cpp files, they get linked in the linker as objects... that's why we call it that, and that's also why we call this object oriented. Once you make something that's solid code and you aren't going to touch again, you create an object and then leave the header to include.

for example you have #include <string> at the top of the code.

That's just a header file. It might have some things in the header that has to be there, but there are functions that you won't find the body in the header. Where do you think they are? They are in an object file that get's linked to your code. That file was at one time source code too. You wouldn't want to have to compile each and every library each time, especially for a large project, so you create objects when you are done with a piece of code...
closed account (4Gb4jE8b)
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/ is what you are looking for.

Btw, from a gamers standpoint, direct attributes are lame.

playerHp = playerHp - enemyDamage + playerArmor

add either weird constants to make it so that it's not direct, or use a small random interval as a coefficient to make it more dynamic

for the criticals, you'd want something like if(rand() % 3 < 1){add damage to base damage}, which returns a whopping 33% chance of a critical.

I'm interested in how this game turns out, keep us posted yeah?
Last edited on
@ headless
" if(rand() % 3 < 1)

can you explain this from a mathmatical view really quickly, I see the modulous but Im unsure how it works
Last edited on
Break it down?
1
2
3
rand() // -> random number!
rand() % 3 // What is the remainder of the number if I divided by 3?
if(rand() % 3 < 1) // if the remainder of a random number that is divided by three is less than 1 (0?) then true. 
but what keeps the random number from being like 59 trillion on chance, then doesnt it have a higher chance of not being less then 1 below it? and ps. everytime i've used random(), everytime i run the program, its the same number.
you need to seed the random to be different. you will need to #include <time.h>

1
2
srand ( time(NULL) ); // do this first
Last edited on
Proof: Run this:
EDIT BETTER CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <ctime> // for time()
#include <cstdlib> // for rand() and srand()
#include <iomanip> // for boolalpha
#include <iostream>

using namespace std;

int main()
{
  srand( time (0) ); // SEED!

  cout << "MAXIMUM RANDOM NUMBER VIA rand(): " << RAND_MAX << endl << endl;

  for (int i = 0; i < 10; ++i)
  {
    int var = rand();
    cout << "rand() = " << var << endl;
    cout << "rand() % 3 = " << var % 3 << endl;
    cout << "is rand % 3 less than 1? true or false? "
         << boolalpha << ( (var % 3) < 1 ) << endl << endl;
  }

  return 0;
}
Last edited on
ahhh, okay, so this will be a 1/3 chance?
1
2
3
4
if(rand() % 3 < 1)
{
//do whatver
}


but would that mean this is a one fifth chance?
1
2
3
4
if(rand() % 5 < 1)
{
//do whatver
}


and a 1/11 chance?
1
2
3
4
if(rand() % 11 < 1)
{
//do whatver
}

Pages: 1234