I am so frustrated with this...

After the program calculates the triangle from the first line of the file, I need it to calculate, triangle number 2 from the second line of the file. How would I do that. All I'm getting from this is an infinite loop of the first triangle. The .txt file looks like this:

10.2 6.5 8.1
10 10 10
-1 10 -8

And here is the source code:

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
 
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

 double side1,
        side2,
        side3,
        equTriArea,
        tri_area,
        tri_perim;
       
ofstream fout;
ifstream fin;

void printIdInfo(ostream &);
void readData();
void printInvalidMsg();
void printEquilateralMsg();
double areaEquilateral(double);
double perimeterTriangle(double, double, double);
double areaTriangle (double side1, double side2, double side3);
void printResults();


int main()
{
    fout.open("prog5_out5.txt");
    
    printIdInfo(cout);
    printIdInfo(fout);
    readData();
             
             if ( side1 <= 0 || side2 <= 0 || side3 <= 0)
             printInvalidMsg();
             else do
             {
                 readData();
    
                 if ( side1 == side2 && side1 == side3 )
                 { 
                     printEquilateralMsg();
                     tri_area= areaEquilateral(side1);
                     tri_perim= perimeterTriangle(side1, side2, side3);
                     printResults();
                 }
    
                  else
                  {
                     tri_area= areaTriangle (side1, side2, side3);
                     tri_perim= perimeterTriangle(side1, side2, side3);
                     printResults();
                  }
               readData();
            }
            while ( side1 >= 0 && side2 >= 0 && side3 >=0 );
             
    
    system ("PAUSE>NUL");
    return 0;
    
}

/*
Function: printIdInfo

This function prints ID info to the screen

Receives: outfile variable
Constants:none
Returns:nothing
*/
        
void printIdInfo(ostream &out)
{
     out << "Jacob Hummer, Matthew Grant" << endl 
         << "C.S. 1428.0005" << endl
         << "11/12/09" << endl;
}

/*
Function:readData

This function reads inputs from a file

Receives: access to sides 1,2 and 3
Constants: none
Returns: nothing
*/
        
void readData()
{
       fin.open("prog5_inp5.txt");
       fin >> side1 >> side2 >> side3;
}

/*
Function:printInvalidMsg

This function prints a message if there is a negative number from the file

Receives: nothing
Constants: none
Returns: nothing
*/

void printInvalidMsg ()
{
     cout << "There were no valid input values on the input file.";
}

/*
Function:printEquilateralMsg

This function prints a message if the triangle is equilateral

Receives: nothing
Constants: none
Returns: nothing
*/
        
void printEquilateralMsg()
{
     fout <<"The Triangle is Equilateral " <<endl;
}

/*
Function:areaEquilateral

This function calculates the area of equilateral triangles

Receives: side1
Constants: none
Returns: area
*/
        
double areaEquilateral( double side1 )
{
       return ( pow(side1,2) * sqrt(3)) / 4.0;
}

/*
Function:perimeterTriangle

This function calculates the perimeter of the triangle.

Receives: side1, side2, side3
Constants: none
Returns: perimeter
*/
    
double perimeterTriangle(double side1, double side2, double side3)
{
       return side1+side2+side3;
}

/*
Function:areaTriangle

This function calculates the area of the non-equilateral triangle.

Receives: side1, side2, side3
Constants: none
Returns: area
*/

double areaTriangle (double side1, double side2, double side3)
{
   double s;
   
   s = perimeterTriangle ( side1, side2, side3 ) / 2.0; 
     
   return sqrt( s * ( s - side1 ) * ( s - side2 ) * ( s - side3 ) );
}

/*
Function:areaTriangle

This function calculates the area of the non-equilateral triangle.

Receives: side1, side2, side3, tri_area, tri_perim, and outfile variable
Constants: none
Returns: nothing
*/

void  printResults ()
{
     fout <<endl
          <<"The sides of the triangle are " << side1 <<", " << side2 
          <<", and " << side3 <<"." <<endl
          <<"The area of the triangle is " << tri_area <<". \n" 
          <<"The perimeter of the triangle is " << tri_perim 
          << endl << endl;
}

 
Last edited on
You should open the file in main not in readData.
You should also pass arguments to the functions instead of using all those global variables
Yes, opening the file in main fixed the problem. Thank you very much.
Topic archived. No new replies allowed.