This was simple in Fortran, but...

I am new to C++ and can't seem to resolve these issues.

Part 1:

Need to get DIFFERENT inputs, why not works? Or an alternate method. I just want the inputs to be different.
do{
do{
do{
printf("\n Enter three different numbers\n");
scanf_s("%lf %lf %lf", &low, &mid, &high);
} while (low = mid);
} while (low = high);
} while (mid = high);

__________________________________
Part 2:

This does not work either.

int main
char answer = 'y';
do
// this is the part I want to y/n loop on, functions and such. I left the code out. Just need the yes-no thing. //
system("pause");
answer = 'n';
printf("\n Would you like to sort more numbers?\n"); // this just blows by the scan for input and the function loops infinitely.
scanf_s("%c", &answer);
system("pause");
system("cls");
while (answer = 'y');
Hi,

I just want the inputs to be different


You may use logical operators and just one do while loop

1
2
3
4
5
6
7
do
{
printf("\n Enter three different numbers\n");	
scanf_s("%lf %lf %lf", &low, &mid, &high);	
}
while((low!=mid!=high));//logical not operator cascaded  



Just need the yes-no thing.


1
2
3
4
5
6
7
char answer='Y'
do
{
//stuff
}
while (answer=='Y')//comparison operator is == assignment it =
//also c++is case sensitive so you might wanna add some more to your code 


Further reading : http://www.cplusplus.com/doc/tutorial/operators/
http://www.cplusplus.com/doc/tutorial/control/

HOPE IT HELPS

PS: please use code tags
http://www.cplusplus.com/articles/jEywvCM9/


EDIT: go with Z e r e o
Last edited on
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.
Last edited on
@Z e r e o

Thanks for the input

I skipped the
1
2
3
4
{
printf("\n Enter three different numbers\n");	
scanf_s("%lf %lf %lf", &low, &mid, &high);	
}

part as everybody (apparently almost everybody) does it right and I am not much aware about the C syntax...

And about the while((low!=mid!=high)); part I was concrete sure that that code would work.... At first I also thought about while(low == mid || low == high || mid == high) but it was too lengthy (Imagine if there were 7 parameters instead of three), I also thought about using the algorithm header file but it would make stuff too complicated...

It was my fault that I didn't cross-verified the code..... My mind in on horses these days.... My bad @mwapy I apologize for the stupid mistake

Thanks for dictating me @Z e r e o
Last edited on
My bad about that, just reread what I wrote and seems I did come on a bit strong there. Didn't mean to take the tone of discrediting you or anything, sorry if it came off that way.

Mainly just wanted to point out that while it is good to try and help others when you can, always make sure your solutions work how you intend and that they fit the OP's question also.

Best way to do this is to run them with some test inputs that cover most of the average cases.

Anyways sorry again about that.
My bad about that, just reread what I wrote and seems I did come on a bit strong there. Didn't mean to take the tone of discrediting you or anything, sorry if it came off that way.

No no.... I was wrong, You corrected me, I learnt something new... Thanks for that :)
Last edited on
shadder wrote:
//comparison operator is == assignment it =

That was spot on. All the while conditions in OP have assignments.

1
2
3
4
5
} while ( answer = 'y' );

// does same as
  answer = 'y';
} while ( 'y' );

In this condition one side happens to be constant. Equality should be commutative, so a==b and b==a should both give same result.
1
2
3
4
5
6
7
} while ( answer == 'y' );

// does same as
} while ( 'y' == answer );

//but if one has a typo on the latter:
} while ( 'y' =  answer ); // compile error 


Note. The compiler should at least warn about assignments that are used within conditional expressions. Overall, one should pay attention to what the compiler says. That, I presume, one does with the Fortran compilers as well?


On logical level, if one has values named "low", "mid" and "high" and they have to differ, does one also want to ensure ordering implied by the names: low < mid < high?
This should be true as long as the input does not fulfill that: ! (low < mid && mid < high)
keskiverto wrote:
! (low < mid && mid < high)

that's awesome, also works for multiple ints....
If you have multiple values (perhaps runtime-determined amount), then you should store them in a (dynamic) container and iterate over all the array elements, rather with Standard Library algorithms than self-written code.

For example: http://www.cplusplus.com/reference/algorithm/is_sorted/
Topic archived. No new replies allowed.