Sign in to follow this  

Rookie Question

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

This topic is 2552 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this