Endless loop in c++ - c++

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.

Your input variable needs to be initialized because you check its value in the while (input != 0 && counter <= 100){ before assigning the input value.
Undefined Behaviour is typical in C++ when exceeding data type limits. These are some common UB.
This post contains the "solution" to your problem

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++;
}
}

Related

program to find the minimum of a set of numbers using while loop in c++

I am new to c++ and I have been having a hard time understanding the following program. it looks so simple and so it made me feel like I am wrong about everything I have learn so far in c++.
int number = 0;
int min = 0;
cout << "enter (-1) to stop" << endl;
while( number != -1)
{
cout << "Enter an integer:";
cin >> number ;
if (number < min)
min = number;
}
cout << "your minimum number is: " << min << endl;
what I am mostly confused about is the if statement. "min" has only been initialized as equal to zero so the entire if statement does not make sense to me. there is nothing that really defines "min" in the program in my opinion.
Any contribution is much appreciated. thank you!
the program does work fine. And, indeed it does find the minimum of a set of numbers that a user enters. I just do not understand how that happens
On each iteration of the loop a new number is read in using cin. Because number is a signed integer it is possible that it is less than min in which case the if will pass. It is strange that min starts at 0 but the if statement is not redundant.
Initialize min with first number entered.
cout << "enter (-1) to stop" << endl;
cout << "Enter an integer:";
cin >> number ;
min = number;
while( number != -1)
{
cout << "Enter an integer:";
cin >> number ;
if (number < min)
min = number;
}

Entering specific character into while loop

I am writing a code for class that asks the user to input a size that is an odd number equal to or greater than 7. I have been able to make that part of my code work successfully. However, the next part consists of asking the user to enter a specific letter, in this case 'c'. If they do not enter 'c' then the loop should ask them to input another character. Whenever I run this code, it is creating an infinite loop whether I enter 'c' or another letter. I think my expression in my second while loop is incorrect, but I haven't been able to find a lot of information regarding this that could help me.
#include <iostream>
using namespace std;
int main() {
int s, l;
cout << "Welcome to the letter printer." << endl;
cout << "Enter the size: " << endl;
cin >> s;
while (s < 7 || s%2==0 || s<0)
{
cout << "Invalid size. Enter the size again: " << endl;
cin >> s;
}
cout << "Enter the letter: " << endl;
cin >> l;
while (l != 'c')
{
cout << "Invalid letter. Enter the letter again: " << endl;
cin >> l;
}
return 0;
}
because you are getting char for int variable
wrong:
int s, l;
right one:
int s;
char l;
what is why it goes on infinite loop in second while
explanation for infinite loop
This is how basic_istream works. In your case when cin >> l gets
wrong input - failbit is set and cin is not cleared. So next time it
will be the same wrong input. To handle this correctly you can add
check for correct input and clear&ignore cin in case of wrong input.
incorporated from here

Programming Principles and Practice: chapter 4 drill part 1

I just can't seem to get this program to work properly. I can get it to accept two integers and print them to the screen. But I can't get the program to terminate when the '|' is used. Once that its entered it loops infinitely. Here is the code that I have so far:
#include "../../std_lib_facilities.h"
int main()
{
int num1 = 0;
int num2 = 0;
char counter = '\0';
cout << "Please enter two integers and press enter. \n";
bool test = true;
while (counter != '|')
{
cin >> num1 >> num2;
cout << "Your numbers are: " << num1 << " " << num2 << endl;
if (cin.fail())
{
cout << "Goodbye!\n";
test = false;
}
else (counter != '|');
cout << "Enter more numbers or press '|' to exit.\n";
}
system("pause");
}
You are using the wrong condition in your while loop. You are never changing counter so the loop will never end. However you do change test to false in the while loop if the input fails. You can change the condition of the while loop to use test instead like
while(test)
{
//...
}
Since counter is no longer being used you can get rid of it completely.
Please note that unless you change to taking in string and parsing the input any input that will cause cin to fail will end the loop not just a |.

While Loop with LCV of cin won't exit in C++

This is hw. I have asked my professor why the following code won't exit the while loop, and he/she couldn't tell me. My understanding is that once the input stream has no more values to read, the cin will return a value of false, and should cause the while loop to exit. Mine does not. It seems to keep read the input values (a set of integers) process through the loop, then wait for more input. Can anyone tell me why? Below is the code.
# include <iostream>
using namespace std;
int main()
{
int iEvenSum = 0;
int iOddSum = 0;
int iNum;
// prompt user
cout << "Input any set of integers, separated by a space:\n";
cin >> iNum;
cout << "You input: ";
while (cin)
{
cout << iNum << " ";
if (iNum % 2 == 0)
iEvenSum = iEvenSum + iNum;
else
iOddSum = iOddSum + iNum;
cin >> iNum;
}
cout << "\n\nThe sum of Even numbers is " << iEvenSum << "." << endl;
cout << "The sum of Odd numbers is " << iOddSum << "." << endl;
return 0;
}
while(cin) remains true as long as the cin stream is ok and becomes false if cin encounters an end of file character or an error.
In your case, while(cin) will keep on reading the numbers until it encounters an EOF character or an error. Type Ctrl-D when you don't have any more input numbers and it should quit the while loop

How do I use cin in a while loop?

I'm trying to get the user to input their name(s) using a while loop with an array and cin, but after the last person's name is input, the program crashes instead of moving on. Is there a way to fix this, or do I need to completely change up the code? I'm also fairly new to c++, so can any answers be given as simply as possible?
#include <iostream>
#include <string>
using namespace std;
int main()
{
unsigned int numberofplayers;
number://loop back here if more than 4 players
cout << "Number of players: ";
cin >> numberofplayers;
if(numberofplayers > 4 || numberofplayers < 1){
cout << "Invalid number, please enter a number from 1 to 4." << endl;
goto number;
}
string name[numberofplayers];
cout << "Enter your name" << endl;
int a = 1;
while(a < numberofplayers + 1){
cout << "Player " << a << ": ";
cin >> name[a];
cout << "Hello, " << name[a] << "." << endl;
a++;
}
}
You would probably facing array index out of bound, so Change you while loop to this and set a=0 to fill from 0th index.
while(a < numberofplayers){
}
Your last iteration exceeds the size of the array. You need to change it to
while(a < numberofplayers)
also, on another note, the keyword goto isn't used much anymore. I would suggest using a while there also like
while(true){
cout<<"number of players";
cin>>numberofplayers
if(numberofplayers is valid input){
break;
}
cout<<"bad input";
}
There is a question on stackoverflow discussing the use of goto extensively here:
GOTO still considered harmful?