strings and failed passing

I'm trying to have classification return two strings but it is only giving me one. I know that i can return more than one at a time so i tried messing around with void and passing by ref but i had trouble, any advice?

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string> 
using namespace std;
string validity(double, double, double);
string classification(double, double, double);
string classi(double, double, double);
string kind(double, double, double);



int main()
{
	double a,b,c;
	double x=0,y=0;
	ifstream infile;
	ofstream outfile;
	outfile.open("output.txt");
	infile.open("input.txt");

	outfile << "Angle 1    Angle 2    Angle3    Status       Type           Kind" << endl;
	while(!infile.eof())
	{
	infile >> a >> b >> c;
	string i = validity(a,b,c);
	string t = classification(a,b,c);
	string k = classification(a,b,c);											
	outfile << a << setw(15) << b << setw(10) << c <<setw(10) << i << setw(13) << t << setw(15) << k << endl;
	if(i=="Valid")
		x++;
	else
		y++;
	}
	
	outfile <<"Valid Triangles: " << x << "\n" <<"Inavlid Triangles: " << y << endl;

	infile.close();
	outfile.close();
	return 0;
}

//checks for valid triangles 
string validity(double a, double b, double c)
{
	string i;
	if (a<0 || b<0 || c<0 || a+b+c==180)
		i="Valid";
		
	else 
		i="Invalid";
	return i;
}

//classifys the triangle
string classification(double a, double b, double c)
{
	string t = classi(a,b,c);
	string k = kind(a,b,c);
	return t,k;
}

string classi(double a, double b, double c)
{
	string t;
	if(a==b && b==c && a==c)
		t="equalaterial";
	
	else if(a==b || b==c || a==c)
		t="isosceles";

	else
		t="scalene";

	return t;
}

string kind(double a, double b, double c)
{
	string k;
	if(a==90 || b==90 || c==90)
		k="right";
	else if(a>90 || b>90 || c>90)
		k="obtuse";
	else
		k="acute";

	return k;
}
TMK you can only return one. You could however, concat a string using the + operator giving you the illusion of returning two strings.
it works but a little messy in the outfile
If you wanted your code to stay close to the same, you could use references. Otherwise it is just a matter of manipulation and formatting to get it to look right.
1
2
3
4
5
6
7
8
9
10
 
void classification(double a, double b, double c, string &t, string &k)
{
      t = classi(a, b, c);
      k = kind(a, b, c);
}

// function call in main
string t, k;
classification(a, b, c, t, k);


Your code with my change and output.txt:
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string> 
using namespace std;
string validity(double, double, double);
void classification(double, double, double, string &t, string& k);
string classi(double, double, double);
string kind(double, double, double);



int main()
{
	double a,b,c;
	double x=0,y=0;
	ifstream infile;
	ofstream outfile;
	outfile.open("output.txt");
	infile.open("input.txt");

	outfile << "Angle 1    Angle 2    Angle3    Status       Type           Kind" << endl;
	while(!infile.eof())
	{
	infile >> a >> b >> c;
	string i = validity(a,b,c);
	string t, k;
	classification(a, b, c, t, k); 										
	outfile << a << setw(15) << b << setw(10) << c <<setw(10) << i << setw(13) << t << setw(15) << k << endl;
	if(i=="Valid")
		x++;
	else
		y++;
	}
	
	outfile <<"Valid Triangles: " << x << "\n" <<"Inavlid Triangles: " << y << endl;

	infile.close();
	outfile.close();
	return 0;
}

//checks for valid triangles 
string validity(double a, double b, double c)
{
	string i;
	if (a<0 || b<0 || c<0 || a+b+c==180)
		i="Valid";
		
	else 
		i="Invalid";
	return i;
}

//classifys the triangle
void classification(double a, double b, double c, string &t, string &k)
{
	t = classi(a,b,c);
	k = kind(a,b,c);
}

string classi(double a, double b, double c)
{
	string t;
	if(a==b && b==c && a==c)
		t="equalaterial";
	
	else if(a==b || b==c || a==c)
		t="isosceles";

	else
		t="scalene";

	return t;
}

string kind(double a, double b, double c)
{
	string k;
	if(a==90 || b==90 || c==90)
		k="right";
	else if(a>90 || b>90 || c>90)
		k="obtuse";
	else
		k="acute";

	return k;
}
Angle 1    Angle 2    Angle3    Status       Type           Kind
2             24         2   Invalid    isosceles          acute
60             60        60     Valid equalaterial          acute
30             30        30   Invalid equalaterial          acute
30             30        30   Invalid equalaterial          acute
Valid Triangles: 1
Inavlid Triangles: 3
Last edited on
oh i was told it goes like this string& t, thanks!
Well it can go either way <type>& t or <type> &t and same with pointers, but I prefer the latter especially if you are the kind of person that does like I do and make two or three declarators per line like I did with string t, k;. A lot of beginners get told to do the former and end up with errors like this:
1
2
3
4
5
6
7
// only the first is a pointer and the second one is an int
// not what the programmer intended
int* ptrFirstPointer, ptrSecondPointer;

// now both are pointers, though with C++11 I should really do
// is set both to nullptr...let me do that below
int *ptrFirstPointer = nullptr, *ptrSecondPointer = nullptr;


Glad I could help.
Last edited on
Topic archived. No new replies allowed.