I'm very new to c++, just 2 days, so I don't really know any complex language as of yet, so i was hoping someone could explain to me why my code isn't working, it almost compiles, but with one "else without an if" error, but even without that chunk of code, there's alot of freezing and loops that don't seem to work properly.
Code:
#include <iostream>#include <string>
using namespace std;
int main()
{
string option1;
string charname;
int doorkey = 0;
string option3;
string option2;
cout<<"Greetings adventurer, I am Lolterzard..\n";
cout<<"What is your name?\n";
cout<<"Enter character name: \n\n";
getline(cin, charname, '\n');
cout<<"Hello, " << charname << "..\n";
cout<<"I will be your guide throughout your time in the cave..\n";
cout<<"I will not always be able to tell you what to do..\n";
cout<<"But if you require assitance, I will give any advice I can..\n\n\n";
cin.ignore();
cout<<"Press enter to begin..\n";
cin.ignore();
cout<<"You awaken..";
cin.ignore();
cout<<"You look around..";
cin.ignore();
cout<<"You are in a small room, you can smell dampness..";
cin.ignore();
cout<<"You look around the room, you can vaguely make out a door on the oposite side..";
cout<<"You notice a small chest in the corner of the room\n\n\n";
do
{
cout<<"What would you like to do?\n";
cout<<"walk to door";
cout<<"\nwalk to chest\n\n";
getline(cin, option1, '\n');
while (option1 != "walk to door" && option1 != "walk to chest" ) {
cout<<"Invalid command, please reenter: \n";
getline(cin, option1, '\n');
}
if (option1 == "walk to door") {
cout<<"\nYou walk towards the door..\n";
cin.ignore();
cout<<"\n\nUpon reaching the door, you decide to..\n";
cout<<"Examine door\n";
cout<<"Open door\n\n";
getline (cin, option2, '\n');
}
else {
cout<<"\nYou walk towards the chest";
cin.ignore();
cout<<"You reach the chest, you think for a second, before deciding to..\n";
cout<<"Examine chest\n";
cout<<"Open chest\n\n";
getline (cin, option2, '\n');
}
if (option2 == "examine door") {
cout<<"The door is tall and wooden..\n";
cout<<"You can smell a foul odour..\n\n";
cout<<"How would you like to proceed?\n";
cout<<"open door\n";
cout<<"walk to chest\n";
getline(cin, option2, '\n');
}
else if (option2 == "examine chest") {
cout<<"The chest is quite small..\n";
cout<<"It is made of wooden, with brass braces..\n\n";
cout<<"You quickly make up your mind, and decide that you'll..\n";
cout<<"open chest\n";
cout<<"walk to door\n";
getline (cin, option2, '\n');
}
else if (option2 == "open chest")
cout<<"\n\nYou kneel down to open the chest..\n";
cout<<"You open the chest, there's a key inside..\n";
cout<<"Using your cat-like reflexes, you..\n";
cout<<"Take the key\n\n";
do {
getline(cin, option3, '\n' );
if (option3 == "take the key" && doorkey == 0) {
cout<<"\nYou take a large metal key..\n";
doorkey++;
cin.ignore();
cout<<"\nYou decide it's about time that you..\n";
cout<<"walk to door";
getline (cin, option3, '\n');
}
else {
cout<<"invalid command, please reenter";
getline (cin, option3, '\n');
}
} while (option3 != "take the key");
else if (option2 != "open chest" && option2 != "open door") {
cout<<"\nInvalid command, please reenter..\n";
getline(cin, option1, '\n');
}
} while (doorkey <= 0);
while (option2 != "open door")
cout<<"\n\nYou walk to the door\n";
cout<<"You think, before..\n";
cout<<"open door";
getline (cin, option2, '\n');
cout<<"You open the door";
cin.ignore();
}
Thanks for anyone who takes the time to read this.
Sorry if bad English, not my first speak.