I wanted to make a program that I could use as a console. I have created a while loop as you can see below (code1). i know i can fix this by putting getline after while but why does this not work? and why does else repeat when i switch their spots(code2).
Your 'while' statement has two conditions, separated by an 'OR'.
It tries to evaluate the first condition; if necessary - and only if necessary - it will then evaluate the second.
In code1, the first condition actually requires it to 'get a line': so it will sit there waiting until you feed it (bit like my cat!). Once you have put in anything it will deal with it according to the block of statements.
In code2, the first condition will immediately evaluate as 'true' (because you have already assigned exit as false!), so it doesn't need to do the second condition (because OR will be satisfied), so it attempts to do the commands in the while block. Since you haven't actually defined line (because it didn't need to look at the second condition) it will go to the last else and say "Invalid command".