shadder wrote: |
---|
You may use logical operators and just one do while loop
1 2 3 4 5 6
|
do
{
printf("\n Enter three different numbers\n");
scanf_s("%lf %lf %lf", &low, &mid, &high);
}
while((low!=mid!=high));//logical not operator cascaded
|
|
OP please don't use that and think it is correct. That code has two quite glaring errors in it and will not work like you would think it would.
If you are going to use C++ I would recommend to use to C++ streams instead to grab and print your input.
So instead of
1 2
|
printf("\n Enter three different numbers\n");
scanf_s("%lf %lf %lf", &low, &mid, &high);
|
You can do something like
1 2
|
std::cout << "Enter three different numbers" << std::endl; // Prints to standard output
std::cin >> low >> mid >> high; // Asks for three inputs from the standard input, though it does not check to make sure it is an integer.
|
Second never do this
(low!=mid!=high) // logical not operator cascaded
while that code will certainly compile, it will not give you the results you're assuming. It is not valid C++ so please don't ever do that.
Below is one possible correct way to do what you want. The main differences between this code and shadder's code is that you want to use the == operator instead of the != operator and you need to separate your tests with the || (Logical OR) operator
http://www.cplusplus.com/doc/tutorial/operators/ ( || operator is about halfway down the page)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// It is important to have them all initializd to 0 in this case so we
// enter the while loop.
int low = 0;
int mid = 0;
int high = 0;
// This while loop checks to see if any of the low, mid, high variables
// have the same value and if they do it keeps running.
// It does this check by using the == operator which tests equality and the ||
// operator (Logical OR).
while(low == mid || low == high || mid == high)
{
std::cout << "Enter three different numbers" << std::endl;
std::cin >> low >> mid >> high;
}
// If we exit the while loop that means all the three variables hold different values.
std::cout << "All numbers are not the same.";
|
Hopefully that helps a bit with your first part. The second part you should be able to figure out also once you have got the hang of your first part.
@Shadder Please don't submit answers to questions that you don't have to the experience to answer, while it is good that you want to help others it isn't helping when you give them incorrect information.
There was two obvious errors in your answer code that you should have caught if you tried using the code so if you do want to try and keep helping other beginners I would at least recommend compiling and using the code to make sure it is correct before submitting it as an answer.