Calculating Volume within a text

I have a text file that contains a list of shapes that looks like this

s1 tetrahedron 3.2
s2 cube 3.2
s3 octahedron 3.2
s4 dodecahedron 3.2
s5 icosahedron 3.2

s1 being the name, tetrahedron being the type, 3.2 being the side_length.

My goal is to find the volume for each shape and then use a sorting routine to sort the list by volume in ascending order. Here's my code, what would be the correct way to write my calculateVolume method?

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
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include <string>

using namespace std;

struct Platonic
{
	string name;
	string type;
	float side_length;
	float volume;
};

void readFile(Platonic shapes[])
{
	string line;
	ifstream myFile;
	float stemp = 0.0;
	float atemp = 0.0;
	myFile.open("Shapes.txt");
	if (myFile.is_open())
	{
		int i = 0;
		while (myFile.good() )
		{
		getline(myFile,line,' ');
		shapes[i].name = line;
		cout << line << endl;

		getline(myFile,line,' ');
		shapes[i].type = line;
		cout << line << endl;

		//convert line to float

		getline (myFile,line,' ');
		stemp = atof(line.c_str());
		shapes[i].side_length = stemp;
		cout << line << endl;

		atemp = atof(line.c_str());
		getline(myFile,line,' ');
		shapes[i].volume = atemp;
		cout << line << endl;
		}
		myFile.close();
	}
	else cout << "Unable to open file";
}

void calculateVolume(Platonic shapes[])
{

	Platonic myShapes;
	if(myShapes.type == "cube")
	{
		myShapes.volume = myShapes.side_length * myShapes.side_length * myShapes.side_length;
		
	
}
}

int main() 
{
	Platonic shapes[12];
	readFile(shapes);
	calculateVolume(shapes);
	
	return 0;
}
Write one function each per type for calculating the volume:
1
2
3
double volume_of_tetrahedron( double side_length ) ;
double volume_of_cube( double side_length ) ;
// etc 


And then:
1
2
3
4
5
6
7
8
9
10
11
void compute_volume( Platonic& p )
{
    if( p.type == "tetrahedron" ) p.volume = volume_of_tetrahedron( p.side_length ) ;
    else if( p.type == "cube" ) p.volume = volume_of_cube( p.side_length ) ;
    // etc
} 

void calculateVolumes( Platonic shapes[], int num_shapes )
{
     for( int i=0 ; i < num_shapes ; ++i ) compute_volume( shapes[i] ) ;
}

Alright I did as you told but how would I pass the compute_volume into my main method?

Correct me if I am wrong, what you did here was reference the struct Platonic and assigned it p. So that way I would be able to have a pointer to the shapes in calculateVolumes.

Thanks
> Correct me if I am wrong, what you did here was reference the struct Platonic and assigned it p

Yes. p is a reference to Platonic.
It is initialized (to refer to an object in the array) each time the function is called.

This is how you could call it from main():

1
2
3
4
5
6
7
8
9
10
11
12
13
void calculateVolumes( Platonic shapes[], int num_shapes ) ;

int main() 
{
        enum { NSHAPES = 12 } ;
	Platonic shapes[NSHAPES];
	
        // ....
	
        calculateVolumes( shapes, NSHAPES  ) ;

        // ...
}
I'm running into a problem, once I compile my code it tells me that 'Platonic shapes[12]': redefinition.

This is how my main() is looking:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() 
{

	enum {NSHAPES = 12};

	Platonic shapes[NSHAPES];

	Platonic shapes[12];
	readFile(shapes);
	calculateVolumes(shapes, NSHAPES);

	return 0;
}


Well, you are redefining it, aren't you?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main() 
{

	enum {NSHAPES = 12};

	Platonic shapes[NSHAPES];
        
        // shapes has already been defined above.
	// Platonic shapes[12]; // this is a redefinition

        // note: you might want to add an extra parameter to readfile() 
        // using which you can pass the maximum number of objects in the
        // array as an argument.
        // since the file may contain information about fewer objects
        // make readfile() return the actual number of objects read.   
	readFile(shapes);

	calculateVolumes(shapes, NSHAPES);

	return 0;
}
Topic archived. No new replies allowed.