char isn't transferring in my function

In my classification function, the char isn't transferring to main. Am i suppose to make it a string.
Or perhaps something else i messed up on
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

#include <iostream>
using namespace std;
double validity(double, double, double);
double classification(double, double, double, char);

int main()
{
	double a,b,c;
        char t;

	cout << " Please input three degrees of a triangle: ";
	cin >> a;
	cin >> b;
	cin >> c;
	validity(a,b,c);
	cout << a << " " << b << " " << c << endl;
	classification(a,b,c,t);
	cout << a << " " << b << " " << c << " " << t << endl;
	return 0;
}

//checks for valid triangles 
double validity(double a, double b, double c)
{
	cout << a << " " << b << " " << c << endl;
	if (a>0 && b>0 && c>0)
		cout << "Valid Angles" << endl;
	else
		cout << "invalid" << endl;
	

	if(a+b+c==180)
		cout << "Valid Size" << endl;
	else 
		cout << "invalid" <<endl;
	
	return a,b,c;
}

double classification(double a, double b, double c, char t)
{
	if(a==b && b==c && a==c)
		t='equalateral';
	
	else if(a==b || b==c || a==c))
		t='isosceles';

	else
		t='scalene';

	return a,b,c,t;
}
Last edited on
t is a char that holds 1 character, you are passing multiple characters on line 44, 47 and 50
Just to extend on my last post, you cant return multiple variables using return
Line 38,52: You can't return multiple values like that.

Line 41: You're passing t by value. Any changes to t will not not be reflected in the caller. You want to pass by reference.

Lines 44,47,50: A character literal is limited to a single character. You want t to be a string passed by reference and these literals need to use double quotes (").
I would suggest changing the function to accept just the three sides as parameters, and return the result as a string.
std::string classification(double a, double b, double c)

To return multiple values from a function you can use a structure like this. Note that I have just used your variables so you can see the difference but refer to AbstractionAnon's post.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

struct Data
{
	double a;
	double b;
	double c;
	char t;
};

Data sampleFunction(double, double, double, char);

int main()
{

	// used for function calls
	Data ret;

	ret = sampleFunction(5, 4, 3, 'a');

	// ret.a, ret.b, ret.c, ret.t will have
	// return values from function.

	return 0;

};

Data sampleFunction(double a, double b, double c, char t)
{
	// create a object to populate the values
	// of a, b, c and t that you want to return 
	// back.
	Data ret;

	// do something with a, b, c, t


	// populate
	ret.a = a;
	ret.b = b;
	ret.c = c;
	ret.t = t;

	//return struct
	return ret;

}

Last edited on
i fixed up this part but it still isn't coming up in main
1
2
3
4
5
6
7
8
9
10
11
12
13
string classification(double a, double b, double c, string t)
{
	if(a==b && b==c && a==c)
		t="equalaterial triangle";
	
	else if(a==b || b==c || a==c)
		t="isosceles triagle";

	else
		t="scalene triangle";

	return t;
}
Call the function from main like this:
string t = classification(a,b,c);
and omit the 4th parameter.
yes! thank you.
So is the reason it didn't work properly from my last post was because string t was not equal to anything?
No, that isn't the reason. The parameter t was passed by value. That means a copy of the variable t in main was made and so any changes made inside the function affected that separate copy, leaving the variable in main unchanged.

(You can also pass variables by reference instead - that's sometimes useful too. See the tutorial for more details. http://www.cplusplus.com/doc/tutorial/functions/ )
Last edited on
gotcha thanks again everyone!
Topic archived. No new replies allowed.