-1

Guys why this code become an infinite loop? I'm learning C++ so if you can explain the solution, for me would be very important!

// Odd_or_Even.cpp : This program determinate if a number is Odd or Even
//

#include "stdafx.h"
#include "std_lib_facilities.h";


int main()
{
    int num = 0;
    bool repeat = true;

    while (repeat == true)
    {
        cout << "Please enter an integer to determinate if it's odd or even: ";
        cin >> num;
        cout << "\nReading data...";

        if (!cin) {
            cout << "Failed\n";
            cout << "There is some problem with the number, sorry!\n";
            cout << "\n";
            cin.clear();
        }
        else
        {
        cout << "God job, now stop lose time.";
        repeat = false;
        }


    }

    keep_window_open();
    return 0;
}

Thanks!

EDIT: ok i writed the if for block the loop, but if you try to write a letter, instead of a number, it still go in a loop!!

RavenJe
  • 129
  • 4
  • 2
    you should have a line that will change the value of repeat to false in the if statement – brenners1302 Nov 10 '15 at 02:49
  • Yes i know but i want that when i say "sorry" it go back to the top and repeate, instead the output become infinite in: cout << "Failed\n"; cout << "There is some problem with the number, sorry!\n"; cout << "\n"; – RavenJe Nov 10 '15 at 02:51
  • if it goes back to the start to prompt another value, when do you propose to end the loop? – brenners1302 Nov 10 '15 at 02:55
  • @RavenJe There's the `else`clause available, where you can set `repeat = false;`? What kinda silly question is this? – πάντα ῥεῖ Nov 10 '15 at 02:57
  • @RavenJe OK, as for your edit, you'll need to call `cin.clear()` for the failure case, to be able to get input again. – πάντα ῥεῖ Nov 10 '15 at 03:00
  • yes the question was stupid becouse i forgot to edit with the stop while sorry. Mh there is just this solution i mean, just with cin.clear? And why i need to use it? ( i'm learning, i like a long description, if you can and have time =) ) – RavenJe Nov 10 '15 at 03:01
  • Ok i added the cin.clear(); but still reapeat infinite T-T – RavenJe Nov 10 '15 at 03:03
  • @RavenJe Marked as duplicate, forgot to mention you'll also need to consume the erroneous input after calling `cin.clear()`. – πάντα ῥεῖ Nov 10 '15 at 03:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94766/discussion-between-ravenje-and--). – RavenJe Nov 11 '15 at 00:10

2 Answers2

0

It's an infinite loop because you never update repeat. Your while loop will continue to run until repeat is set to equal 0 or false.

P.S. since repeat is a Boolean value, while(repeat) is the same as while(repeat==true)

user3246167
  • 129
  • 1
  • 15
0

Your code sets repeat to true and then your while loop runs while

while(repeat == true)

To get it out of an infinite loop you need to do this somewhere inside the while loop:

repeat = false;

On what condition would you like to break out of the loop?

Subito
  • 36
  • 1
  • 5