Jump to content
  • Advertisement
Sign in to follow this  
PsyberMind

Rookie Question

This topic is 2770 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

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]

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

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!