How to write a function?

I'm confused about how to write a function. I'm writing a text adventure and it's becoming complicated writing a bunch of nested if statements and loops. I want to write functions for the more commonly used actions but I don't know how. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// This is an example text adventure.

#include <iostream>
#include <string>
using namespace std;

int main()
{
	cout << "A man rushes toward you with a knife. What do you do?\n";
	string response;
	cin >> response;
	// I want to write this if statement as a callable function.
	if (response == "attack")
	{
		cout << "You attack and kill your enemy. You win!\n";
	}
	
	return 0;
}


How do I write the if statement as a function so I can call it whenever I want instead of retyping it all the time?

Thanks in advance.
http://www.cplusplus.com/doc/tutorial/functions/

Basically, the function syntax is

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;

void atk() {
     cout << "You attack and kill your enemy.  You win!\n";
}

int main () {
etc.



When you want to call it later, you would simply use atk();
Topic archived. No new replies allowed.