lol when I compiled your code Avast antivirus though it was the evo virus. Anyways
What are you trying to achieve, your code seems to work fine for me. I can type any number besides 5, and it will ask me for a new number. If I type 5, it exits.
It also exits for me but how many times can you type any number besides 5? Because If I do it more than 7 times then it suddenly goes into an infinite loop and I have to Ctrl + C to shut it down. Thats what I don't understand.
Ok I really don't know what's wrong, because that doesn't happen to me.
You can try making a do while loop, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
int main()
{
int num;
do
{
cout << "Please enter anything but 5: ";
cin >> num;
cout << num << '\n';
} while ( num != 5 );
cout << "You entered 5";
}
Permission denied? Curiouser and curiouser... Are you running the program with administrative rights, or is your virus scanner trying to run the program in a sandbox?
Well I am an administrator on my account and I am the only account on my pc and I do run codeblocks as administrator and what do you mean by trying to run the program in a sandbox?
Thats exactly whats strange, there is no error. The program runs fine but if I don't type 5 in 7 tries then it suddenly does an infinite loop displaying "Please enter anything but 5: " with my last entered number at the end.
Try editing this line cout << num << '\n'; so it is like this cout << num << endl;, or if you don't want a new line like this cout << num << flush;
Basically what that code does is endl creates a newline and flushes the buffer, and flush just flushes the buffer and that is it.
That might correct your error, let me know if it didn't and I will try and help find out what is wrong to the best of my ability.
EDIT: Also just to make sure you are not entering anything that is not a integer are you? Like "hello", or 'a', or 1.05 these are all not integers and will cause a infinite loop because your stream is in a error state.
If you downloaded Code::Blocks with MinGW it should be fine, but there are cases of it misbehaving, try looking at http://tdm-gcc.tdragon.net/ it's a very good compiler to work with Code::Blocks.
Then you don't make sense here:
You:
Could it be the permission denied error that I have?
I am currently using codeblocks but is there any other good compilers you can recommend that are free or not? Using the cout << num << endl; or
cout << num << flush; didn't help but is it possible it might have anything to do with installion location?
MinGW is a perfectly fine compiler and it is probably not best for him since he is a beginner to be trying to switch to different compilers and might just complicate things for him.
But as you said the permission denied error might be useful for debugging the problem.
To answer your question to where I don't make sense, I only get the permission denied error when I use debug build target but when I switch
it to release the error goes away which is strange, sorry if I confused you.
Could the permission error be because I am using windows 7?
Which version of code blocks/MinGW did you install? Release 08.02 is only supported for windows vista / xp / 2000. Either way, I might try re-installing the newest version from here: http://www.codeblocks.org/downloads/26
Maybe your program was not shut down correctly after you compiled it before when your code was fully complete, and it's still open on your machine from the original. Try opening task manager, and going to your processes tab and terminating any process that resembles this program you made, and re compile and run again, also delete the executable file first.