Unexpected Execution in Do-While Loop - c++

int main()
{
int number;
do
{
cout << "Input a number (1 - 8): " << endl;
cin >> number;
}
while (number < 0 || number > 8);
}
Why do the numbers 0 and 8 break out of the loop even though the while expressions are not satisfied? 0 < 0 == false and 8 > 8 == false?

Currently, the condition in your do-while loop is checking if your number is less than 0 or greater than 8. If the condition is true the loop repeats until your number is within the range from 0 to 8. The loop repeats as long as the condition is true. I think you are trying user to input a number in the range 1 to 8, if the input is out of this range, you want to keep asking the user to input a correct number. If this is the case, you need to edit your while condition this way:
while (number < 1 || number > 8);

Related

C++ I'm trying to end this infinite loop to find even numbers between 0 and the users entered value

This program is supposed to find the even numbers between 0 and the users entered value like 500 for example. I can only use for and if loops.
#include <iostream>
using namespace std;
int main()
{
int value = 0;
//Ask user to enter a number between 0 and 501
cout << "Please enter a value between 0 and 501: \n";
cin >> value;
if (value < 0 || value > 501)
{
cout << "ERROR! The value you entered is out of range!\n";
cout << "Please enter a value between 0 and 501\n";
cin >> value;
}
for (value = 0; value > 0 || value < 501; value++)
{
if (value % 2 == 0)
{
cout << "The even numbers are: " << value << endl;
}
}
return 0;
}
the reason of the broken code is the fact that you are mixing variables in the loop, i.e. the variable that increases and the variable to be compared for going out of the loop:
for (value = 0; value > 0 || value < 501; value++)
you should have different values for the counter and the condition breaking the loop
for (int i = 0; <some logic related to user input>; ++i)
Your if statement has no use because it is not in a while loop. Meaning that if the user enters a number lower than 0 or higher than 501, the code will execute normally. You can use this instead:
while (true) {
if (value < 0 || value > 501) {
cout << "ERROR! The value you entered is out of range!\n";
cout << "Please enter a value between 0 and 501\n";
cin >> value;
continue;
}
else {
break;
}
}
This new code will keep asking the user to enter a number between 0 and 501 like you intended.
Your for loop isn't working because you set the value (that the user inputted) to 0 again. So you should initialize a new variable and use it in the for loop. You can use this:
for(int i = 0; i < value; i++) {
if (i % 2 == 0) {
cout << "The even numbers are: " << i << endl;
}
}
So it will do this:
See if the i is less than the value that the user inputted, starting with 0.
If it is less than the value, it will check if it is an even number (using % 2)
If it is an even number, it will print it out.
Then it will increment i (increase i by 1) and do this all over again.
Hopefully this helps and don't be afraid to ask questions !
The conditions tells when the loop should stop, there is no value that can make this codition false because any integer is either >0 or <501.
You do not need the value > 0 part of the condition, because you start the loop at value = 0. It is not possible that it gets smaller than 0 by incrementing it.
Moreover, you are using value as the loop index, but in the first part of the code value is the user input that is now lost (overwritten with 0).
The loop can look like this:
for (int index = 0; index < value; index+=2)
{ // ^^^-------------------- the loop stops when this is false
cout << "The even numbers are: " << index << endl;
}
This loop loops until index is bigger or equal to the user entered value.
{
if (value % 2 == 0)
{
cout << "The even numbers are: " << value << endl;
}
}
For every number greater than 501 and greater than 0 your condition value > 0 || value < 501 will evaluate to be true.
Make sure you run this loop till the number given by the user.

Why am I having issues with my while loops in this function?

The function is meant to read in two values, minimum and maximum. If either value is less than two an error message prints and both values are read in again.
I've tried making a separate while loops for min < 2 and max < 2. I've tried parenthesizing each condition on each side of the or operator. I've tried declaring two variables withing the function and setting one as equal to max and one as equal to min, and using those new variables in the loops.
void read_range(int & min, int & max)
{
cout << "Enter minimum and maximum: ";
cin >> min >> max;
while (max < 2 || min < 2); {
cout << "Error. Minimum and maximum must be at least 2." << endl;
cout << "Enter minimum and maximum: ";
cin >> min >> max;
}
while (max < min) {
cout << "Error. Minimum must be less than maximum." << endl;
cout << "Enter minimum and maximum: ";
cin >> min >> max;
}
}
the second while loop works fine.
There are 2 strange cases:
1. If either entered value is less than two, the program continues reading in values and must be manually stopped:
Enter minimum and maximum: 0 5
5 6
1 2
50 90
90 50
^Z
[23]+ Stopped
Even if both entered values are greater than two, the while loop still executes. The while loop does not execute again even if the re-entered value(s) is less than 2:
Enter minimum and maximum: 5 10
Error. Minimum and maximum must be at least 2.
Enter minimum and maximum: 1 2
(program continues running after this).
also, if the exact same values are re-entered then the while loop does not execute again:
Enter minimum and maximum: 5 10
Error. Minimum and maximum must be at least 2.
Enter minimum and maximum: 5 10
(program continues running after this)
Any help is appreciated.
Your 1st loop has an erroneous ; on it:
while (max < 2 || min < 2); {
It should be this instead:
while (max < 2 || min < 2) {
while (max < 2 || min < 2); {
should be like this
while (max < 2 || min < 2) {
OUTPUT:
Enter minimum and maximum: 1
1
Error. Minimum and maximum must be at least 2.
Enter minimum and maximum: 10
5
Error. Minimum must be less than maximum.
Enter minimum and maximum: 5
12
NO ERR

Loops and Modulo

I'm not sure why this code isn't working as intended.
"input % 7 != 3 || input % 7 != 4)". I'm saying if input modulo 7 does not equal to 3 or 4 then stay in the loop. however when I enter 10 it still doesn't work when 10 modulo 7 equals to 3.
#include <iostream>
#include <string>
using namespace std;
int main(){
int input;
int count = 1;
cout << "Please Enter a positive integer that is 3 or 4 modulo 7: ";
cin >> input;
while (input <= 0 || input % 7 != 3 || input % 7 != 4){
count++;
cout << count << "tries, " << "please try again: ";
cin >> input;
}
cout << "Congratulations, you passed";
return 0;
}
I'm saying if input modulo 7 does not equal to 3 or 4 then stay in the loop.
In that case, you need to change your condition to this:
while (input <= 0 || (input % 7 != 3 && input % 7 != 4))
There is a logical error, here, this is the condition you might need:
while (input <= 0 || (input % 7 != 3 && input % 7 != 4)){
You want to stay in the loop while the modulu is different from both 3 and 4.
I'm not sure why this code isn't working as intended. "input % 7 != 3 || input % 7 != 4)". I'm saying if input modulo 7 does not equal to 3 or 4 then stay in the loop
No, you're not!
You're saying if input modulo 7 does not equal 3, or input modulo 7 does not equal 4, stay in the loop.
That condition always holds because input modulo 7 cannot be both 3 and 4 at the same time.
This kind of rewrite of logical connectives is actually an English mistake; although colloquially accepted in English, it's not in C++.
You meant "if input modulo 7 does not equal 3, and input modulo 7 does not equal 4, stay in the loop", i.e. input % 7 != 3 && input % 7 != 4.

Do.. while loop: How should I do so that the input (int) does not include digit '1' or '0'?

do
{
cout << "Enter a positive integer: ";
cin >> n;
}while ( n <= 1 || n == 0);
How should I code so that if I enter '1234', it would reject because there is a digit '1' there.
One approach would be to input a std::string from the user, and use std::string::find to see if 0 or 1 are present. Once you're done with that, attempt to convert the string to an integer.
Alternatively, if you want to keep everything "numeric", you can always use % 10 to extract the rightmost digit:
if (n % 10 < 2){
/*not allowed: I'm assuming n is positive here*/
}
Followed by
n /= 10
to remove that digit, and repeat, until you're left with zero. Obviously you'll need to test the special case of n starting at 0 separately.

Need help understanding what's going on in my while loop

My program executes just fine, but I have questions about how my while loop is set up.
I know the Boolean values for true and false are 1 and 0, respectively, but I'm not understanding why my output displays the even and odd numbers backwards (to my understanding, it's backwards). Simply put, I don't understand why if ( number % 2 == 0 ) would display that a number is even and when I change it to 1, it displays odd. I'm reading this line as, if (even number equals to false). I don't know if that's where I'm going wrong. What's the correct way to read this line?
The way I have my code set up now displays the numbers correctly, I'm just not understanding why. Can anyone help?
// Program indefinitely displays an even
// or odd number until a negative number
// is entered.
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Please enter a number: ";
cin >> number;
while ( number >= 0 )
{
if ( number % 2 == 0 )
{
cout << number << " is even \n";
cout << "Please enter a number: ";
cin >> number;
}
else
{
cout << number << " is odd \n";
cout << "Please enter a number: ";
cin >> number;
}
}
cout << "Thank you. \n";
return 0;
}
number % 2 is 0 if number divides 2 (i.e. is even), 1 if number is positive and does not divide 2 (i.e. is odd), and -1 if number is negative and does not divide 2 (i.e. is odd). (The last point must be the case from C++11 onwards).
So, since 0 == 0 is true, number % 2 == 0 is true if, and only if, number is even.
So you've written if ( number % 2 == 0 ) to trap all even cases, and the else traps the odd cases.
Testing if ( number % 2 == 1 ) is only a test for positive oddness, but older C++ standards allow this to be true for negative number.