0

In this infinite loop which terminates when 0 is entered, when I'm entering anything outside the range of int say 2147483648(range of int + 1) this program keeps on running infinitely.

 #include<iostream>
    using namespace std;
    int main(){
     int n;
     while(1){

           cout<<"enter n: ";
           cin>>n;
           if(n==0)   break;

    }

    return 0;
    }
jwdonahue
  • 6,199
  • 2
  • 21
  • 43
Abhishek Guru
  • 483
  • 4
  • 8
  • Please read [ask]. The "program ... misbehaves" is not a good problem description. Perhaps you should have stopped at "...on running infinitely."? – jwdonahue Jun 19 '20 at 05:15
  • okk brother next time I'll pay attention to this sorry, btw problem is solved now. – Abhishek Guru Jun 19 '20 at 05:53
  • I recommend that you search for "C++ undefined/unspecified behavior". You'll also want to know about "C++ implementation defined behavior". Understanding those, early on, can help you avoid a lot of head scratching. – jwdonahue Jun 19 '20 at 16:04

2 Answers2

3

From the documentation on std::basic_istream::operator>>:

If extraction fails (e.g. if a letter was entered where a digit is expected), value is left unmodified and failbit is set. [until C++11]

So the behavior you are seeing is caused by the failbit, which causes subsequent calls to the >> operator to fail immediately.

Interestingly, the behavior was changed for C++11 and later:

If extraction fails, zero is written to value and failbit is set. If extraction results in the value too large or too small to fit in value, std::numeric_limits::max() or std::numeric_limits::min() is written and failbit flag is set. [Since C++11]

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • thanks brother, I'm a newbie can you tell me how you became so good at cpp. – Abhishek Guru Jun 19 '20 at 05:38
  • @AbhishekGuru, I am guessing he made his share of mistakes over the years and learned from them, the same way we are all doing on a continuous basis. That and prodigious reading of the standards, manuals and issue boards such as this one. When an interface is new to you, study its documentation. Don't assume that "works for me" means your code is correct. You must always understand why it works and whether it should have worked. The longer you practice due-diligence, the fewer mistakes you'll make over time. Just blindly coding for years on end, is the slow road. – jwdonahue Jun 19 '20 at 15:45
  • The way to learn to write good code is to write a lot of bad code, and suffer the consequences of doing so. That will motivate you to figure out what you're doing wrong so as to suffer less in the future :) – Jeremy Friesner Jun 19 '20 at 19:23
  • thank you guys, thanks for supporting your brother:) – Abhishek Guru Jun 22 '20 at 09:36
2

Since you don't test for any errors it's not surprising that your code misbehaves.

Try this

while(1){

       cout<<"enter n: ";
       cin>>n;
       if (!cin || n==0)   break;

}

!cin is a test if cin is in an error state. This would happen (for instance) if the last input failed because what was entered could not be converted to an int.

john
  • 85,011
  • 4
  • 57
  • 81
  • Okk thanks brother, I'm a newbie can you tell me how you became so good at cpp. – Abhishek Guru Jun 19 '20 at 05:38
  • Just practise, I started in the early 90s with one of the first C++ compilers. C++ takes a long time to learn. – john Jun 19 '20 at 06:15
  • Probably the most I learned was from Scott Meyers's [Effective C++](https://www.amazon.co.uk/Effective-Specific-Programs-Professional-Computing/dp/0321334876) – john Jun 19 '20 at 07:42
  • Also SO has a book list [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – john Jun 19 '20 at 07:43
  • @john wrote "C++ takes a long time to learn". Definitely, but I think "a lot of effort" is more accurate. How long it takes is a matter of attention and retention. If you first learn to practice diligence, everything else will come faster. Though I have found with C++, there's a lot of interesting corner cases that even the most skilled practitioners are often unaware of. Even though my current job doesn't involve any C++ coding, I still stumble across new areas of it on a weekly basis, just following the occasional thread here on SO. – jwdonahue Jun 19 '20 at 15:56
  • I would also add that, for C++, there's a lot that most developers really don't need for their daily work and even some things you might want to be blissfully unaware of ;). – jwdonahue Jun 19 '20 at 15:59