Jump to content

While loops not working- c++ (warning: noob ^^)

Go to solution Solved by msevilgenius,

Remove the semicolons after the 'while(condition)'s they cause infinite empty loops.

It's been a while since I coded anything and I was quite a noob to begin with, so when earlier I was trying to write a simple text game in c++ (the level I made so far is pretty much a test, which I obviously need since I can't get it to work xD) I got stuck as my loops weren't giving any output whatsoever.

This is the code:

 

// game attempt1.cpp : Defines the entry point for the console application.// #include "stdafx.h"#include <iostream>#include <string>#include "stdio.h"#include "windows.h"using namespace std;  int _tmain(int argc, _TCHAR* argv[]){bool ship = true;bool cargobay = false;bool cabin = false;bool won = false;bool end = false;string direction = "";while (end == false){while (ship == true);{printf ("You are now on the main deck. \n Possible directions: north (to cabin), south (to cargo) \n");cin >> direction;if (direction == "north"){ship = false;cabin = true;}else if (direction == "south"){ship = false;cargobay = true;}}while (cargobay == true);{printf ("You are now in the cargo bay. \n Possible directions: north (to main deck) \n");cin >> direction;if (direction == "north"){cargobay = false;ship = true;}}while (cargobay == true);{printf ("You are now in the cabin. \n Possible directions: south (to main deck), start (enter hyperspace, end of the level) \n");cin >> direction;if (direction == "south"){cabin = false;ship = true;}else if (direction == "start"){cabin = false;won = true;}}while (won == true){printf ("Congratulations! You have finished this level! Enter any value to continue. \n");cin >> direction;won == false;end == true;}}return 0;}

 

Am I doing the whole thing wrong (is this not how while works)? I'm sure it's a very inefficient way of doing things, but I'll get back to optimizing it once I get it working...

 

Thanks for any help! :)

Edited by alpenwasser
code tags :)

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Remove the semicolons after the 'while(condition)'s they cause infinite empty loops.

I remain,  

msevilgenius

Link to comment
Share on other sites

Link to post
Share on other sites

I'm sure it's a very inefficient way of doing things, but I'll get back to optimizing it once I get it working..

it's probably a better idea to do it in the best way you can starting from now

if you don't correct asap the flaws in your knowledge, you will start thinking that those things are right, and it will be harder to correct them later

 

the biggest thing i see in your code is the C/C++ confusion

you should decide which language to use between the two, and do a quick google or something to verify which is the correct way to do input/output/string manipulation in the language of your choice

also, indentation

Link to comment
Share on other sites

Link to post
Share on other sites

it's probably a better idea to do it in the best way you can starting from now

if you don't correct asap the flaws in your knowledge, you will start thinking that those things are right, and it will be harder to correct them later

 

the biggest thing i see in your code is the C/C++ confusion

you should decide which language to use between the two, and do a quick google or something to verify which is the correct way to do input/output/string manipulation in the language of your choice

also, indentation

 

It is indented in the compiler, it's just that after copy pasting it it came out like that on the post. What do you suggest I do instead of whiles?

 

 

Remove the semicolons after the 'while(condition)'s they cause infinite empty loops.

 

thanks that solved the main issue!

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

It is indented in the compiler, it's just that after copy pasting it it came out like that on the post. What do you suggest I do instead of whiles?

while is fine, printf isn't

Link to comment
Share on other sites

Link to post
Share on other sites

while is fine, printf isn't

 

so cout?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

so cout?

yup

you're also using C includes: for this program you only need iostream and string

Link to comment
Share on other sites

Link to post
Share on other sites

yup

you're also using C includes: for this program you only need iostream and string

 

Ok thank you :D some of the includes where already there from the start, not sure why ^^

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Remove the semicolons after the 'while(condition)'s they cause infinite empty loops.

Just to elaborate on the answer given, semicolons give the compiler a signal that the statement written is finished. For example:

 

bool ship = true;

However, with loops, like for loops and while loops, they have a different meaning. A typical while loop will look like:

 

while(expression){statement}

In the above code, the while loop will loop until the expression becomes true. Every time it is not true, including the first time, it will execute whatever your statement code is.

Sometimes, you don't want to evaluate a statement for every iteration. Sometimes expression is a function, for example:

 

while(check_something()){}

In the above case, the program will execute check_something(), which will return either true or false. If it returns true, the program will exit the while loop and continue on. If it returns false, the code between the { and } will be executed; but there is no code! so it will simply continue to the top and check_something() again.

The above is such a common practice that C/C++ allow you to drop the { and } completely, and simply put a ; at the end of the while statement:

 

while(check_something());

This simply tells the compiler, "I don't want to execute anything else. Just evaluate my expression indefinitely."

-robodude666

Link to comment
Share on other sites

Link to post
Share on other sites

-

 

Oh I see! Thanks!

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

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

×