Loop goes infinitly

Pages: 12
Hello I am a total newbie at C++ and I am learning about loops but I have a problem, if I type even a simple loop like this one,


#include <iostream>

using namespace std;

int main()
{
int num = 0;

while ( num != 5 )
{
cout << "Please enter anything but 5: ";
cin >> num;
cout << num << '\n';
}

cout << "You entered 5";
}




it will loop infinitly after I enter any number other than 5 for 7 times
Is it something I am doing wrong?
Much appreciated.
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.
Last edited on
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.
Hmmm... That's not supposed to happen. Try adding cin.ignore(); after cout << "You entered 5"; it might help
Nope cin.ignore(); doesn't help.
Could it be because I switch from debug to release when I compile and run the program?
Last edited on
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>

using namespace std;

int main()
{
int num;

do
{
cout << "Please enter anything but 5: ";
cin >> num;
cout << num << '\n';
} while ( num != 5 );

cout << "You entered 5";
}
It still does the same thing even with a do-while loop.
Could it be the permission denied error that I have?
Last edited on
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?
Last edited on
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?
Could you paste the exact contents of your error?
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.
Last edited on
Sounds strange. Which compiler are you using? Maybe try using a different one...
closed account (3qX21hU5)
It might have to do with your output buffer...

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.
Last edited on
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?

Me:
Could you paste the exact contents of your error?

You:
Thats exactly whats strange, there is no error.
Last edited on
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?
closed account (3qX21hU5)
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?

EDIT: I only type in integers
Last edited on
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
Last edited on
closed account (3qX21hU5)
Hmm, one more test them you might have something wrong with your installation/computer.

Copy this code and see if it works.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <limits>

using namespace std;

int main()
{
int num;

while (num != 5 )
{
    cout << "Please enter anything but 5: ";
    cin >> num;
    if (!cin >> num)
    {
        cin.clear();
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        continue;
    }
    cout << num << '\n';
}

cout << "You entered 5";
}
Last edited on
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.
Last edited on
Pages: 12