Crashing with wrong input

Hi, I'm pretty new to coding, so I started this practice program. It's supposed to be a text-based rpg, very simple, but I seem to be coming up on some errors. Now the program runs with no problem, but when i enter anything except "help", the program crashes with no error message, no popup window saying what's wrong. building the program reveals no syntax errors. I am using visual studio.

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
39
40
41
42
 #include <iostream>
#include <string>

using namespace std;

int main()
{
	string name = ""; //name of user
	bool sex = true; //gender of user
	string gender = "";
	string playerInput = ""; //players input
	string finalOutput = "";//modified input

	cout << "introductory story where I tell you where you are, and ask for your name and gender." << endl;
	cout << "What is your name?" << endl;
	cin >> name;
	cout << "What is your gender" << endl;
	cin >> gender;
	while (gender != "female" && gender != "male")
	{
		cout << "I'm sorry, that is not a valid gender. Please try again." << endl; // make sure the user enters a valid gender before continuing
		cin >> gender;
	}
	if (gender == "female") {
		sex = false; // certain events will occur differently depending on the player's gender
	}
	cout << "Well now we are properly introduced!" << endl;
	cout << "This is where I tell you your backstory and commands for help" << endl;
	cin >> playerInput;
	finalOutput = playerInput.substr(0, 4); // finds the first letters of the input
	if (finalOutput == "help") {
		cout << "This is where I display all the commands and helpful tips" << endl;
		cin >> playerInput;

	else if (finalOutput == "move") {
		cout << "I'm sorry, I don't know that command." << endl;
		cin >> playerInput;
	}

}
	return 0;
}
Last edited on
1
2
3
if (finalOutput == "help") {
		cout << "This is where I display all the commands and helpful tips" << endl;
		cin >> playerInput;


Your if statement is missing a }.
You're main is also missing a }.

Why would the program react to an input that is anything but "help" or "move" when you've only created if statements regarding help and move. You need to code what you want to happen incase non of these 2 words are entered. Perhaps use a while-loop to loop around and ask the user to re-enter until they enter help or move, or perhaps you want something else to happen? It's all in your hands.
What is your program exactly supposed to do?

As far as I can tell, your program is functioning.

I did have to add some brackets at the end because they were missing.
Line 34, you need a bracket to enclose the if statement on line 31
Line 39, You need a bracket to enclose the main function.

I ran the program and this is the output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
introductory story where I tell you where you are, and ask for your name and gen
der.
What is your name?
Michael
What is your gender
male
Well now we are properly introduced!
This is where I tell you your backstory and commands for help
movesfasf
I'm sorry, I don't know that command.
ok

Process returned 0 (0x0)   execution time : 31.643 s
Press any key to continue.


As you can see, I typed something other than "help" and the program produced the intended output. It did not crash on my system.

EDIT:

If you type in anything other than "help" and "move" as the first 4 letters, the program will simply terminate since you did not tell the program what to do with any input for playerInput other than "help" and "move"


EDIT2:

TarikNeaj beat me to it, but yes, basically what he said.
Last edited on
Topic archived. No new replies allowed.