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
Related
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);
I've just had one of my exams at the college
one of the questions were to program a function that counts how many prime numbers are between 2 and a given number from the user(including the number).
I wrote this algorithm which works for me but they still deducted all of the points of the question as if it was completely wrong.
Can please someone tell me what's wrong with the code?
Thanks a lot.
#include <iostream>
using namespace std;
void main()
{
int count = 0;
int num;
cout << "Please enter a natural number " << endl;
cin >> num;
for (int i = 2; i <= num; i++)
{
if ((i == 2 || i == 3 || i == 5||i == 7) || (i % 3 != 0 & i % 5 != 0 & i % 7 != 0 & i % 2 != 0))
count++;
}
cout << "There are " << count << " prime numbers beteween 2 amd " << num << endl;
}
Your code only checks if a number is a multiple of 2, 3, 5, or 7. That's missing a lot of multiples, meaning your code likely gives many false positives.
That condition is also quite difficult to read, so you probably would have lost points for that as well.
You would have been much better off replacing that condition in the loop with another for loop, and checking every odd number from 3 to the square root of the test number.
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.
I'm trying to check if the users input data falls within the range i want. so far i have it like this:
void getPlayersNumbers(int playerArray[], int size) {
cout << "Please enter 5 numbers between 1-5\n";
for (int i = 0; i < size; i++) {
cin >> playerArray[i];
if (playerArray[i] < 1 || playerArray[i] > 5) {
cout << "Please enter numbers between 1 and 5\n";
}
}
}
now if they enter a number less than 1 or greater than 5 the message shows up. The only problem is i can still only input 5 numbers total even if one of them is out of the range and the message shows up. what should i do so that the function will only end if they enter 5 numbers that are within my set range?
Consider what you are doing:
You get input.
You put that input into your array.
You check whether input was smaller than 1 or bigger than 5
Go back to 1.
Hi I'm needing some help. I'm in a intro to programming class and we are using c++. I am hoping someone can help me with an assignment that was due yesterday (I understand not to expect miracle responses but a girl can always try).
I'm having two problems that I know of. The first is regarding the smallest value.
The big one is in trying to make it loop for requirements of three times but not lose out on my total count. I cannot use arrays or anything I haven't learned yet which is why I've posted this. I've seen similar problems and questions but they have ended up with answers too complex for current progress in class. So here is the problems instructions:
Instructions
1) Write a program to find the average value, the largest value, and the smallest value of a set of numbers supplied as input from the keyboard. The number of values in the data set must be in the range 0 to 20, inclusive. The user will first enter the number of values in the data set(use variable int Number). Give the user 3 attempts at entering Number in the range given. If the value for Number entered is out of this range, write an error message but continue. If the user does not enter a valid value for Number within the 3 attempts print an error message and terminate the program.
2) Format only the output for the Average value to 3 decimal places when printed.
3) The values in the data set entered as input can be any value positive, negative, or zero.
4) Make the program output readable(see the example below). (Note: that you will notprint out the input values that were entered in this program like you normally are required to do. This is because we have not covered the “tool” needed to do so yet in our studies).
Below will be the output from the execution of your program:
(using these values in order for the data set --> 19.0 53.4 704.0 -15.2 0 100.0)
The largest number: 704
The smallest number: -15.2
The average of the 6 numbers entered: 143.533
yourName L4p2XX.cpp
Lab#4 prob 2 XX-XX-12
Here is my poor excuse at the solution:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double Number = 0, minValue, maxValue, average, total = 0;
int ct = 0, numCount;
cout << "How many numbers would you like to enter? ";
cin >> numCount;
for(ct = 1; ct <= numCount; ct += 1)
{
cout << "Enter Value from 0 to 20, inclusive: ";
cin >> Number;
if(Number > 20|| Number < 0)
for(int errorCt = 1; errorCt <= 4; errorCt += 1)
{
if(errorCt == 4)
{
cout << "You have had 3 attempts to enter a valid" <<
"number. \nPlease try this program again when you" <<
"are able to follow directions.";
cout <<"\nLBn\n"<<"L4P2LB.cpp\n"<<"11-05-12\n";
return 0;
}
cout << Number << "is not within range.\n" <<
"Please enter a number from 0 to 20: ";
cin >> Number;
} //end for loop
total += Number;
if(maxValue <= Number)
maxValue = Number;
if(Number <= minValue)
minValue = Number;
} //end for loop
cout << "The smallest number entered was " << minValue << endl;
cout << "The largest number you entered was " << maxValue << endl;
average = total/numCount;
cout << setprecision(3) << fixed << showpoint << "You entered " <<
numCount << " numbers. The average of these is " << average;
//Program ID
cout <<"\n" << "L4P2LB.cpp\n" << "11-05-12\n";
system ("pause");
return 0;
} // End main
Thank you in advance to anyone who can steer me in the right direction. Not looking for anyone to do my work I just need help in direction if nothing else or any suggestions as to what to do. Thanks again. Lynda
Also I need somehow to pause after the third time and exit properly. If I put the second pause in it won't work so am I missing something obvious there too!
The first problem I see is that you didn't initialize a couple of variables.
You should either initialize both minValue and maxValue variables with something which will overwritten in every case in the first loop (typically "positive/negative infinity", as provided by <limits>), or just set both to Number in the first iteration, regardless of their current value. So I'd suggest to fix this by replacing
if(maxValue <= Number)
maxValue = Number;
if(Number <= minValue)
minValue = Number;
with
if(maxValue <= Number || ct == 1)
maxValue = Number;
if(Number <= minValue || ct == 1)
minValue = Number;
as ct == 1 will be true in the first iteration.
That said, you check the 0..20 range condition on the wrong variable. You check it on the Number variable, but you should check the numCount variable. But you also didn't respect the requirement that the variable to store the "number of numbers" should be Number, so you did check the correct variable, but used the wrong to read the input into. This should fix this issue (I changed the variable name in the cin >>... line + moved the check outside your main loop):
cout << "How many numbers would you like to enter? ";
cin >> Number;
if(Number > 20|| Number < 0)
{
for(int errorCt = 1; errorCt <= 4; errorCt += 1)
...
if(errorCt == 4)
{
cout << "You have had 3 attempts to enter a valid" <<
"number. \nPlease try this program again when you" <<
"are able to follow directions.";
cout <<"\nLBn\n"<<"L4P2LB.cpp\n"<<"11-05-12\n";
return 0;
}
cout << Number << "is not within range.\n" <<
"Please enter a number from 0 to 20: ";
cin >> Number;
} //end for loop
}
for(ct = 1; ct <= Number; ct += 1)
{
...
}
...