file i/o, structs, arrays. confused help please

i have a program that reads in a file with shapes and numbers and tokenizes each line. after it tokenizes, it then calculates the shapes dimensions. what i need to do is make it so that this program reads in the file and stores every shape and its values into an array first. then after it stores everything into an array it then does it's calculations and then last prints out the results.

changes i need to do:
- Use an array of generic pointers (for example, void* shapes[100], and another array of integer codes to store up to 100 objects. I also will need a way to keep track of exactly how many valid objects are stored. Also, i need to use dynamic memory allocation to create struct instances, and store the memory address of each in the array of generic pointers.
- I need to write print functions for each shape (for example, void printSquare(Square* s)), which should print to the cout standard console output.

im having trouble storing the char* tokens into an array with its name and values. This part must be done first before it calculates anything. So if there are multiples of the same shapes, then that must be stored first.

any help or code would help a lot. appreciate it.

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
const int MAX_CHARS_PER_LINE = 50;
const int MAX_TOKENS_PER_LINE = 4;
const char* DELIMITER = " ";

struct Square
{
  double length;
};

void squareCalc(double);

int main()
{
  cout << endl;
  cout << "Description: Calculates area, perimeter, surface area, and volume" << endl;
  cout << "of 6 different geometric objects." << endl;
  cout << endl;
  
  ifstream fin;
  fin.open("geo.txt");
  if(!fin.good())
    return 1;

  char* token[MAX_TOKENS_PER_LINE] = {0};

  Square shape;

  while(!fin.eof())
  {
    char buf[MAX_CHARS_PER_LINE];
    fin.getline(buf, MAX_CHARS_PER_LINE);

    int n = 0;

    token[0] = strtok(buf, DELIMITER);

    if(token[0])
    {
      for(n = 1; n < MAX_TOKENS_PER_LINE; n++)
      {
        token[n] = strtok(0, DELIMITER);
        if(!token[n]) break;
      } //for

      if((strcmp(token[0], "SQUARE") == 0))
      {
        if(n != 2)
          cout << token[0] << " Invalid Object";
        else
        {
          shape.length = atof(token[1]);
          squareCalc(shape.length);
        } //else
      } //if

    cout << endl;
  } //while

  cout << endl;
  cout << "Press Enter to continue..." << endl;
  cin.get();

  return 0;
} //main


void squareCalc(double side)
{
  double perimeter;
  double area;

  perimeter = side * 4;
  area = side * side;

  cout.setf(ios::fixed|ios::showpoint);
  cout << setprecision(2);
  cout << "SQUARE side=" << side << " perimeter=" << perimeter;
  cout << " area=" << area;
} //squareCalc 


this is the input file:
SQUARE 14.5
RECTANGLE 14.5    4.65
CIRCLE 14.5
CUBE 13
PRISM 1 2 3

SPHERES 2.4
CYLINDER 50 1.23
TRIANGLE 1.2 3.2 3.4
Topic archived. No new replies allowed.