need help with tokens

i'm stuck on a program that i am working on. your suppose to open a text file with names of shapes and certain lengths of it's sides. it then calculates what it's area/perimeter/volume/circumference and so on and so forth for each shape named in the text file.

my programming is kinda rusty, but i was able to tokenize the input file and was able to print on screen. now i am stuck trying to figure out how to manipulate the tokens. i am not sure how i can manipulate the tokens.

here 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
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <fstream>
using std::ifstream;

#include <cmath>

#include <cstring>
using std::string;
using std::strtok;
using std::strncmp;

#include <cstdlib>

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

int main()
{
  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;

  shape_token hold;

  char* token[MAX_TOKENS_PER_LINE] = {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

    for(int i = 0; i < n; i++)
    {
      cout << "Token[" << i << "] = " << token[i] << endl;
    }

    cout << endl;
  } //while

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

  return 0;
} //main 


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


the output should look something like this:
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


if certain objects don't have enough information to calculate what needs to be calculated then it outputs invalid object.
I'm not sure I would worry about some really fancy tokenizer... If every object is just a name followed by numbers then all you need is a struct to represent an object as a token group:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct obj_toks_t
  {
  string name;
  vector <double> arguments;
  };

istream& operator >> ( istream& ins, obj_toks_t& ots )
  {
  ins >> ots.name;
  double d;
  while (ins >> d)
    ots.arguments.push_back( d );
  return ins;
  }

You can use this to collect information about objects from the input:

1
2
3
4
5
6
7
8
9
10
vector <obj_toks_t> obj_toks;

string s;
while (getline( f, s ))
  {
  obj_toks_t ots;
  istringstream ss( s );
  if (ss >> ots)
    obj_toks.push_back( ots );
  }

After that, you can use some simple function lookup to select how to use your token group.
Use a map <string, fnptr> to collect functions of the
form void fn( const vector <double> & args );

1
2
3
4
5
6
7
for (size_t n = 0; n < obj_toks.size(); n++)
  {
  if (fnmap.count( obj_toks[ n ].name ))
    {
    fnmap[ n ]( obj_toks[ n ].arguments );
    }
  }

Hope this helps.
well i kinda have to use tokens because thats what my instructor said to use, so i'm going with that. i'm just having a problem manipulating the tokens because everything i try to store into token[0] is the name of the shape. so if i try to look up say for instance SQUARE, i would put that in an if statement, but when i try to do that it can't find it in the token. so that is where im confused.
Topic archived. No new replies allowed.