help with function that calculates area of a circle

closed account (EAk1vCM9)
Hello everyone, so for this program i have to read names and numbers from the file, from there I have to use the numbers to calculate the area of the circle, but I cant really figure out how to fix my function that calculates the area, as of now it prints out the same number for all people(which im not sure how my program keeps getting that number) so any ideas on how to fix it? ps. Im required to use a string array and double array

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

using namespace std;

double CalcCircleArea(double [], int SIZE);

const int SIZE=12;
const double PI = 3.14;

int main() {

	string FirstName[SIZE],dummy[SIZE];
	double circleArea[SIZE],areaTotal=0;

	ifstream infile("PiiCircles.txt"); //opens file

	    if (!infile) // checks if file opened
	    {
	        cout << "Could not open input file\n"; // tells user if file didnt open correctly
	        return 1;
	    }
	    else
	    {
	    	for(int i=0; i < SIZE;i++)
	    	{
	    		infile >> FirstName[i] >> dummy[i] >> circleArea[i];
	    	}
	    	areaTotal =CalcCircleArea(circleArea,SIZE);
	    	for(int i=0; i <SIZE; i++)
	    	{
	    		cout << left << setw(10) << FirstName[i]  << areaTotal <<" " <<endl;
	    	}
	    }
	    infile.close();





	return 0;
}
double CalcCircleArea(double circleArea[SIZE], int SIZE)
{
	double area=0;

	for(int i=0; i <SIZE; i++)
	{
	area= PI * circleArea[i] * circleArea[i] ;
	}

	return area;

}

current output:
LoriBeth  138474 
Chachi    138474 
Richie    138474 
Howard    138474 
Marion    138474 
Joanie    138474 
Al        138474 
Arthur    138474 
Ralph     138474 
Roger     138474 
Jenny     138474 
Potsie    138474 


file used:

LoriBeth Allen 79
Chachi Arcola 125
Richie Cunningham 171
Howard Cunningham 255
Marion Cunningham 252
Joanie Cunningham 234
Al Delvecchio 122
Arthur Fonzarelli 255
Ralph Malph 165
Roger Phillips 61
Jenny Piccalo 55
Potsie Weber 210
Last edited on
closed account (48T7M4Gy)
line 31 is not doing what you want where you want it:

f
1
2
3
4
5
6
7
8
9
10
or(int i=0; i < SIZE;i++)
	    	{
	    		infile >> FirstName[i] >> dummy[i] >> circleArea[i];
	    	}
	    	//areaTotal =CalcCircleArea(circleArea,SIZE);
	    	for(int i=0; i <SIZE; i++)
	    	{
	    		areaTotal = CalcCircleArea(circleArea[i]);
                        cout << left << setw(10) << FirstName[i]  << areaTotal <<" " <<endl;
	    	}

Which means you need to modify the area function. (There is another alternative but that's unecessary)

1
2
3
4
5
6
7
8
double CalcCircleArea(double diameter)
{
	double area=0;
        area= PI * diameter * diameter/4 ;

        return area;

}


closed account (48T7M4Gy)
And you can do it all in one loop by the look of it:
1
2
3
4
5
6
7
8
9
10
for(int i=0; i < SIZE;i++)
{
    infile >> FirstName[i] >> dummy[i] >> circleArea[i];
	    	//}
	    	//areaTotal =CalcCircleArea(circleArea,SIZE);
	    	//for(int i=0; i <SIZE; i++)
	    	//{
    areaTotal = CalcCircleArea(circleArea[i]);
    cout << left << setw(10) << FirstName[i]  << areaTotal <<" " <<endl;
}
Put other way, the line 31 computes "areaTotal" exactly once and line 34 prints the same value with each name.


Note: line 51 overwrites the "area" every time. Your CalcCircleArea() does same as this:
1
2
3
4
5
6
7
8
double CalcCircleArea(double circleArea[SIZE], int SIZE)
{
	if ( 0 < SIZE )
	{
	  return PI * circleArea[SIZE-1] * circleArea[SIZE-1] ;
	}
	else return 0.0;
}
closed account (48T7M4Gy)
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

double CalcCircleArea(double);

const int SIZE=12;
const double PI = 3.14;

int main() {
    
    string FirstName[SIZE],dummy[SIZE];
    double circleArea[SIZE],areaTotal=0;
    
    ifstream infile("circle_area_size.txt"); //opens file (use your file name)
    
    if (!infile) // checks if file opened
    {
        cout << "Could not open input file\n";
        return 1;
    }
    else
    {
        for(int i=0; i < SIZE;i++)
        {
            infile >> FirstName[i] >> dummy[i] >> circleArea[i];
            areaTotal = CalcCircleArea(circleArea[i]);
            cout << left << setw(10) << FirstName[i]  << areaTotal <<" " <<endl;
        }
    }
    infile.close();
    
    return 0;
}

double CalcCircleArea(double diameter)
{
    double area=0;
    area= PI * diameter * diameter/4 ;
    
    return area;
}


LoriBeth  4899.19 
Chachi    12265.6 
Richie    22954.2 
Howard    51044.6 
Marion    49850.6 
Joanie    42983.5 
Al        11683.9 
Arthur    51044.6 
Ralph     21371.6 
Roger     2920.99 
Jenny     2374.63 
Potsie    34618.5 
Program ended with exit code: 0
closed account (EAk1vCM9)
Ahh I see, thank you for the help guys !
Topic archived. No new replies allowed.