problem with reading files

i'm trying to sort out a file by tokenizing the strings, then after figure out how many tokens are on each line, then after see if each line has a certain object and if it has enough data to find the perimeter, area, surface area, etc...

this is what i have so far:
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <fstream>
using std::ifstream;

#include <cmath>

#include <cstring>
using std::strtok;
using std::strcmp;

#include <cstdlib>

const int MAX_CHARS_PER_LINE = 50;
const int MAX_TOKENS_PER_LINE = 4;
const char* DELIMITER = " ";

void squareCalc(double);
void rectangleCalc(double, double);
void circleCalc(double);
void cubeCalc(double);
void prismCalc(double, double, double);
void sphereCalc(double);
void cylinderCalc(double);
void cylinderCalc(double, double);
void triangleCalc(double, double, 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};

  double squareSide = 0;
  double rectangleSide1 = 0;
  double rectangleSide2 = 0;
  double circle = 0;
  double cubeSide = 0;
  double prismSide1 = 0;
  double prismSide2 = 0;
  double prismSide3 = 0;
  double sphere = 0;
  double cylinder1 = 0;
  double cylinder2 = 0;
  double triangleSide1 = 0;
  double triangleSide2 = 0;
  double triangleSide3 = 0;


  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 

    if(token[0])
    {
      for(n=1; n < MAX_TOKENS_PER_LINE; n++)
        {
        if((strcmp(token[0], "SQUARE") == 0))
        {
          squareSide = atof(token[1]);;
        } //if

        if((strcmp(token[0], "RECTANGLE") == 0))
        {
          rectangleSide1 = atof(token[1]);
          rectangleSide2 = atof(token[2]);
        } //if

        if((strcmp(token[0], "CIRCLE") == 0))
        {
          circle = atof(token[1]);
        } //if

        if((strcmp(token[0], "CUBE") == 0))
        {
          cubeSide = atof(token[1]);
        } //if

        if((strcmp(token[0], "PRISM") == 0))
        {
          prismSide1 = atof(token[1]);
          prismSide2 = atof(token[2]);
          prismSide3 = atof(token[3]);
        } //if

        if((strcmp(token[0], "SPHERES") == 0))
        {
          sphere = atof(token[1]);
        } //if

        if((strcmp(token[0], "CYLINDER") == 0))
        {
          cylinder1 = atof(token[1]);
          cylinder2 = atof(token[2]);
        } //if

        if((strcmp(token[0], "TRIANGLE") == 0))
        {
          triangleSide1 = atof(token[1]);
          triangleSide2 = atof(token[2]);
          triangleSide3 = atof(token[3]);
        } //if

        if(!token[n]) break;
      } //for
    } //if

    cout << endl;
  } //while

  squareCalc(squareSide);
  rectangleCalc(rectangleSide1, rectangleSide2);
  circleCalc(circle);
  cubeCalc(cubeSide);
  prismCalc(prismSide1, prismSide2, prismSide3);
  sphereCalc(sphere);
  cylinderCalc(cylinder1, cylinder2);
  triangleCalc(triangleSide1, triangleSide2, triangleSide3);

  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 << "SQUARE side=" << side << " perimeter=" << perimeter;
  cout << " area=" << area << endl;
} //squareCalc

void rectangleCalc(double side1, double side2)
{
  double perimeter;
  double area;

  perimeter = (side1 * 2) + (side2 * 2);
  area = side1 * side2;

  cout << "RECTANGLE side1=" << side1 << " side2=" << side2;
  cout << " perimeter=" << perimeter << " area=" << area << endl;
} //rectangleCalc

void circleCalc(double side1)
{
  double circumference;
  double area;
  double pow(double, int);

  double pie = 3.14;

  circumference = 2 * pie * side1;
  area = pie * pow(side1, 2);

  cout << "CIRCLE radius=" << side1;
  cout << " circumference=" << circumference << " area=" << area << endl;
} //rectangleCalc

void cubeCalc(double side1)
{
  double volume;
  double surfaceArea;

  double pow(double, int);

  volume = pow(side1, 3);
  surfaceArea = 6 * pow(side1, 2);

  cout << "CUBE side=" << side1;
  cout << " volume=" << volume << " surface area=" << surfaceArea << endl;
} //rectangleCalc

void prismCalc(double side1, double side2, double side3)
{
  double volume;
  double surfaceArea;

  volume = side1 * side2 * side3;
  
  double lw = side1 * side2;
  double lw2 = 2 * side1 + 2 * side2;
  
  surfaceArea = 2 * lw + lw2 * side3;

  cout << "PRISM side1=" << side1 << " side2=" << side2 << " side3=" << side3;
  cout << " volume=" << volume << " surface area=" << surfaceArea << endl;
} //prismCalc

void sphereCalc(double side1)
{
  double volume;
  double surfaceArea;

  double pie = 3.14;

  volume = (4/3) * pie * pow(side1, 3);
  surfaceArea = 4 * pie * pow(side1, 2);

  cout << "SPHERE radius=" << side1;
  cout << " volume=" << volume << " surfaceArea=" << surfaceArea << endl;
} //rectangleCalc

void cylinderCalc(double side1, double side2)
{
  double volume;
  double surfaceArea;

  double pie = 3.14;

  volume = 2 * pie * pow(side1, 2) * side2;
  surfaceArea = 2 * pie * pow(side1, 2) + 2 * pie * side1 * side2;

  cout << "CYLINDER side1=" << side1 << " side2=" << side2;
  cout << " volume=" << volume << " surfaceArea=" << surfaceArea << endl;
} //rectangleCalc

void triangleCalc(double side1, double side2, double side3)
{
  double perimeter;
  double area;

  perimeter = side1 + side2 + side3; 
  area = (1/2) * side1 * side2;

  cout << "TRIANGLE side1=" << side1 << " side2=" << side2 << " side3=" << side3;
  cout << " perimeter=" << perimeter << " area=" << area << endl;
} //triangleCalc 


here 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 15
CYLINDER 50 1.23
TRIANGLE 1.2 3.2


i was able to parse the file and put each word and variable into a token. i was then able to convert the tokenized variables into numbers since it is coming from a char. i am then able to find all the objects perimeter, area, etc...

the only problem is that when the input files have more the one of the same object it would crash my program and also if the file has objects with insufficient information, for example PRISM 1 2, prism is suppose to have 3 variables in order to find perimeter and such. so i need to figure out how to make it say PRISM Invalid Object and how it can take in an input file with more then one of the same shape.

any information will help.

this is what the output should look like:
SQUARE side=14.5 area=210.25 perimeter=58.00
RECTANGLE length=14.5 width=4.65 area=67.43 perimeter=38.30
CIRCLE radius=14.5 area=660.52 circumference=91.11
CUBE side=13 surface area=1014.00 volume=2197.00
PRISM length=1 width=2 height=3 surface area=22.00 volume=6.00
SPHERES invalid object
CYLINDER radius=1.23 height=0 surface area=9.51 volume=0.00
CYLINDER radius=50 height=1.23 surface area=16094.37 volume=9660.39
TRIANGLE invalid object
Last edited on
Topic archived. No new replies allowed.