0

I am currently learning c++ and I have come to a strange behavior at my code:

#include<iostream>
using namespace std;

int main(){
    int input;
    int counter = 0;
    while (input != 0 && counter <= 100){
        cout << "-----------\n";
        cout << "0 = Get Out!\nEverything else will be displayed!\nPlease enter a number: ";
        cin >> input;
        cout << "The number: " << input << "\n";
        counter++;
    }
    return 0;
}

This Program itself works fine, but when I am typing a number as input, which is at least the maximum of the integer datatype + 1, then this loop will be endless (I programmed a stop after 100 loops) and I cannot really explain this to me. In other languages the program just crashes or gives out an error, because the allocated storage is just not high enough for the data typed in, what is logic and I understand that, but why do the loop gets endless here, that does not make any sense, because I cannot do any inputs after this happens, it just keeps doing the loop with the value (integer maxnumber, in my book stands, that the maxnumber can vary from system to system, so I do not type my number) endlessely and I cannot do anything, but watch my console getting flooded with the same 4 lines.

I would really appreciate, if somebody could explain this phenomena to me, I mean it is not crucial, but it catched my interest anyways.

Roy Mustang
  • 139
  • 1
  • 10
  • 4
    The program does not "work fine". It has undefined behavior right at the very start, because you don't initialize `input` before using it. Turn on compiler warnings. – paddy Feb 21 '22 at 00:17
  • 3
    If `cin` is in an error state because of bad input, you need to clear the error and consume the bad input. – 001 Feb 21 '22 at 00:18
  • @paddy how do I turn these warnings on (I am using the gcc g++ compiler if that matters) I mean I get errors sometimes though so they should be activated idk. I didn’t got one though at this program at all, just the endless loop as I mentioned. – Roy Mustang Feb 21 '22 at 00:34
  • Try the flag `-Wall` or `-Wpedantic` – paddy Feb 21 '22 at 00:37
  • These warnings won't pick up on the thing that causes the endless loop, but they should pick up the use of uninitialized variables and many other handy things. – paddy Feb 21 '22 at 00:39
  • @paddy I tried it out and actually it didn’t warmed me about that I haven’t initialised the variable too after compiling with the -Wall and -Wpedantic flag – Roy Mustang Feb 21 '22 at 00:42
  • I just tried this out, and you're right -- if no optimizations are used, g++ does not warn. That's kinda broken IMO. It's probably because it is initializing everything for you at that level. If you have any level of optimization `-O1` or higher, you'll get the warning. Personally, I would use something like `-O1 -Wall -Werror` as my default day-to-day flags. – paddy Feb 21 '22 at 00:52
  • Ah alright I will use these in the future then thanks – Roy Mustang Feb 21 '22 at 00:57

2 Answers2

1
  1. Your input variable needs to be initialized because you check its value in the while (input != 0 && counter <= 100){ before assigning the input value.
  2. Undefined Behaviour is typical in C++ when exceeding data type limits. These are some common UB.
  3. This post contains the "solution" to your problem
emetsipe
  • 162
  • 1
  • 9
  • Alright thank you and yeah it is right that I didn’t initialised Input, but he doesn’t give an error there and the program works fine. Do I have to manually turn something like error handling on? – Roy Mustang Feb 21 '22 at 00:32
  • are you using G++ or Gcc? – emetsipe Feb 21 '22 at 00:47
  • G++ is in the gcc compiler family isn’t it? I am using g++ and somebody told me, that I can add flags like -Wall to detect errors, but that does not do anything at this program. – Roy Mustang Feb 21 '22 at 00:50
0

How about the following? Is this what you want?

#include<iostream>
using namespace std;

int main(){
    int input;
    int counter = 0;
    cout << "0 = Get Out!\nEverything else will be displayed!\nPlease enter a number: ";
    cin >> input;
    cout << "The number: " << input << "\n";
    while (input != 0 && counter <= 100){
        cout << "Please enter the next number: ";
        cin >> input;
        cout << "The number: " << input << "\n";
        counter++;
    }
}
DaCard
  • 511
  • 4
  • 15