Somewhat experienced, but stuck

Running through a function assignment, most of it works, but I'm stuck on a particular function: I am supposed to take in 4 parameters and return a true statement with a bool if and only if all four parameters are different numbers... I took this approach, and I warn you, it's not pretty ;).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void differentparameters ()
{

	double a,b,c,d,x;
	bool parameters = false;
		cout << "THIRD FUNCTION, RETURNS TRUE ONLY IF 4 PARAMETERS ARE DIFFERENT" << endl;
	cout << "Please give 4 parameters" << endl;
	cin >> a;
	cin >> b;
	cin >> c;
	cin >> d;

	if (a!=b || b!=c)
	{
		if (a!=c || c!=d)
		{
			if (d!=b || d!=a)
				parameters = true;
			else
				parameters = false;
		}
		else
			parameters=false;
	}
	else
		parameters=false;
				
	if (parameters=true)
		cout << "This is TRUE." << endl;
	else
		cout << "This is FALSE." << endl;
}//differentparameters 


My code keeps saying it's true no matter what I put in it. I tried doing all the if statements separately instead of the || but it still said true. :( I kept them for now, because it immensely shortens the code. I don't know many C++ shortcuts, maybe there is one that compares all of the values? Also I'm not looking for someone to write the code for me, I am looking for help on how to fix it, and what the problem is. Thanks so much for any help!
Why are you using or? That's going to return true if
a does not equal b OR c does not equal b

So basically, if anything is different, it's going to be true. Try the and operator, might have better luck :D

This function is also not so good. You're doing waaay too many things in there. Why not take the input from main, or another function, and then actually pass those values into this function using parameters, like your assignment says.
Switched the || to &&, still the same problem, always is true... I am intrigued by the other part of your suggestion though... are you saying I should put two of the parameters into the main function, and if I did, how would I get those parameters into my function? Also, I didn't put my whole assignment up, just the particular function I was having problems with, but do you really think I'm not following the assignment? I thought I was lol. Thank you for a swift reply! :)
Wrote this in notepad in about 1 minute
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool testForSomething(int x, int y);

int main()
{
	int a,b;
	cout << "Hey there, gimme two ints\n";
	cin >> a >> b;
	//Probably want to do some input validation
	testForSomething(a,b);
	return 0;
}

bool testForSomething(int x, int y)
{
	//When you pass int a and int b to this function, essentially what is happening
	//is the values that a and b hold are copied to x and y, respectively.
	//So now you can perform any tests or calcuations you want using x and y.

	return true; //If everything turned out good, false if it didn't do what you're trying.
}


Surely you've working with passing arguments into functions. If not, I can explain that in more detail, but this should get you started
Perhaps I'm just a n00b when I ask this, but you can create a function with a bool? and if so, how do I take that true(or false) value back to the main? Haha, ignorance isn't always blissful :D

Thank you so very much for your time to help me on this issue... I've literally been working on this project for hours and I can't figure out what to do next.
ResidentBiscuit's example shows exactly how to do what you just asked. testForSometing()'s type is bool, which means it will return either true or false. To alter his example slightly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool testForEquality(int x, int y);

int main()
{
	bool HoldsTheBool;
	int a, b;
	cout << "Hey there, gimme two ints\n";
	cin >> a >> b;
	//Probably want to do some input validation
	HoldsTheBool = testForEquality(a,b);
	return 0;
}

bool testForEquality(int x, int y)
{
	if(x == y){
        return true;
	} else {
        return false;
	}
}
Last edited on
Ok well let's start with the basics. I'm gonna refer to my snippet I just posted.
Function signatures goes as follows
type name(parameters if any)

Type can be any data type, primitive or types that you make. It basically is just the data type that the function will return back to the caller. We'll assume that you call everything from main() for now. Since I made my function up there as bool type, that function is expecting a
return bool;
If you don't supply the proper return type, it probably won't even compile. You can also have functions as void, which you have up there. Void expects no return type, which also means you can't have a return statement in that function.

Now, let's put all this into context. You are trying to test rather 4 parameters are all different. You take this input in main and store into some variables. You will then pass these variables into this new function we made like so
testSomething(a,b,c,d);
Like I said in the above code, this will pass by value, the values of the variables you sent, and store these values into the variables defined in your function parameters. So now, you have 4 new variables with the same values that the user just gave you. Using these values you can now test them all against each other, and if there are any similarities you can return false, because they are not all different. If they ARE all different, you return true.

Now what return does is returns a value back to the caller. You are already familiar with this, just don't know it. You know how at the end of main you have return 0;?
Well guess what, that is returning the integer of 0 back to the caller, which is your OS. When you start a program, your OS invokes (calls) the main() function. See? You've been doing this all along, just weren't aware of it.

So let's try this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

//This tells the compiler that there is a function somewhere in here that will be implemented with this name
bool testReturns(int a, int b);

int main()
{
	int x;
	int y;
	std::cout << "Enter two numbers, and we'll send it to that function.\n";
	std::cin >> x >> y;
	std::cout << "Ok cool, now we'll compare them and return true (1) if they are different, or false (0), if they are the same\n";
	//Now here is going to be our function call.
	//When you call a function, you only need to supply the name and proper arguments
	//What this will do is call this function, the function will do it's work, and
	//then it will send back a 1 or 0, for true or false respectively.
	std::cout << testReturns(x,y);
	
	return 0;
}


//When you implement your function, you need to make sure the signature of it matches what you put earlier

bool testReturns(int a, int b)
{
	if(a==b)
	   return true;
	else
	   return false;
}


Now you should run this and watch what happens when you put in some various integer values.
That is EXTREMELY helpful. I literally just said "OOOOH! THAT's what return statements mean!" Thank you very much. I'll probably post more if I get in a rut... but for now, that should at least get me moving!
I'm not nearly experienced to the other guys that posted but when I read your way to face the problem with lots of nested ifs I thought "why not make all the comparisions in one if". So I made the program and tested:

Instead of this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if (a!=b || b!=c)
	{
		if (a!=c || c!=d)
		{
			if (d!=b || d!=a)
				parameters = true;
			else
				parameters = false;
		}
		else
			parameters=false;
	}
	else
		parameters=false;
				
	if (parameters=true)
		cout << "This is TRUE." << endl;
	else
		cout << "This is FALSE." << endl;


Just use somethign like this:
1
2
3
4
5
6
7
8
if (a!=b && a!=c && a!=d && b!=c && b!=d && c!=d)

	parameters = true;

	else
	parameters = false;

	cout << "Function results:" << std::boolalpha << parameters << endl;


So that you do not incur in nested ifs complicated logic.

Sorry for any mistakes in the code and for any orthographic mistakes English is not my first language.
The last reply was the most helpful for the problem I had, but all of the other replies were helpful, and taught me more about programming. Solved!! :D
Topic archived. No new replies allowed.