Rookie Question

Started by
4 comments, last by PsyberMind 13 years, 4 months ago
I'm in the process of learning C++, and running into a snag. I'm starting to study conditional loops, and for some reason, I can't get this thing to loop. I know I'm missing something simple but, if you could take a look:

#include "stdafx.h"#include <iostream>using namespace std;int main (){		bool quitGame = false;	int moveDirection = 0;		while (!quitGame)	{		int x = 0;		int y = 0;				cout << "Current Position: " << x << "," << y << endl;		cout << "Enter a direction" << endl;		cout << "1) North 2) South 3) East 4) West 5) Quit ";		cin >> moveDirection;			switch (moveDirection)		{			case 1:				x++;						case 2:				x--;							case 3:				y++;							case 4:				y--;										case 5:				quitGame = true;		}	}}


The first part works, but it won't loop, even if quitGame is evaluated false

[EDIT: If someone could tell me how to block code, that would be awesome. bbCode tags don't seem to work]
Advertisement
under x++, x--, y++, y-- etc...

add a break statement. In c++, the switch statement keeps executing past the next case. Confusing I know :)
You need to insert breaks, or the flow will just go through the cases.

#include <iostream>using namespace std;int main (){        bool quitGame = false;        int moveDirection = 0;        while (!quitGame)        {                int x = 0;                int y = 0;                cout << "Current Position: " << x << "," << y << endl;                cout << "Enter a direction" << endl;                cout << "1) North 2) South 3) East 4) West 5) Quit ";                cin >> moveDirection;                switch (moveDirection)                {                        case 1:                                x++;                                break;                        case 2:                                x--;                                break;                        case 3:                                y++;                                break;                        case 4:                                y--;                                break;                        case 5:                                quitGame = true;                }        }}


The tags I used are [source]...[/source].
Thanks! That did the trick.. Asside from it not updating X and Y, which I fixed by un-defining X and Y from 0 and moving them outside of the main function.

Thanks again

Quote:Original post by PsyberMind
Thanks! That did the trick.. Asside from it not updating X and Y, which I fixed by un-defining X and Y from 0 and moving them outside of the main function.

Thanks again


Just to let you know, setting x and y to zero was actually a good idea (even if only for clarity). You should have put it above the while loop though. As you had it x and y was being set to zero at the beginning of every loop iteration. You didn't have to move them all the way out of the main function you could have put them just below the moveDirection definition.
Thanks.. I'll keep that in mind, and give it another try here in a few.

I do remember reading that if I didn't assign a value to the variable, it would basicaly assign whatever was in that memory location, so if I output X or Y before I assigned a value it would take whatever was in that memory location, which could be a disaster.. Thanks for that reminder though :)

This topic is closed to new replies.

Advertisement