Jump to content
  • Advertisement
Sign in to follow this  
drskittles

it doesn't work like i planned!?!?!

This topic is 4487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I need some help. I'm trying to learn how to program in C++ and I just learned about do while loops and would like to kno if any1 could help me figure out what is wrong with my first do while loop. When i run the program and get to the question in the loop, no matter what answer I input it just repeats the loop?!?! any suggestions plz.
#include <iostream>
#include <string>

using namespace std;

int main(void)
{
	string name;
	char firstResp;
	char secondResp;
	bool deathCond = false;
	short firstPuzResp;
	short secondPuzResp;
	short thirdPuzResp;

	cout<< "Hello warrior, what is your name? ";
	cin >> name;
	cout<< "\nAlright " << name << ", lets begin our adventure!" << endl
		<< "You come upon a dungeon which is guarded by a minotaur!";
	do
	{
		cout<< "\nDo you stand and fight? (Y/N) ";
		cin >> firstResp;
	}
	while ((firstResp != 89) || (firstResp != 121) || (firstResp != 78) || (firstResp != 110));
	if ((firstResp == 89) || (firstResp == 121))
	{
		cout<< "\nToo bad you did't have any weapons. The minotaurs ripped\nyou to shreds!"
			<< "\n\n\tGAME OVER";
		deathCond = true;
	}
	else if ((firstResp == 78) || (firstResp == 110))
	{
		cout<< "\nGood thinking. You didn't have any weapons anyway. You " << endl
			<< "dodge the minotaur and run into the dungeon.";
	}
	cout<< "Inside the dungeo\nyou come upon two tunnels. Which one do you go down?";
	do
	{
		cout<< "The left tunnel or the right? (L/R) ";
		cin >> secondResp;
	}
	while ((secondResp != 76) || (secondResp != 108) || (secondResp != 82) || (secondResp != 114));
	if (secondResp == 76 || secondResp == 108) 
	{
		cout<< "You follow the left path and happen upon a rotting corpse." << endl
			<< "He is still wearing some armor and a sword so you decide" << endl
			<< "to equip yourself.";
	}
	else if ((secondResp == 82) || (secondResp == 114))
	{
		cout<< "You head to the right and after a while you come to a room" << endl
			<< "with magic glyphs on the wall. They read \"1-2-3 is pretty" << endl
			<< "neat but twenty-three is number one.\" You see three panels " << endl
			<< "each with a different glowing number on it.";
			do
			{
				cout<< "Which one will you press first?";
				cin >> firstPuzResp;
				cout<< "Which one will you press next?";
				cin >> secondPuzResp;
				cout<< "Which one will you press last?";
				cin >> thirdPuzResp;
			}
			while ((firstPuzResp != 2) && (secondPuzResp != 3) && (thirdPuzResp != 1));
		cout<< "You figured out the puzzle and the glowing wall opened up " << endl
			<< "revealing a suit of shiny armor and a sharp sword.";
	}
	cout<< "You continue along the tunnel which leads into a large room with " << endl
		<< "a huge dragon which you quickly vanquish with you sword.\n\n\t THE END.";
}


[edit: added source tags -SiCrane]

Share this post


Link to post
Share on other sites
Advertisement
while ((firstResp != 'y') || (firstResp != 'Y') || (firstResp != 'n') || (firstResp != 'N'));

Always evaluates to true. If it is one of the above values (y,Y,n,N) then it cannot be any of the other values, therefore one section of the OR must always be true, therefore the whole statement will always be true. You should change those OR's to AND's.

And also, hardcoding ASCII codes adds unnecessary confusion >_>

Share this post


Link to post
Share on other sites
cout<< "Inside the dungeo\nyou come upon two tunnels. Which one do you go down?";

That will print out:

Inside the dungeo
you come upon two tunnels. Which one do you go down?

Remove the '\' in the middle of dungeon.

Not that this helps your problem, but that was covered above.

Share this post


Link to post
Share on other sites
And, for some more random advice:
do
{
cout<< "Which one will you press first?";
cin >> firstPuzResp;
cout<< "Which one will you press next?";
cin >> secondPuzResp;
cout<< "Which one will you press last?";
cin >> thirdPuzResp;
}
while ((firstPuzResp != 2) && (secondPuzResp != 3) && (thirdPuzResp != 1));



Change the above construct to something like:

bool solved_puzzle = false; // the puzzle doesn't solve itself!
while (!solved_puzzle) {
cout<< "Which one will you press first?";
cin >> firstPuzResp;
cout<< "Which one will you press next?";
cin >> secondPuzResp;
cout<< "Which one will you press last?";
cin >> thirdPuzResp;
// the correct order is 231:
solved_puzzle = (firstPuzResp == 2) && (secondPuzResp == 3) && (thirdPuzResp == 1);
if (!solved_puzzle) {
cout<< "The runes pulse brightly, then fade away.\n"
cout<< "Try again.\n"
} // if
} // while


There are a number of reasons to make the above changes.

First, postive logic (has X happened?) is easier to solve than negative logic (has X not happened?). "solved_puzzle" is expressed positively -- has the player solved the puzzle yet?

Second, the above construct gives the player feedback if they fail the puzzle.

Third, do {} while loops tend to be harder to follow than while {} loops.

Lastly, I might want to suggest moving your variable declarations as close as possible to where you need to use them. There is no need to put all your variables at the start of your function.

Good luck!

[Edited by - NotAYakk on July 7, 2006 4:24:30 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!