Working with functions and infile/outfile?

My assignment: "Write a C++ program to do the following: The main program reads in three integers,
representing the degree measurement of three angles of a triangle and sends them to a function.

If the function says that the three integers form a valid triangle, the main program calls
another function to classify the triangle. After repeating this process for the entire set of
data, the main program prints out how many groups formed valid triangles and how many formed
invalid ones.
The first function, which checks for validity, uses the following rule: The three angles form
a valid triangle if they add up to 180 and each one is greater than 0. For example 70, 60 and 50
is valid while 120, 80, and -20 is invalid.
The classification function calls two additional functions. One of them determines if the
triangle is equiangular (all three angles are equal), isosceles (exactly two angles are equal),
or scalene (all three angles different). The other function determines if the triangle is right
has one angle with 90 degrees), obtuse (one angle above 90), or acute (all three below 90).

Be sure to cover all the possible combinations and to include some invalid triangles as well.
Make sure your name appears on the printout.
You should create a well formatted report indicating whether or not the set of angles is valid
or not. If they are valid then classify the triangles as to whether it is equiangular, isosceles
or scalene, and whether it has an obtuse angle, right angle or is acute."

Right now, the main problem I'm aware of is that in the bolded part, "validity" is incorrect because "no instance of overloaded function 'validity' matches the argument list."

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
  #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

ifstream infile; 
ofstream outfile;

int classify(ofstream&, int, int, int);
int validity(ofstream&, int, int, int);
void type();
void kind();

int main()
{
	int angle1;
	int angle2;
	int angle3;
	int validtriangles;
	int invalidtriangles;
	string valid;
	string type;
	string kind;

	infile.open("infile.txt");
	outfile.open("outfile.txt");

	if (!infile)
	{
		cout << "Cannot open." << endl;
	}

	outfile << "Angle 1    Angle 2     Angle 3     Status      Type      Kind" << endl;
	while (!infile.eof())
	{
		infile >> angle1 >> angle2 >> angle3;

		return angle1;
		return angle2;
		return angle3;

		classify();

		outfile << "  " << angle1 << "    " << angle2 << "       " << angle3 << "     " << valid << "         " << type << "       " << kind << endl;
	}
		
	validtriangles = validity();
	invalidtriangles = validity();

	outfile << endl;
	outfile << validtriangles << " valid triangles." << endl;
	outfile << invalidtriangles << " invalid triangles" << endl;

	infile.close();
	outfile.close();

	return 0;
}
Last edited on
closed account (SECMoG1T)
Please check these from your original code , correct each bit and your prg should work.

Your line 12 declares a function known as validityint validity(ofstream&, int, int, int); taking four arguments.

1
2
3
4
5
6
7
8
validtriangles = validity(); 	///you dint provide any of the 4 arguments so your compiler expected to find 
invalidtriangles = validity();/// an overload of these function that takes no argument.  You'll need to 
          /// provide all the four arguments for these to work. 

///FYI /****/ I checked your definition of the function validity and I saw that you attempted to return two  
/// variables line 85 & 86 , well that's not how it is done, you could return a pair object with both values
/// or you could return one value from your function while the other variable is passed by reference to the 
// function  



int validity(int, int, int, int, int) line 62
It seems your function needs some arguments for the parameters you have provided, I can see you ended up creating some local variables instead, just remove those locals and use parameter identifiers instead.

On line 71, 72, 73, 102, 103, 104, 136, 137, 138 . It seems you attempted to call your main function, might be you aren't aware of it but it's illegal to call your main function.

I am also sure that your classify and type functions too needs parameters because according to your explanation I guess all these operations are called on the same values , if you provide parameters to these functions you'll just require to plug in those values and that's all.

Please go through your functions again and check out all those mistakes av pointed out plus any other I might have missed, and your program will be fine again.
Well, I've made some adjustments, deleting the main calls like you said, and that seemed to fix the validity call, but now it says there's something wrong with the classify function.
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
void classify();
int validity();
void type();
void kind();

int main()
{
	int angle1;
	int angle2;
	int angle3;
	int validtriangles;
	int invalidtriangles;
	string valid;
	string type;
	string kind;

	infile.open("infile.txt");
	outfile.open("outfile.txt");

	if (!infile)
	{
		cout << "Cannot open." << endl;
	}

	outfile << "Angle 1    Angle 2     Angle 3     Status      Type      Kind" << endl;
	while (!infile.eof())
	{
		infile >> angle1 >> angle2 >> angle3;

		return angle1;
		return angle2;
		return angle3;

		classify();

		outfile << "  " << angle1 << "    " << angle2 << "       " << angle3 << "     " << valid << "         " << type << "       " << kind << endl;
	}
		
	validtriangles = validity();
	invalidtriangles = validity();

	outfile << endl;
	outfile << validtriangles << " valid triangles." << endl;
	outfile << invalidtriangles << " invalid triangles" << endl;

	infile.close();
	outfile.close();

	return 0;
}

int validity(int, int, int, int, int)
{
	int angle1;
	int angle2;
	int angle3;
	int validtriangles = 0;
	int invalidtriangles = 0;
	string valid;

	if (angle1 > 0 && angle2 > 0 && angle3 > 0 && (angle1 + angle2 + angle3 == 180))
	{
		validtriangles++;
		valid = "Valid";
	}
	else
	{
		invalidtriangles++;
		valid = "Invalid";
	}
	return validtriangles;
	return invalidtriangles;
}

void classify()
{
	type();
	kind();
}

void type()
{
	int angle1;
	int angle2;
	int angle3;
	string type;

	if (angle1 > 0 && angle2 > 0 && angle3 > 0 && (angle1 + angle2 + angle3 == 180))
	{
		if (angle1 == angle2 == angle3)
		{
			type = "Equiangular";
		}

		else if (angle1 == angle2 || angle1 == angle3 || angle2 == angle3)
		{
			type = "Isosceles";
		}

		else
		{
			type = "Scalene";
		}
	}
	else
	{
		outfile << "Invalid";
	}
}

void kind()
{
	int angle1;
	int angle2;
	int angle3;
	string kind;
	
	if (angle1 > 0 && angle2 > 0 && angle3 > 0 && (angle1 + angle2 + angle3 == 180))
	{
		if (angle1 == 90 || angle2 == 90 || angle3 == 90)
		{
			kind = "Right";
		}
		else if (angle1 > 90 || angle2 > 90 || angle3 > 90)
		{
			kind = "Obtuse";
		}
		else if (angle1 < 90 && angle2 < 90 && angle3 < 90)
		{
			kind = "Acute";
		}
	}
	else
	{
		outfile << "Invalid" << endl;
	}
	
}
Last edited on
To go into more detail, the two remaining syntax errors are:

- "Error 1 error C2556: 'void classify(void)' : overloaded function differs only by return type from 'int classify(void)"

- "Error 2 error C2371: 'classify' : redefinition; different basic types"

closed account (SECMoG1T)
Lemmi give you an examples on how to use parameters from your function declaration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 bool validity (int, int, int)//// your declaration

bool validity (int angle1, int angle2, int angle3)//how to define your function
{
  
  if (angle1 > 0 && angle2 > 0 && angle3 > 0 && (angle1 + angle2 + angle3 == 180)) 	
      return true;
  else 	
 	return false;
 }

 int main ()
 {
    int a=40, b=60, c=80, valid=0, invalid=0;

    if (validity (a, b, c))
      ++valid;
    else
      ++invalid; 
 }
Last edited on
1
2
3
4
5
6
7
8
9
 int main ()
 {
    int a=40, b=60, c=80, valid=0, invalid=0;

    if (validity (a, b, c))
      ++valid;
    else
      ++invalid; 
 }


I don't think I can do this because I have to read all of the angles from an infile.
closed account (SECMoG1T)
All I wanted to demonstrate to you is how you are supposed to define all your functions,
After you define them using that routine, you can then read your file data and simply apply your function as I demonstrated in the example with the main function.
Sorry if it sounds like I'm stupid or something by this point, but I made this my function declaration.....: int validity(int, int, int, int, int, string);

.....I added this to main.....:
1
2
3
4
5
6
7
8
if (validity(angle1, angle2, angle3))
	{
		validtriangles++;
	}
	else
	{
		invalidtriangles++;
	}


......and this is my validity function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int validity(int angle1, int angle2, int angle3, int validtriangles, int invalidtriangles, string valid)
{

	if (angle1 > 0 && angle2 > 0 && angle3 > 0 && (angle1 + angle2 + angle3 == 180))
	{
		validtriangles++;
		valid = "Valid";
	}
	else
	{
		invalidtriangles++;
		valid = "Invalid";
	}
	return validtriangles;
	return invalidtriangles;
}


And now it says there's too few arguments in my function call. Not to mention I still have no idea how to fix the 2 classify problems.
closed account (SECMoG1T)
No no no , all of us came from that point , guided step by step till we understood the concept.
You should always make sure that you provide the same number of arguments just as those parameters used in function definition. Parameters are used as placeholders in declarations and definitions while arguments are used to substitute the parameters in function calls.

According to your problem description you should have a function to confirm the validity of the three values read from a file, if the values are valid then increment the valid counter and proceed to classify the values. I believe the validity function should have a form similar to the one I gave you, once you find the values are valid you should pass the values as arguments to your classify and type function, these functions to I believe they should take three arguments which are the three valid values, these function might return a string or some thing representing the type and kind of your triangle.

Examples on funct classify

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
pair <string, syring> classify (int angle, int angle1,  int angle2);///declaration

pair <string, string> classify (int angle, int angle1,  int angle2)///definition
 {
     string typ, knd;
  
     typ=type(angle, angle1, angle2); 	
     knd=kind(angle, angle1, angle2);

     return make_pair (typ, knd);
  }

/// usage example
 
 int main ()
 {
    pair <string, string> result;//will use this to contain my classification results
    if (validity (a, b, c))///a b c are values read from a file
     {
       ++valid; ///increment valid counter and call classify
       
        result=classify (a, b, c);

        ///result.first== to thr type of triangle while result.second== to the kind of triangle
      }

     else 
       ++invalid; 
   }
         


       
     





I'm not sure what pair does; we haven't learned about that yet. At this point, I'd like to just fix those remaining errors and getting it to run. Thanks for all of your help today, by the way.
closed account (SECMoG1T)
Yeah but note if you want your function to work as stated you should define them to take the values read from your file as arguments, if you aren't used to pairs, alternatively you can pass a fourth argument by reference that will contain the result of classification , both in your type and kind functions. Avoid creating local variables in your functions, just use you parameters as I demonstrated.
Last edited on
Alright, I've fixed the "too few arguments" error, but that still leaves me with C2556 and C2371. Any tips?

1
2
3
4
5
6
7
void classify()
{
	string tritype;
	string trikind;
	type(tritype);
	kind(trikind);
}
closed account (SECMoG1T)
Please note all your functions should have this kind of declaration or something close to.

1
2
3
4
5
6
7
 bool validity (int angle,int angle1, int angle2);
 string type (int angle, int angle1,  int angle2);
 string kind (int angle, int angle1,  int angle2);
 void classify (string& tritype, string& trikind, int angle, int angle1, int angle2);///tritype and trikind should 
  ///be assigned the results of calling type (a, b, c), kind (a, b, c) respectively



Please follow the guide on my second post to guide to define your functions correctly,
Also note that you should provide equal number of arguments as your functions parameters.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
///example usage . Assuming you have defined your functions correctly

 ///read three values a, b and c from a file
 /// create two strings to hold the typeinfo and kindinfo of each valid triangle,  you'll also require a valid and 
 /// invalid triangle counters

  if (validity (a, b, c))
    {
       ///call classify with the previous strings we just created
        classify (typeinfo, kindinfo, a, b, c);///exactly five arguments as we declared and defined our 
                                                                     ///function. 

       ///classify function might have the following form
         typeinfo=type (a, b, c);///exactly three arguments as in function definition
         kindinfo=kind (a, b, c);/// "         "           "               "
 
   ///after all that now we have all the info we needed and we can write it to a file or the console
    ///out is our output stream; 
  
    out <<a <<" "<<b <<" "<<c <<" is a valid triangle of the type "<< typeinfo <<" and kind "<< kindinfo <<endl; 



There you go, that's all you needed and probably embeding that code in a loop if your file contains a series of readings.


Last edited on
Topic archived. No new replies allowed.